Table alias for related tables.

I have three table models.

Parent model ofcourse have alias ‘t’.

But when I’m trying to get $this->tableAlias in second table model relations() function I also get alias ‘t’, instead of this I should get ‘sm’ (same as relation name).




return array(

  'link' => array(

    self::BELONGS_TO, 

    'SmLinkModel', 

    'child_guid_fk', 

    'on' => "{$this->tableAlias}.sm_type = video",

  ),

);



But if I’m trying to get tableAlias in scopes() function, I get right alias.

Hi!

Same problem here (Yii 1.1.6).

I noticed that by working with LOCK TABLES.

$model1 = new model1();

$model2 = new model2();

echo $model1->TableAlias; //returns ‘t’

echo $model2->TableAlias; //returns also ‘t’

Using "LOCK TABLES" at mysql you have to specify an alias, if the statement is also using table aliases. (what active records do)

That means: to lock tables for two different models like model1 and model2 you have to write something like that:

Yii::app()->dbWrite->createCommand("LOCK TABLES model1 as ".$model1->TableAlias." WRITE, model2 as ".$model1->TableAlias." WRITE")

wich results in an error “ERROR 1066 (42000): Not unique table/alias: ‘t’”

The Workaround is to set the table aliases explicit:

$model1->SetTableAlias(‘t1’);

$model2->SetTableAlias(‘t2’);

(followed by)

Yii::app()->dbWrite->createCommand("LOCK TABLES model1 as ".$model1->TableAlias." WRITE, model2 as ".$model1->TableAlias." WRITE")

//Edit:

it looks like $model1->count(…) does not notice $model1->SetTableAlias(‘t1’);

and results in "SELECT COUNT(*) FROM model1 t WHERE …"

…witch results in "General error: 1100 Table ‘t’ was not locked with LOCK TABLES "

Thanks, rall0r