为了使用这种多对多关联,您需要定义第 3 个模型并使用has_many
through
其中一个模型中的语法:
class Home extends ActiveRecord\Model {
static $has_many = array(
array('home_furniture_bridge', 'class_name' => 'HomeFurnitureBridge'),
array('furniture', 'class_name' => 'Furniture', 'through' => 'home_furniture_bridge')
);
}
class HomeFurnitureBridge extends ActiveRecord\Model {
static $belongs_to = array(
array('home'),
array('furniture', 'class_name' => 'Furniture')
);
}
class Furniture extends ActiveRecord\Model {
static $has_many = array(
array('homes')
);
}
请注意,您必须为home_furniture_bridge
and使用类名furniture
,因为 php AR 使用表和类名的复数/单数约定。