关于产品数据要两次查询才能获取有没有可优化的空间?
/**
* 初始化数据包括
* 主键值:$this->_primaryVal
* 当前产品对象:$this->_product
* Meta keywords , Meta Description等信息的设置。
* Title的设置。
* 根据当前产品的attr_group(属性组信息)重新给Product Model增加相应的属性组信息
* 然后,重新获取当前产品对象:$this->_product,此时加入了配置中属性组对应的属性。
*/
protected function initProduct()
{
$primaryKey = Yii::$service->product->getPrimaryKey();
$primaryVal = Yii::$app->request->get($primaryKey);
$this->_primaryVal = $primaryVal;
$product = Yii::$service->product->getByPrimaryKey($primaryVal);
if ($product) {
$enableStatus = Yii::$service->product->getEnableStatus();
if ($product['status'] != $enableStatus){
return false;
}
} else {
return false;
}
$this->_product = $product;
Yii::$app->view->registerMetaTag([
'name' => 'keywords',
'content' => Yii::$service->store->getStoreAttrVal($product['meta_keywords'], 'meta_keywords'),
]);
Yii::$app->view->registerMetaTag([
'name' => 'description',
'content' => Yii::$service->store->getStoreAttrVal($product['meta_description'], 'meta_description'),
]);
$this->_title = Yii::$service->store->getStoreAttrVal($product['meta_title'], 'meta_title');
$name = Yii::$service->store->getStoreAttrVal($product['name'], 'name');
//$this->breadcrumbs($name);
$this->_title = $this->_title ? $this->_title : $name;
Yii::$app->view->title = $this->_title;
//$this->_where = $this->initWhere();
// 通过上面查询的属性组,得到属性组对应的属性列表
// 然后重新查询产品
$attr_group = $this->_product['attr_group'];
Yii::$service->product->addGroupAttrs($attr_group);
// 重新查询产品信息。
$product = Yii::$service->product->getByPrimaryKey($primaryVal);
$this->_product = $product;
return true;
}