JamesBarnsley
(Jamesbarnsleyfreelance)
February 3, 2015, 2:01pm
1
Hi,
I have defined a default scope as follows …
public static function find() {
return parent::find()->where(["deleted" => 0]);
}
The problem is when I come to use it in relational searches like this …
$query->joinWith(["project"]);
$query->andFilterWhere(["like", "id", $this->id])
->andFilterWhere(["like", "name", $this->name])
It throws the following error because the deleted is ambiguous, how can I get around this …
SQLSTATE[23000]: Integrity constraint violation: 1052 Column ‘deleted’ in where clause is ambiguous
The SQL being executed was: SELECT COUNT(*) FROM milestone
LEFT JOIN project
ON milestone
.fkIDWithProjectID
= project
.id
WHERE ((deleted
=0) AND (project
.name
LIKE ‘%Test%’)) AND (deleted
=0)
lukos
(Lukiebriner)
February 3, 2015, 2:06pm
2
Only a guess but can’t you use the table name in the find function:
return parent::find()->where(["project.deleted" => 0]);
Or whatever the syntax is for MySql
JamesBarnsley
(Jamesbarnsleyfreelance)
February 3, 2015, 2:45pm
3
Lukos:
Only a guess but can’t you use the table name in the find function:
return parent::find()->where(["project.deleted" => 0]);
Or whatever the syntax is for MySql
Hi Lukos,
Yes that would have been nice.
But the global scope is defined in a global modal from which all other models extend from.
I tried doing $this->tableName() to get table of child to append to query however …
I cannot use $this either as I have to declare it in a static function, as find() is a static function and find() is the method I am overriding.
JamesBarnsley
(Jamesbarnsleyfreelance)
February 7, 2015, 9:15pm
4
Anyone got any answers to this?
Or has anyone found a good way to implement a global deleted scope or similar.
Pasman
(Pasman P)
February 8, 2015, 10:00am
5
JamesBarnsley:
Hi Lukos,
Yes that would have been nice.
But the global scope is defined in a global modal from which all other models extend from.
I tried doing $this->tableName() to get table of child to append to query however …
I cannot use $this either as I have to declare it in a static function, as find() is a static function and find() is the method I am overriding.
tableName is static too. try:
static::tableName()