How to join two tables in Propel that don't have a foreign key defined between them?

411 Views Asked by At

I have a project that uses Propel 2 and there are two tables that I want to join on a certain field, but there is no foreign-key relationship there. I just want to match them up based on that value. Unfortunately I cannot get it to work, and Propel's documentation, thorough as it tries to be, still lacks a good reference. How can I do it?

What I have tried:

echo Table1Query::create()
        ->addJoin(Table1TableMap::COL_A, Table2TableMap::COL_B)
        ->where('Table2.C=?', 'asd')
        ->toString();

This fails at the where() part with System error Cannot determine the column to bind to the parameter in clause "Table2.C=?".

I know I can get the PDO connection and drop back into pure SQL, which would definitely be easier, but it kinda feels wrong when using an ORM (then again...).

1

There are 1 best solutions below

0
On

You need to do an explicit join type since you have not defined the relation in your schema, e.g.:

->addJoin(Table1TableMap::COL_A, Table2TableMap::COL_B, Criteria::JOIN_TYPE)
// INNER_JOIN, INNER_JOIN, etc