Hi,
$this->getTableAlias(false, false); in default scope returns t for the all the models. whereas different models as different alias like c etc, Any hellp why this problem is caused.
Thanks,
Praveen J
Hi,
$this->getTableAlias(false, false); in default scope returns t for the all the models. whereas different models as different alias like c etc, Any hellp why this problem is caused.
Thanks,
Praveen J
I also have the same problem!
Any CActiveRecord will default the alias to ‘t’, it is intended this way I believe. You can always change it with:
$model->tableAlias='differentAlias';
I’m not sure why you would want to do that though.
Hi ksangers,
how do i place the same in default scope, i tried setTableAlias, its also defaulting to t. Problem is when fetching from single table it defalut scope works fyn without alias, but when fetching using joins i need to change the alias, either set or get but both defaults to t. So how i resolve this.
Thanks,
Praveen J.
I’m not sure I follow, could you show me when your issue occurs? A bit of code will probably make this alot clearer
Hi ksangers,
Ok sorry for the typos,
This is my default scope for user table
return array(
'condition' => 'u.active = "Y"'
);
now when i execute a cdbcriteria like
$testCriteria = new CDbCriteria(array(
'join' => 'left join department d on u.user_id = d.user_id'
'condition' => 'd.active = "Y"'
'alias' => 'u'
));
$model = Users::model()->find($testCriteria);
Works fine.
Now when i try
$testCriteria = new CDbCriteria(array(
'condition' => 'user_id = 1'
));
$model = Users::model()->find($testCriteria); show error saying u.user_id is unknown column,
if i do $this->setTableAlias = ‘u’; in default scope it wont work. because alias is defaulted to ‘t’ .
Hope i am clear this time… any idea…
Thanks,
Praveen J
You could add the following line to the defaultScope() method:
$this->tableAlias = 'u';
I don’t believe that is what you want though. Did you make CActiveRecord models of all tables? If so, I would keep the defaultScope more like this:
public function defaultScope() {
return array(
'condition'=>'[t].[active] = "Y"',
);
}
Now to get the records you need:
$firstCriteria = new CDbCriteria(array(
'with'=>'departments',
'condition'=>'[departments].[user_id] = [t].[user_id] AND [departments].[active] = "Y"',
));
$model = Users::model()->find($firstCriteria);
$secondCriteria = new CDbCriteria(array(
'condition'=>'[t].[user_id] = 1',
));
$model = Users::model()->find($secondCriteria);
I hope this helps out
Hi ksangers,
Thanks for the reply. Your first solution $this->tableAlias = ‘u’; was the thing i was talking about. Thats what i used for the default scope and yeah have created CActiveRecord models for all the tables. Now when i use
public function defaultScope() {
$this->tableAlias = ‘u’;
return array(
'condition'=>' u.active = "Y"',
);
}
Instead of using [t], because i have already used users table around many places with alias ‘u’. It shows me an sql error
– select * from users ‘t’ where u.active = ‘y’ – see even though i specified ‘u’ as the alias its still taking ‘t’…
Sorry for the all the confusion… Hope now its clear…
In that case you might want to set the tableAlias via init(), add the following to your Users class:
public function init() {
$this->setTableAlias('u');
}
This will cause every instance of that class to have ‘u’ as the alias I believe.
Hi ksangers,
its still taking t for the model…
Have added this function
public function init() {
$this->setTableAlias('u');
}
in users model class…
Thanks,
Praveen J
I don’t think that should be possible, I’ve even tried a little application with
'alias'=>'u'
,
$this->tableAlias='u'
and
init(){$this->setTableAlias('u');}
and it worked. There might be something else going wrong with your code.
Hi ksangers,
Thank you for all the inputs… Let me check the code and try to resolve the code…
Thanks,
Praveen J