I have a many to many relationship between Profesional
and Plan
so I wrote the following code:
Profesional.php
class Profesional extends BaseModel {
static $table_name = 'profesionales';
static $primary_key = 'idprofesional';
static $has_many = array(
array(
'_planes',
'class_name' => 'Plan',
'through' => '_profplanes'
),
array(
'_profplanes',
'class_name' => 'ProfPlan',
'foreign_key' => 'profesional'
)
);
}
profplan.php
class ProfPlan extends BaseModel{
static $table_name = 'profplanes';
static $belongs_to = array(
array(
'_plan',
'class_name' => 'Plan',
'foreign_key' => 'plan'
),
array(
'_profesional',
'class_name' => 'Profesional',
'foreign_key' => 'profesional'
)
);
}
plan.php
class Plan extends BaseModel {
static $table_name = 'planes';
static $primary_key = 'idplan';
static $has_many = array(
array(
'_profplanes',
'class_name' => 'ProfPlan',
'foreign_key' => 'plan'
)
);
}
But I get Class Profplane does not exist
. Where is the error? What am I doing wrong?
Fix the class / table spellings! it'll be easy to fix early in your code and becomes a maintenance nightmare later on =P! "Plan->Plans' 'Profesional=>Professional', "Professionales->"Professionals".
Honestly the through relationship code in phpactiverecord is annoying, fickle, and really needs to be looked at in general.
You should show how you're actually calling the code, but I'm going to assume you're doing
Professional::find(1)
$professional->_plans, //Error is thrown here
You need to set up your code so that ProfPlan->_plans
(or _planes
in your case), is definded, and not just ProfPlan->_plan
. Phpactiverecord needs to explicitly know the relationship it's going to go through. Let me know if you need a more clear explanation.