trance
(Tranceorder)
November 8, 2016, 9:44am
1
I am trying to write this query using AR:
AND (document.datum_izvora = document_content.editDate)
I have tried this:
$this->_query->andFilterWhere(['document.datum_izvora => document_content.editDate'])
but it is not doing what I want since it is quoting second parameter as value.
My colleague has tried this dirty solution, and I would like to know if there is any better ?
$this->_query->andFilterWhere(['and', 'document.datum_izvora = document_content.editDate', '1=1']);
softark
(Softark)
November 8, 2016, 1:40pm
2
Use "andWhere" in place of "andFilterWhere" here, because the latter "Adds an additional WHERE condition to the existing one but ignores empty operands."
http://www.yiiframework.com/doc-2.0/yii-db-querytrait.html#andFilterWhere()-detail
A string as the condition might be the simplest solution.
$this->_query->andWhere('document.datum_izvora = document_content.editDate')
trance
(Tranceorder)
November 9, 2016, 10:17am
3
Thank you. I have one question though. What "ignores empty operands" really means ? Yii will not execute that condition if value that is passed is empty ?
softark
(Softark)
November 10, 2016, 8:24am
4
Yes.
This is not a precise explanation, but roughly speaking,
$query->andFilterWhere(['some_column' => $this->some_column]);
can be considered as
if ($this->some_column != "") {
$query->andWhere(['some_column' => $this->some_column]);
}
Please check the source code of yii\db\QueryTrait::andFilterWhere() and yii\db\QueryTrait::filterCondition()
$this->where($condition);
}
return $this;
}
/**
* Adds an additional WHERE condition to the existing one but ignores [[isEmpty()|empty operands]].
* The new condition and the existing one will be joined using the 'AND' operator.
*
* This method is similar to [[andWhere()]]. The main difference is that this method will
* remove [[isEmpty()|empty query operands]]. As a result, this method is best suited
* for building query conditions based on filter values entered by users.
*
* @param array $condition the new WHERE condition. Please refer to [[where()]]
* on how to specify this parameter.
* @return $this the query object itself
* @see filterWhere()
* @see orFilterWhere()
*/
public function andFilterWhere(array $condition)