0

I am a beginner. Here I would to ask all of you that how can I save many to many relationship in to the three tables(table A , table A-b , table B)? Now I am trying to save a new record with new ID into table A, and I have some IDs of table B that I want to save them to the middle table A-B depend on ID of table A. If anyone have experiences with this, please kindly share. Example:

$a = new modelA();
$a->name = ‘new name’;
$a->des = ‘something to say’;

$b = new modelB();
$IDs = new array(1,2,3); //IDs of records in table B


$a->save(array($IDs=>$b));
chantheoun
  • 31
  • 3

2 Answers2

1

Passing ID's is not supported, Datamapper needs objects to be able to relate.

If you have an array of ID's, you can fetch the objects using an where_in() query, and then save the relation using

$a->save($b->all);
WanWizard
  • 2,574
  • 15
  • 13
  • I just want to save new record into table A and save new record's ID of table A with many IDs of table B into the middle table(table A-B) because table A has many to many relationship with table B. – chantheoun Apr 07 '12 at 09:06
0

I have some values which get from post form like:

  • 'new name'
  • 'new description'
  • array('val1','val2','val3')

in my controller I want to save a new record to table A with: - 'new name' - 'new description' and array('val1','val2','val2') I got IDs of them from table B which has many to many with table A(a new record going to save).

So when I save into table A, the IDs of table B also save to middle table A-B with new ID of table A I just save. How to save?

chantheoun
  • 31
  • 3