给一个属性赋值 null 值不起作用,因为它没有被 Yii 记录:属性的初始值为 null 并且 Yii 不跟踪修改的属性(这有点烦人)。
这里的工作是像这样使用它:
$productSearch = clone Product::model();
$productSearch->Category = array(null);
$products = $productSearch->search()->getData();
请注意,如果您想搜索更高级Category IS NULL OR Category IN (1, 2, 3)
的预期方法:
$productSearch->Category = array(null, 1, 2, 3);
由于 Yii 盲目地将其全部放入单个 IN 语句中,因此无法正常工作:
Category IN (NULL, 1, 2, 3)
这里的工作更复杂,因为它需要在模型搜索方法中使用额外的代码:
public function search()
{
$criteria = new CDbCriteria;
// Work around of inability of Yii to handle IS NULL OR IN () conditions
if (is_array($this->Category) && count($this->Category) > 1)
{
$hasNull = false;
$values = array();
foreach ($this->Category as $value)
{
if (is_null($value))
{
$hasNull = true;
}
else
{
array_push($values, $value);
}
}
$condition = array();
if ($hasNull) array_push($condition, 'Category IS NULL');
if (count($values)) array_push($condition, "Category IN ('" . implode("', '", $values) . "')");
$criteria->addCondition(implode(' OR ', $condition));
}
else
{
$criteria->compare('Category', $this->Category);
}
// other search criterias ...
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}