1

我被困住了,阅读文档似乎并没有帮助我。

我有一对多的关系,尽管我确信有一种更简单的方法,但当涉及到多对多的关系时,我被卡住了。

我已经尝试了很多东西,但基本上我有三个表:

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
4

1 回答 1

1

您可以在保存时将一个对象与另一个对象关联起来。

您可以多次这样做:

$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);
于 2012-04-03T06:42:27.430 回答