winnfield
winnfield

Reputation: 273

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?

Upvotes: 2

Views: 3626

Answers (2)

Vitalii Zurian
Vitalii Zurian

Reputation: 17976

This worked for me:

return ArticleQuery::create()
    ->joinWith('ArticleCategoryRelatedByNewsCategoryId a', Criteria::LEFT_JOIN)
    ->joinWith('ArticleCategoryRelatedByHelpCategoryId b', Criteria::LEFT_JOIN);

In other case - you should try to upgrade your Propel ;)

Upvotes: 3

CheeseSucker
CheeseSucker

Reputation: 1290

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.

Upvotes: 2

Related Questions