Update many-to-many relationship in both directions after adding

Showing 1-5 of 6 messages
Update many-to-many relationship in both directions after adding Jan Walther 3/28/13 8:55 AM
Hi,

although I have a many-to-many relationship that has iscrossref="true" I have the following problem:
Here are my tables: table1, table2 and the join table table1_table2

I want to link them together this way:
$table1Object = new Table1();
$table2Object = new Table2();
$table1Object->addTable2Object($table2Object);

Now only $table1Object knows of the relationship, $table2Object not.
When I do
var_dump($table1Object->countTable2Objects()); // result: 1
var_dump($table2Object->countTable1Objects()); // result: 0

What am I doing wrong?
Re: [propel] Update many-to-many relationship in both directions after adding Marius Ghita 3/28/13 9:12 AM
You're not doing anything wrong, but looking at the code, it just doesn't add the reverse relation. You can have it after save though.

The generated code should look something like this.

public function addTable2Object($Table2Object)
  {
    if ($this->collTable2Objects === null) {
      $this->initTable2Objects();
    }
    if (!$this->collTable2Objects->contains($Table2Object)) { // only add it if the **same** object is not already associated
      $table1Table2 = new Table1Table2Object();
      $table1Table2->setTable2Object($Table2Object);
      $this->addTable1Table2Object($tabl1Table2);

      $this->collTable2Objects[]= $Table2Object;
    }
  }


You could write a test case for your scenario, and place an issue on the github repo. Maybe have a try at it, because a call on table2Object->addTable1 should take place there.

But if you do the unit test and a pull, maybe I'll have a take and try to fix this issue.



--
You received this message because you are subscribed to the Google Groups "Propel Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to propel-users...@googlegroups.com.
To post to this group, send email to propel...@googlegroups.com.
Visit this group at http://groups.google.com/group/propel-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Google+: https://plus.google.com/111881868112036203454
Re: Update many-to-many relationship in both directions after adding Jan Walther 3/30/13 1:27 AM
Hello VinceW,

everything works fine after saving. So the table1_table2 record is created correctly and foreign keys are also correct.

I think Marius is right as Propel in fact does not add the relation to the added object ($table2Object).
I will try to write a test for that. Never done that before but sometimes is always the first time ;-)

Lastly, thanks for the link but this article also only handles the relation after saving.

Regards,
Jan

Am Donnerstag, 28. März 2013 17:34:31 UTC+1 schrieb vin...@barttool.com:
Hello Jan,

Did you check if your var_dump is showing false positives?
In other words, was the record actually created in table1_table2?
Or is var_dump showing the $table1Object you've made earlier?

Are the foreign keys declared properly?

Perhaps you find something to solve your issue this link: http://propel.posterous.com/many-to-many-relationships-check

Best,
VinceW

Op donderdag 28 maart 2013 16:55:07 UTC+1 schreef Jan Walther het volgende:
Hi,

although I have a many-to-many relationship that has iscrossref="true" I have the following problem:
Here are my tables: table1, table2 and the join table table1_table2

I want to link them together this way:
$table1Object = new Table1();
$table2Object = new Table2();
$table1Object->addTable2Object($table2Object);

Now only $table1Object knows of the relationship, $table2Object not.
When I do
var_dump($table1Object->countTable2Objects()); // result: 1
var_dump($table2Object->countTable1Objects()); // result: 0

What am I doing wrong?
Re: [propel] Re: Update many-to-many relationship in both directions after adding Marius Ghita 3/30/13 6:07 AM
Great, if you'll write the test; this would be the file in which I'd recommend you add it test/testsuite/generator/builder/om/GeneratedObjectRelTest.php

Regards


--
You received this message because you are subscribed to the Google Groups "Propel Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to propel-users...@googlegroups.com.
To post to this group, send email to propel...@googlegroups.com.
Visit this group at http://groups.google.com/group/propel-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Google+: https://plus.google.com/111881868112036203454
Re: [propel] Re: Update many-to-many relationship in both directions after adding Jan Walther 3/31/13 4:11 AM
Now I edited the file you mentioned and opened a pull request. Hopefully I have done this correctly...
see https://github.com/propelorm/Propel/pull/642

Regards,
Jan