Yii Alias Table

Hi All,

I’m not sure what is suitable title for this thread. but I think the problem is about Alias Table in ActiveRecord

So here is the problem (I Attached the screenhoot Error1.png)

my source code is like this:




        public function findRecentPosts($limit=10)

        {

          $criteria=array(

                          'condition'=>'Post.status='.self::STATUS_PUBLISHED,

                          'order'=>'Post.createTime DESC',

                          'limit'=>$limit,

                          );

          return $this->findAll($criteria);

        }



I have tried to change all of Post.{fieldname} into t.{fieldname}, therefore become like this, and it works:




        public function findRecentPosts($limit=10)

        {

          $criteria=array(

                          'condition'=>'t.status='.self::STATUS_PUBLISHED,

                          'order'=>'t.createTime DESC',

                          'limit'=>$limit,

                          );

          return $this->findAll($criteria);

        }



BUT, when I work with multiple models that involving another Model that has same field I getting the same error. therefore changing table name into "t" is not resolving all problems, even emerging another problem.

how can I fix this?

Thanks in advance :slight_smile:

You didn’t say how you work with related tables, but getTableAlias() will probably help you:




$t=$this->getTableAlias();

$criteria->condition="$t.status=".self::STATUS_PUBLISHED;

Thanks Mike, for your reply, I just know about getTableAlias() :-).

Here I include relations as you said:




	public function relations()

	{

		return array(

			'author'=>array(self::BELONGS_TO, 'User', 'authorId'),

			'comments'=>array(self::HAS_MANY, 'Comment', 'postId', 'order'=>'??.createTime'),

			'tagFilter'=>array(self::MANY_MANY, 'Tag', 'PostTag(postId, tagId)',

				'together'=>true,

				'joinType'=>'INNER JOIN',

				'condition'=>'??.name=:tag'),

		);

	}



I also wondering about that relations (actually this is not my code), in that relation there are


??

, is this right? and how its use?

in relations you need to set alias parameter see in here

The default alias is the name of your relation. So in your case instead of ‘??’ use ‘tagFilter’. If i remember well, ‘??’ was the alias used in Yii 1.0.x.