本文整理汇总了PHP中app\modules\shop\models\Product::setAttributes方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::setAttributes方法的具体用法?PHP Product::setAttributes怎么用?PHP Product::setAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类app\modules\shop\models\Product
的用法示例。
在下文中一共展示了Product::setAttributes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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]);
}
}