我被卡住了,阅读文档似乎对我没有帮助。
我有一对多的关系,尽管我相信有一种更简单的方法,但当涉及到多对多关系时,我陷入了困境。
我试过很多东西,但基本上我有三张表:
homes:
- id
- address_id
- price
- etc
features:
- id
- name
features_homes
- id
- feature_id
- home_id
我已经用很多不同的方式设置了我的模型。
var $has_many = array(
'feature' => array(
'class' => 'feature',
'other_field' => 'home',
'join_self_as' => 'home',
'join_other_as' => 'feature',
'join_table' => 'features_homes'
)
);
以及的基本版本
var $has_many = array('feature');
问题是我不知道如何获得与该住宅相关的多个记录。
我最终得到的是一个项目,而不是一组项目。
示例输出:
[id] => 1
[address_id] => 1
[latlong] => 00.000, -00.00000
...
[feature_id] => 1
[feature_name] => Hard Wood Floors
我正在尝试获得
[id] => 1
[address_id] => 1
[latlong] => 00.000, -00.00000
...
[features] => // Object of all items
保存时通过链接将一个对象与另一个对象相关联。
你可以多次这样做:
$parent = new Parent(1);
$childA = new Child(1);
$childB = new Child(2);
$parent->save($childA); $parent->save($childB);
或者通过传递数组:
$parent = new Parent(1);
$children = array(
new Child(1),
new Child(2),
);
$parent->save($children);