如何使用NULL搜索条件在Yii中搜索记录

Ale*_*ski 3 php model yii

假设我有具有Category属性的Product model,我想使用search()函数拉出所有类别为nu​​ll的产品.

$productSearch = clone Product::model();
$productSearch->Category = null;

$products = $productSearch->search()->getData();
Run Code Online (Sandbox Code Playgroud)

通过检查生成的SQL我发现它不起作用,查询中根本没有提到类别.最好的方法是什么?

另外如何搜索具有某些属性设置为NULL或某些值的记录

Ser*_*gey 5

第一个变种:

// use search scenario, not clone model with metadata
$productSearch = new Product('search'); 
$productSearch->unsetAttributes();

// get CActiveDataProvider
$dataProvider = $productSearch->search();
// Add null condition
$dataProvider->criteria->addCondition('Category is null');
// Get data - first 10 rows
$products = $dataProvider->getData();
Run Code Online (Sandbox Code Playgroud)

第二个变种(优先):

$products = Product::model()->findAll('Category is null');
Run Code Online (Sandbox Code Playgroud)