为什么这段代码坏了?这应该有效,但事实并非如此。
我有三张桌子:
applications
id, name
subjects
id, name
application_subjects
application_id, subject_id, notes
这些是我的模特:
<? # Application.php
class Application extends ActiveRecord\Model
{
static $has_many = array(
array('application_subjects'),
array('subjects', 'through' => 'application_subjects')
);
}
?>
<? # Subject.php
class Subject extends ActiveRecord\Model
{
}
?>
<? # ApplicationSubject.php
class ApplicationSubject extends ActiveRecord\Model
{
static $has_many = array(
array("subjects")
);
}
?>
以下是我访问该模型的代码:
$app = Application::find_by_id(1); # this brings up a valid application record
foreach($app->subjects as $s) {
echo $s->name;
}
然而,当我运行代码时,我得到了:
Fatal error: Uncaught exception 'ActiveRecord\HasManyThroughAssociationException' with message
'Could not find the association application_subjects in model Application'
答案 0 :(得分:1)
看起来ApplicationSubject
是一个连接模型。
如果是这样,请考虑以下设置...
class Application extends ActiveRecord\Model
{
static $has_many = array(
array('application_subjects'),
array('subjects', 'through' => 'application_subjects')
);
}
class Subject extends ActiveRecord\Model
{
static $has_many = array(
array('application_subjects'),
array('applications', 'through'=> 'application_subjects')
);
}
class ApplicationSubject extends ActiveRecord\Model
{
static $belongs_to = array(
array('application'),
array('subject')
);
}