/** * @param CHttpRequest $request * @return array */ public function getTypeAttributesForSearchFromQuery(CHttpRequest $request) { $attributes = Yii::app()->getCache()->get('Store::filter::attributes'); if (false === $attributes) { $attributes = []; $models = Attribute::model()->findAll(['select' => ['name', 'id', 'type']]); foreach ($models as $model) { $attributes[$model->name] = $model; } Yii::app()->getCache()->set('Store::filter::attributes', $attributes); } $result = []; $attributeValue = new AttributeValue(); foreach ($attributes as $name => $attribute) { $searchParams = $request->getQuery($attribute->name); //пропускаем пустые значения if (null === $searchParams) { continue; } if (is_array($searchParams)) { if (isset($searchParams['from']) && null == $searchParams['from']) { unset($searchParams['from']); } if (isset($searchParams['to']) && null == $searchParams['to']) { unset($searchParams['to']); } if (empty($searchParams)) { continue; } } $result[$attribute->name] = ['value' => $searchParams, 'attribute_id' => (int) $attribute->id, 'column' => $attributeValue->column($attribute->type)]; } return $result; }
/** * @param $currentType * @param $newType * @return bool */ public function changeType($currentType, $newType) { if ($currentType == $newType) { return true; } $value = new AttributeValue(); $currentCol = $value->column($currentType); $newCol = $value->column($newType); if ($currentCol == $newCol) { return true; } $transaction = Yii::app()->getDb()->beginTransaction(); try { Yii::app()->getDb()->createCommand(sprintf('UPDATE {{store_product_attribute_value}} SET %s = %s WHERE attribute_id = :id', $newCol, $currentCol))->bindValue(':id', $this->id)->execute(); Yii::app()->getDb()->createCommand(sprintf('UPDATE {{store_product_attribute_value}} SET %s = null WHERE attribute_id = :id', $currentCol))->bindValue(':id', $this->id)->execute(); $transaction->commit(); return true; } catch (Exception $e) { $transaction->rollback(); return false; } }