Default scope relation problems

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)

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.

Anyone got any answers to this?

Or has anyone found a good way to implement a global deleted scope or similar.

tableName is static too. try:


static::tableName()

Works perfect, thanks.