本文整理汇总了PHP中app\modules\shop\models\Product::saveProperties方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::saveProperties方法的具体用法?PHP Product::saveProperties怎么用?PHP Product::saveProperties使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\modules\shop\models\Product
的用法示例。
在下文中一共展示了Product::saveProperties方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionGenerate
/**
* @param $id
* @throws NotFoundHttpException
* @throws \yii\db\Exception
*/
public function actionGenerate($id)
{
$post = \Yii::$app->request->post();
if (!isset($post['GeneratePropertyValue'])) {
throw new NotFoundHttpException();
}
$parent = Product::findById($id, null);
if ($parent === null) {
throw new NotFoundHttpException();
}
$object = Object::getForClass(Product::className());
$catIds = (new Query())->select('category_id')->from([$object->categories_table_name])->where('object_model_id = :id', [':id' => $id])->orderBy(['sort_order' => SORT_ASC, 'id' => SORT_ASC])->column();
if (isset($post['GeneratePropertyValue'])) {
$generateValues = $post['GeneratePropertyValue'];
$post[HasProperties::FIELD_ADD_PROPERTY_GROUP]['Product'] = $post['PropertyGroup']['id'];
} else {
$generateValues = [];
}
$parent->option_generate = Json::encode(['group' => $post['PropertyGroup']['id'], 'values' => $generateValues]);
$parent->save();
$postProperty = [];
foreach ($post['GeneratePropertyValue'] as $key_property => $values) {
$inner = [];
foreach ($values as $key_value => $trash) {
$inner[] = [$key_property => $key_value];
}
$postProperty[] = $inner;
}
$optionProperty = self::generateOptions($postProperty);
foreach ($optionProperty as $option) {
/** @var Product|HasProperties $model */
$model = new Product();
$model->load($post);
$model->parent_id = $parent->id;
$nameAppend = [];
$slugAppend = [];
$tempPost = [];
// @todo something
foreach ($option as $optionValue) {
foreach ($optionValue as $propertyKey => $propertyValue) {
if (!isset($valueModels[$propertyKey])) {
$propertyStaticValues = PropertyStaticValues::findOne($propertyValue);
$propertyValue = PropertyStaticValues::findById($propertyValue);
$key = $propertyStaticValues->property->key;
$tempPost[$key] = $propertyValue;
}
$nameAppend[] = $propertyValue['name'];
$slugAppend[] = $propertyValue['id'];
}
}
$model->measure_id = $parent->measure_id;
$model->name = $parent->name . ' (' . implode(', ', $nameAppend) . ')';
$model->slug = $parent->slug . '-' . implode('-', $slugAppend);
$save_model = $model->save();
$postPropertyKey = 'Properties_Product_' . $model->id;
$post[$postPropertyKey] = $tempPost;
if ($save_model) {
foreach (array_keys($parent->propertyGroups) as $key) {
$opg = new ObjectPropertyGroup();
$opg->attributes = ['object_id' => $parent->object->id, 'object_model_id' => $model->id, 'property_group_id' => $key];
$opg->save();
}
$model->saveProperties(['Properties_Product_' . $model->id => $parent->abstractModel->attributes]);
$model->saveProperties($post);
unset($post[$postPropertyKey]);
$add = [];
foreach ($catIds as $value) {
$add[] = [$value, $model->id];
}
if (!empty($add)) {
Yii::$app->db->createCommand()->batchInsert($object->categories_table_name, ['category_id', 'object_model_id'], $add)->execute();
}
$params = $parent->images;
if (!empty($params)) {
$rows = [];
foreach ($params as $param) {
$rows[] = [$param['object_id'], $model->id, $param['filename'], $param['image_title'], $param['image_alt'], $param['sort_order']];
}
Yii::$app->db->createCommand()->batchInsert(Image::tableName(), ['object_id', 'object_model_id', 'filename', 'image_title', 'image_alt', 'sort_order'], $rows)->execute();
}
}
}
}