I have a model with its related search model class:
class BlogPostSearch extends BlogPost
{
public function search($params)
{
$query = BlogPost::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->joinWith('authorK');
$query->join('INNER JOIN', Profile::tableName()." editor", self::tableName().'.editor_id = editor.profile_id');
The author and the editor are the same model, and obviously same table.
So if i join both without table alias i get an error (obviously).
But if i use an alias i can’t use the default conditions i put in the ProfileQuery class (i need to add the table name cause there are multiple tables with the “status” field
class ProfileQuery extends \yii\db\ActiveQuery
{
public function init(){
$this->andOnCondition(['not',[Profile::tableName().'.status'=>2]]);
parent::init();
}
With the join for editor in place of joinWith it works.
Anyway, in the GridView i need to list the blogposts showing the author’s name and the editor’s name.
I do it like this
The problem is for EVERY record showed in GridView a different query is executed to retrieve the name. (i can see it in the debugger).
How can i prevent that?
Unfortunately, if i’m not wrong, we won’ be able to get the current alias used in an ActiveQuery in this Yii version (should be available in yii3)
Yes, profileK (actually editorK, there was an error in my code) is that relation.
The problem is with an alias i can’t set some default where conditions in the ProfileQuery. (see above code, ProfileQuery part) cause the where condition it adds on init is added with the profile table name. I could remove the Profile::tableName() part but i would get another error (since i’m joining multiple times the same table and the status column is ambiguous)