Search code examples
sqlormpropel

Complex clause in SQL JOIN in Propel 1.6


I have the following SQL statement:

SELECT * FROM image
LEFT JOIN history
ON (image.id = history.image_id
  AND history.user_id = 1
WHERE  active = 1

The purpose of the query is to find ALL the records from the Image table (with flag 'active') and ONLY those records from the History table, that refer to the current user. There in a n-to-m relationship between the tables and there could be records with the same image_id, but for other user_id. I do not want this records to be included.

I am trying to implement it using Propel 1.6 so I thought the code should be like this:

ImageQuery::create()
->filterByActive( 1 )
->leftJoinHistory('History.Image')
->addJoinCondition('History.Image', 'History.user_id = ?', 1)
->find();

but it fails

Cannot determine the column to bind to the parameter in clause 'History.user_id = ?'

Where is the problem?


Solution

  • Try:

    ImageQuery::create()
    ->filterByActive( 1 )
    ->leftJoinHistory()
    ->addJoinCondition('History', 'History.user_id = ?', 1)
    ->find();
    

    I don't know why do you try to join something like History.Image since the relation seems to be Image.History instead.