I'm working on a symfony1.4 project, with propel ORM. In my model, I have a table where its elements can be linked with many elements of the same table, by another table (link table). Some code will explain better:
table1:
id_element: integer;
[...]
and the link table:
link_table:
id1: fk to table1;
id2: fk to table1;
I need to build a query with Propel Criteria that returns me all the related elements with an specific element. The problem is that the element I want to specify, can be as in id1 field as in id2 field of the link table.
now some of my criteria definition code (not working obviously)
$c = new Criteria();
$c->addJoin($linkTable::ID1,$table::ID);
$c->addJoin($linkTable::ID2,$table::ID);
$c->addOr($linkTable::ID1,$specific_id);
$c->addOr($linkTable::ID2,$specific_id);
$result = $table->doSelect($c);
and this is a SQL like that I want to generate:
SELECT * FROM table
WHERE table.ID IN
(SELECT link_table.ID1 FROM link_table
WHERE link_table.ID2 = "the id that I want"
)
OR table.ID IN
(SELECT link_table.ID2 FROM link_table
WHERE link_table.ID1 = "the id that I want"
)
So must I do 2 joins, one for each side of the link table? is there a way to do an "or-join
"?
at last i found a way to do it using criteria:
$c = new Criteria();
$q1 = new Criteria();
$q1->add($linkPeer::ID1,$my_value);
result1 = $linkPeer->doSelect($q1);
foreach($result1 as $result){
ids1[] = $result->getID();
}
$q2 = new Criteria();
$q2->add($linkPeer::ID2,$my_value);
result2 = $linkPeer->doSelect($q2);
foreach($result2 as $result){
ids2[] = $result->getID();
}
$ids = array_merge($ids1,$ids2);
$c->add($tablePeer::ID,$ids,Criteria::IN);
$totalResult = $tablePeer->doSelect($c);
maybe not the best way but working fine.
Thank you very much for your answers!!