本文整理汇总了PHP中app\modules\shop\models\Product::validate方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::validate方法的具体用法?PHP Product::validate怎么用?PHP Product::validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\modules\shop\models\Product
的用法示例。
在下文中一共展示了Product::validate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionClone
/**
* Clone product action.
* @param integer $id
* @throws \yii\web\NotFoundHttpException
*/
public function actionClone($id, $returnUrl = ['index'])
{
/** @var Product|HasProperties $model */
$model = Product::findOne($id);
if ($model === null) {
throw new NotFoundHttpException();
}
/** @var Product|HasProperties $newModel */
$newModel = new Product();
$newModel->setAttributes($model->attributes, false);
$time = time();
$newModel->name .= ' (copy ' . date('Y-m-d h:i:s', $time) . ')';
$newModel->slug .= '-copy-' . date('Ymdhis', $time);
$newModel->id = null;
if ($newModel->validate() === false) {
$newModel->slug = substr(uniqid() . "-" . $model->slug, 0, 80);
}
if ($newModel->save()) {
$object = Object::getForClass(get_class($newModel));
// save categories
$categoriesTableName = Object::getForClass(Product::className())->categories_table_name;
$query = new Query();
$params = $query->select(['category_id', 'sort_order'])->from($categoriesTableName)->where(['object_model_id' => $model->id])->all();
if (!empty($params)) {
$rows = [];
foreach ($params as $param) {
$rows[] = [$param['category_id'], $newModel->id, $param['sort_order']];
}
Yii::$app->db->createCommand()->batchInsert($categoriesTableName, ['category_id', 'object_model_id', 'sort_order'], $rows)->execute();
}
// save images bindings
$params = $query->select(['object_id', 'filename', 'image_title', 'image_alt', 'sort_order'])->from(Image::tableName())->where(['object_id' => $object->id, 'object_model_id' => $model->id])->all();
if (!empty($params)) {
$rows = [];
foreach ($params as $param) {
$rows[] = [$param['object_id'], $newModel->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();
}
$newModelProps = [];
foreach (array_keys($model->propertyGroups) as $key) {
$opg = new ObjectPropertyGroup();
$opg->attributes = ['object_id' => $object->id, 'object_model_id' => $newModel->id, 'property_group_id' => $key];
$opg->save();
$props = Property::getForGroupId($key);
foreach ($props as $prop) {
$propValues = $model->getPropertyValuesByPropertyId($prop->id);
if ($propValues !== null) {
foreach ($propValues->values as $val) {
$valueToSave = ArrayHelper::getValue($val, 'psv_id', $val['value']);
$newModelProps[$prop->key][] = $valueToSave;
}
}
}
}
$newModel->saveProperties(['Properties_Product_' . $newModel->id => $newModelProps]);
Yii::$app->session->setFlash('success', Yii::t('app', 'Product has been cloned successfully.'));
$this->redirect(['edit', 'id' => $newModel->id, 'returnUrl' => $returnUrl]);
}
}
示例2: createProduct
protected function createProduct($item = [], $createNotExists = true)
{
if (empty($item) || !isset($item[static::ELEMENT_ID]) || !isset($item[static::ELEMENT_NAIMENOVANIE])) {
return false;
}
$guid = CommercemlGuid::findOne(['guid' => $item[static::ELEMENT_ID], 'type' => 'PRODUCT']);
if (empty($guid) && $createNotExists) {
$category = !empty($item['categories']) ? array_shift($item['categories']) : null;
$product = new Product();
$product->name = $product->title = $product->h1 = $item[static::ELEMENT_NAIMENOVANIE];
$product->slug = Helper::createSlug($product->name);
$product->main_category_id = isset($this->categoryCache[$category]) ? $this->categoryCache[$category] : $this->rootCategoryCache;
$product->content = empty($item[static::ELEMENT_OPISANIE]) ? '' : $item[static::ELEMENT_OPISANIE];
if ($product->validate() && $product->save()) {
$product->refresh();
$product->linkToCategory($this->rootCategoryCache);
$guid = new CommercemlGuid();
$guid->guid = $item[static::ELEMENT_ID];
$guid->name = $item[static::ELEMENT_NAIMENOVANIE];
$guid->model_id = $product->id;
$guid->type = 'PRODUCT';
$guid->save();
return true;
}
}
if (!empty($guid)) {
/** @var Product $product */
$product = isset($product) ? $product : $guid->product;
if (!empty($product)) {
$product->price = empty($item['price']) ?: array_shift($item['price']);
$product->content = empty($item[static::ELEMENT_OPISANIE]) ?: $item[static::ELEMENT_OPISANIE];
$product->name = empty($item[static::ELEMENT_NAIMENOVANIE]) ?: $item[static::ELEMENT_NAIMENOVANIE];
if (!empty($item['properties'])) {
AbstractPropertyEavModel::setTableName($this->objectProduct->eav_table_name);
$product_eav = array_reduce(AbstractPropertyEavModel::findByModelId($product->id), function ($result, $item) {
$key = $item->property_group_id . ':' . $item->key;
$result[$key] = $item;
return $result;
}, []);
$product_groups = array_reduce(ObjectPropertyGroup::getForModel($product), function ($result, $item) {
$result[] = $item->property_group_id;
return $result;
}, []);
$product_osv = array_reduce(ObjectStaticValues::findAll(['object_id' => $this->objectProduct->id, 'object_model_id' => $product->id]), function ($result, $item) {
$result[] = $item->property_static_value_id;
return $result;
}, []);
foreach ($item['properties'] as $key => $value) {
if (isset(static::$propertiesCache[$key])) {
/** @var Property $prop */
$prop = static::$propertiesCache[$key];
if (!in_array($prop->property_group_id, $product_groups)) {
$objectGroup = new ObjectPropertyGroup();
$objectGroup->object_id = $this->objectProduct->id;
$objectGroup->object_model_id = $product->id;
$objectGroup->property_group_id = $prop->property_group_id;
$objectGroup->save();
}
if ($prop->has_static_values) {
$psv = PropertyStaticValues::findOne(['value' => $value]);
if (empty($psv)) {
$psv = new PropertyStaticValues();
$psv->name = $psv->value = $value;
$psv->property_id = $prop->id;
$psv->slug = Helper::createSlug($value);
if ($psv->validate() && $psv->save()) {
$psv->refresh();
} else {
$psv = null;
}
}
if (!empty($psv) && !in_array($psv->id, $product_osv)) {
$osv = new ObjectStaticValues();
$osv->object_id = $this->objectProduct->id;
$osv->object_model_id = $product->id;
$osv->property_static_value_id = $psv->id;
$osv->save();
}
} elseif ($prop->is_eav) {
$_key = $prop->property_group_id . ':' . $prop->key;
if (isset($product_eav[$_key])) {
/** @var AbstractPropertyEavModel $eav */
$eav = $product_eav[$_key];
$eav->value = $value;
$eav->save();
} else {
$eav = new AbstractPropertyEavModel();
$eav->object_model_id = $product->id;
$eav->property_group_id = $prop->property_group_id;
$eav->key = $prop->key;
$eav->value = $value;
$eav->save();
}
}
}
}
}
$product->save();
return true;
}
//.........这里部分代码省略.........