为什么这个密码坏了?这应该行得通,但不行。
我有三张桌子:
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'
最佳答案
似乎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')
);
}