如何在 Yii2 中过滤掉具有空属性的模型
How to filter out models with null properties in Yii2
在我的应用程序中,我有具有相关翻译(不同语言的描述)、配件(产品列表)和最后的价格实体的产品实体。有欧元价目表、美元价目表、澳元价目表和正确的价格条目是通过使用当前价目表 ID 检索的。
class Product extends \yii\db\ActiveRecord
{
...
public function getPrice()
{
$supplier = Supplier::getCurrent();
return $this->hasOne(ProductPrice::className(), ['IDProduct' => 'IDProduct'])
->onCondition(['PriceListID' => ... getCurrentPricelistID()]);
}
...
}
下面的查询检索了一组包含价格、配件和翻译属性的产品条目。
$query = Product::find()
->where($conditions)
->with('translation', 'accessories', 'price')
->asArray()
->all();
我需要过滤掉价格为空的产品条目。因此一些价目表(例如澳大利亚的价目表)的产品数量会少于其他价目表,因为有些产品不在该国家/地区销售。我该怎么做?
试试这个 where 子句:
$query = Product::find()
->where($conditions)
->with('translation', 'accessories', 'price')
->where(['not', ['price' => null]])
->asArray()
->all();
我最终使用 innerJoinWith
的价格 property/relation:
$query = Product::find()
->with('translation', 'accessories')
->innerJoinWith('price')
->asArray()
->all();
在我的应用程序中,我有具有相关翻译(不同语言的描述)、配件(产品列表)和最后的价格实体的产品实体。有欧元价目表、美元价目表、澳元价目表和正确的价格条目是通过使用当前价目表 ID 检索的。
class Product extends \yii\db\ActiveRecord
{
...
public function getPrice()
{
$supplier = Supplier::getCurrent();
return $this->hasOne(ProductPrice::className(), ['IDProduct' => 'IDProduct'])
->onCondition(['PriceListID' => ... getCurrentPricelistID()]);
}
...
}
下面的查询检索了一组包含价格、配件和翻译属性的产品条目。
$query = Product::find()
->where($conditions)
->with('translation', 'accessories', 'price')
->asArray()
->all();
我需要过滤掉价格为空的产品条目。因此一些价目表(例如澳大利亚的价目表)的产品数量会少于其他价目表,因为有些产品不在该国家/地区销售。我该怎么做?
试试这个 where 子句:
$query = Product::find()
->where($conditions)
->with('translation', 'accessories', 'price')
->where(['not', ['price' => null]])
->asArray()
->all();
我最终使用 innerJoinWith
的价格 property/relation:
$query = Product::find()
->with('translation', 'accessories')
->innerJoinWith('price')
->asArray()
->all();