Search code examples
propel

"Call to a member function mergeWith() on a non-object" when using related queries


I'm having this situation with Propel 1.6 and a MySQL database:

$query->usePublicationQuery("pq");
$query->condition('c1', '(YEAR(`pq.PUBLISHED_DATE`)) = ?', "2013")
        ->condition('c2', '(MONTH(`pq.PUBLISHED_DATE`)) = ?', "03")
        ->condition('c3', '(DAY(`pq.PUBLISHED_DATE`)) = ?', "01")
        ->combine(array('c1', 'c2','c3'), 'and', 'c123');
$query->endUse();

The mergeWith()-error appears upon invocation of the endUse()-method. It is thrown when Propel tries to merge the queries like this in ModelCriteria.php:

$primaryCriteria->mergeWith($this); // (line 941)

$primaryCriteria seems to be null. Can anyone tell me, when and why this can possibly happen?


Solution

  • The caveat is that Propel's useQuery() and endUse() methods RETURN new query rather that change current one. So, either rewrite whole query using method chaining:

    $query
        ->usePublicationQuery("pq")
            ->condition('c1', '(YEAR(`pq.PUBLISHED_DATE`)) = ?', "2013")
            ->condition('c2', '(MONTH(`pq.PUBLISHED_DATE`)) = ?', "03")
            ->condition('c3', '(DAY(`pq.PUBLISHED_DATE`)) = ?', "01")
            ->combine(array('c1', 'c2','c3'), 'and', 'c123')
        ->endUse();
    

    ...or something like this:

    $query2 = $query->usePublicationQuery("pq");
    
    $query2->condition('c1', '(YEAR(`pq.PUBLISHED_DATE`)) = ?', "2013")
            ->condition('c2', '(MONTH(`pq.PUBLISHED_DATE`)) = ?', "03")
            ->condition('c3', '(DAY(`pq.PUBLISHED_DATE`)) = ?', "01")
            ->combine(array('c1', 'c2','c3'), 'and', 'c123');
    
    $query = $query2->endUse();