Search code examples
phpjoinsymfony1aliaspropel

How to add alias in propel query


In my symfony 1.4 project I have query with two joins to the same table (different foreign keys):

return ArticleQuery::create(null,$criteria)
        ->joinWithArticleCategoryRelatedByNewsCategoryId()
        ->joinWithArticleCategoryRelatedByHelpCategoryId();

I'm getting error: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'article_category'

How can I add alias to this join?


Solution

  • Note: I use Propel 1.6

    It should be enough to pass an argument to the join method.

    return ArticleQuery::create(null,$criteria)
            ->joinWithArticleCategoryRelatedByNewsCategoryId('news')
            ->joinWithArticleCategoryRelatedByHelpCategoryId('help');
    

    Look in your generated BaseArticleQuery.php to see what methods have been generated for you. One of my join methods look like this:

    /**
     * Adds a JOIN clause to the query using the ArticleKeyword relation
     *
     * @param     string $relationAlias optional alias for the relation
     * @param     string $joinType Accepted values are null, 'left join', 'right join', 'inner join'
     *
     * @return    KeywordQuery The current query, for fluid interface
     */
    public function joinArticleKeyword($relationAlias = null, $joinType = Criteria::LEFT_JOIN)
    

    One of the strengths of Propel, is that nearly everything has a concrete method. So if you have an IDE with support for code completion, you can receive hints of what arguments the methods support.