简体   繁体   中英

Many to Many DataMapper CodeIgniter

I'm stuck and reading the documentation doesn't seem to be helping me out.

I have one to many relationships working, all though I'm sure there is an easier way, I'm stuck when it comes to Many to Many relationships.

I've tried quite a few things, but basically I have three tables:

homes:
- id
- address_id
- price
- etc

features:
- id
- name

features_homes
- id
- feature_id
- home_id

I've setup my model quite a few different ways.

var $has_many = array(
    'feature' => array(
        'class' => 'feature',
        'other_field' => 'home',
        'join_self_as' => 'home',
        'join_other_as' => 'feature',
        'join_table' => 'features_homes'
    )
);

And the basic version of

var $has_many = array('feature');

The problem is I can't figure out how to get multiple records associated with the home.

I end up with a single item rather than a group of items.

Example output:

[id] => 1
[address_id] => 1
[latlong] => 00.000, -00.00000
...
[feature_id] => 1
[feature_name] => Hard Wood Floors

I am trying to get

[id] => 1
[address_id] => 1
[latlong] => 00.000, -00.00000
...
[features] => // Object of all items

You associate one object to another by linking them when you save.

You can do that multiple times:

$parent = new Parent(1);
$childA = new Child(1);
$childB = new Child(2);

$parent->save($childA); $parent->save($childB);

Or by passing an array:

$parent = new Parent(1);
$children = array(
    new Child(1),
    new Child(2),
);

$parent->save($children);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM