本文整理汇总了PHP中common\models\Product::save方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::save方法的具体用法?PHP Product::save怎么用?PHP Product::save使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类common\models\Product
的用法示例。
在下文中一共展示了Product::save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', ['model' => $model]);
}
}
示例2: actionCreate
/**
* Creates a new EntpProduct model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post())) {
$model->loadDefaultValues();
$model->save();
if ($model->save()) {
$produk = new EntpProduct();
$produk->product_id = $this->id;
$produk->entrepreneur_user_id = Yii::$app->user->id;
$produk->link('product', $model);
}
Yii::$app->session->setFlash('success', 'Create New Product Success');
return $this->redirect(['index']);
//return $this->redirect(['view', 'entrepreneur_id' => $model->entrepreneur_id, 'product_id' => $model->product_id]);
} else {
return $this->render('create', ['model' => $model]);
}
}
示例3: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('success', 'Товар успешно создан!');
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', ['model' => $model]);
}
}
示例4: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$categories = Category::find()->all();
$model = new Product();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', ['model' => $model, 'categories' => $categories]);
}
}
示例5: actionCreate
public function actionCreate()
{
$model = new Product();
if ($model->load(\Yii::$app->request->post())) {
if ($model->validate() && $model->save()) {
return $this->redirect(['index']);
}
}
return $this->render('form', ['model' => $model]);
}
示例6: insert
public function insert(ProductDto $productDto)
{
$product = new Product();
if ($product->load(['Product' => ArrayHelper::toArray($productDto)])) {
$product->type = ArrayUtil::arrayToInt($productDto->type);
$product->save();
return ArrayHelper::toArray($product);
} else {
return [];
}
}
示例7: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
$categoryModel = new CategoryProduct();
$treeParents = TreeHelper::build($categoryModel->find()->addOrderBy('tree')->addOrderBy('lft')->all());
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('create', ['model' => $model, 'treeParents' => $treeParents]);
}
}
示例8: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$currUser = Yii::$app->user->getIdentity();
$currMb = MerchantBrand::find()->where(['merchant_brand_id' => $currUser->username])->one();
$mbFk = $currMb->_id;
$model = new Product();
$model->merchant_brand_fk = $mbFk;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => (string) $model->_id]);
} else {
return $this->render('create', ['model' => $model]);
}
}
示例9: simpan
public function simpan()
{
$model = new Product();
$model->product_name = $this->name;
$model->product_price = $this->price;
$model->product_category = $this->category;
$model->product_created_date = date('Y-m-d');
$model->product_description = $this->description;
$model->admin_id = Yii::$app->user->getId();
$model->product_slug = $this->slug;
$model->save();
$this->uploadsAndSave();
}
示例10: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
if (!is_numeric($model['agreement_id'])) {
$agreement = Agreement::find()->where(["default_flag" => 1])->asArray()->one();
$model['agreement_id'] = $agreement['id'];
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
$redirect = yii::$app->request->post('goto') == 'list' ? ['index'] : ['update', 'id' => $model->id];
return $this->redirect($redirect);
}
return $this->render('create', ['model' => $model]);
}
示例11: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
$categoryModel = new ProductCategory();
$companyModel = new ProductCompany();
$treeParents = TreeHelper::build($categoryModel->find()->addOrderBy('tree')->addOrderBy('lft')->all());
$treeCompany = ArrayHelper::map($companyModel->findAll(['status' => 10]), 'id', 'title');
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('create', ['model' => $model, 'treeParents' => $treeParents, 'treeCompany' => $treeCompany]);
}
}
示例12: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Product();
$model_category = new Category();
$model_product_category = new ProductCategory();
$model_manufacturer = new Manufacturer();
$model_product_manufacturer = new ProductManufacturer();
$upload_model = new UploadForm();
$resource = new Resources();
$resource_product = new ResourcesProduct();
if (isset($_POST['Product']) && !empty($_POST['Product'])) {
$model->attributes = $_POST['Product'];
$model->beforeSave(TRUE);
if ($model->validate() && $model->save()) {
if (empty($_POST['ProductCategory']['category_id'])) {
$model_product_category->addError('category_id', 'No category selected.');
} else {
foreach ($_POST['ProductCategory']['category_id'] as $category) {
$model_product_category = new ProductCategory();
$model_product_category->product_id = $model->id;
$model_product_category->category_id = $category;
$model_product_category->save();
}
}
if (isset($_POST['ProductManufacturer']) && !empty($_POST['ProductManufacturer']['manufacturer_id'])) {
$model_product_manufacturer->attributes = $_POST['ProductManufacturer'];
$model_product_manufacturer->product_id = $model->id;
$model_product_manufacturer->save();
}
$upload_model->imageFiles = UploadedFile::getInstances($upload_model, 'imageFiles');
$uploaded_files = $upload_model->upload('product_image');
if (!empty($uploaded_files)) {
foreach ($uploaded_files as $uploaded_file) {
$resource = new Resources();
$resource->attributes = $uploaded_file;
$resource->beforeSave(TRUE);
if ($resource->validate() && $resource->save()) {
$resource_product = new ResourcesProduct();
$resource_product->product_id = $model->id;
$resource_product->resources_id = $resource->id;
$resource_product->save();
}
}
}
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', ['model' => $model, 'model_manufacturer' => $model_manufacturer, 'model_product_manufacturer' => $model_product_manufacturer, 'model_category' => $model_category, 'model_product_category' => $model_product_category, 'upload_model' => $upload_model]);
}
}
示例13: save
/**
* 保存商品
*/
public function save()
{
#获得用户的地区信息用来提供浏览者的显示信息
$userinfo = \Yii::$app->user->getIdentity();
$newProduct = new Product();
$newProduct->product_country = 0;
if (!empty($userinfo->province)) {
$newProduct->product_province = $userinfo->province;
}
if (!empty($userinfo->city)) {
$newProduct->product_city = $userinfo->city;
}
if (!empty($userinfo->area)) {
$newProduct->product_area = $userinfo->area;
}
$newProduct->setAttributes($this->attributes);
return $newProduct->save();
}
示例14: actionCreate
/**
* Creates a new Product model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
// echo Yii::getAlias('@common'); exit;
$model = new Product();
$modelProdMeta = new ProductMeta();
$stores = [];
if (\Yii::$app->user->identity->id == 1) {
$stores = \common\models\UserStore::find()->select('store_id')->where(['user_id' => Yii::$app->user->identity->id])->all();
$stores = \yii\helpers\ArrayHelper::getColumn($stores, 'store_id');
$storeList = \common\models\Store::find()->where(['IN', 'store_id', $stores])->all();
} else {
$storeList = \common\models\Store::find()->all();
}
if ($postData = Yii::$app->request->post()) {
if ($model->load($postData) && $model->save()) {
$prod_id = $model->prod_id;
foreach ($model->cat_id as $cat) {
$catModel = new ProdCat();
$catModel->cat_id = $cat;
$catModel->prod_id = $prod_id;
$catModel->save();
}
$file = UploadedFile::getInstances($model, 'images');
$model->file = $file[0];
$image_path = Yii::getAlias('@frontend') . '/web/uploads/product/' . $model->file->baseName . '_' . $prod_id . '.' . $model->file->extension;
$model->file->saveAs($image_path);
$this->updateProductMeta($id, 'prod_img', $model->file->baseName . '_' . $prod_id . '.' . $model->file->extension);
foreach ($postData['PMeta'] as $key => $meta_value) {
$this->updateProductMeta($id, $key, $meta_value);
}
return $this->redirect(['view', 'id' => $model->prod_id]);
}
} else {
return $this->render('create', ['model' => $model, 'modelProdMeta' => $modelProdMeta, 'storeList' => $storeList]);
}
}
示例15: actionImport
/**
* batch import product
* @param integer $id
* @return mixed
*/
public function actionImport()
{
//if(!Yii::$app->user->can('viewYourAuth')) throw new ForbiddenHttpException(Yii::t('app', 'No Auth'));
$format = Product::getImportExportFormat();
if (Yii::$app->request->post()) {
$countCreate = $countUpdate = 0;
$file = UploadedFile::getInstanceByName('importFile');
$handle = fopen($file->tempName, 'r');
$result = $this->inputCsv($handle);
$arrData = [];
if (count($result) <= 1) {
Yii::$app->getSession()->setFlash('danger', Yii::t('app', 'No Record, please check file.'));
} else {
// 将数据的key从数字变成固定的格式
for ($i = 1; $i < count($result); $i++) {
$j = 0;
foreach ($format as $item) {
$data[$item] = $result[$i][$j];
$j++;
}
$data['thumbs'] = $result[$i][$j];
$data['images'] = $result[$i][$j + 1];
array_push($arrData, $data);
}
// 处理数据,如果ID大于0,则更新,否则新增
$line = 2;
$errorLines = [];
foreach ($arrData as $item) {
if ($item['id'] > 0) {
// 已存在的值,则更新数据,以及判断缩略图和图片
$model = Product::findOne($item['id']);
if ($model === null) {
array_push($errorLines, $line);
$line++;
continue;
}
foreach ($item as $k => $v) {
if ($k == 'id' || $k == 'thumbs' || $k == 'images') {
continue;
}
$model[$k] = iconv('gb2312', 'utf-8', trim($v));
}
$result = $model->save();
if (!$result) {
//如果保存失败
array_push($errorLines, $line);
$line++;
continue;
}
$countUpdate++;
if ($item['thumbs'] && $item['images']) {
$arrThumb = explode('|', $item['thumbs']);
$arrImage = explode('|', $item['images']);
$i = 0;
$ids = [];
foreach ($arrThumb as $thumb) {
$thumb = trim($thumb);
$image = trim($arrImage[$i]);
$productImage = ProductImage::find()->where(['product_id' => $item['id'], 'thumb' => $thumb, 'image' => $image])->one();
if ($productImage) {
//如果图片在数据库中已经存在,则假如到ids数组,防止后续被删除
array_push($ids, $productImage->id);
} else {
//不存在的话,新增记录并将id加入到ids
$productImage = new ProductImage(['product_id' => $item['id'], 'thumb' => $thumb, 'image' => $image]);
$productImage->save();
array_push($ids, $productImage->id);
}
$i++;
}
//删除在ids数组中记录
ProductImage::deleteAll(['and', 'product_id=' . $item['id'], ['not in', 'id', $ids]]);
}
} else {
// 新的数据,插入,并将缩略图和图片插入
$model = new Product();
foreach ($item as $k => $v) {
if ($k == 'id' || $k == 'thumbs' || $k == 'images') {
continue;
}
$model[$k] = iconv('gb2312', 'utf-8', trim($v));
}
// 将分类和品牌转换成对应的ID
$category = Category::find()->where(['name' => trim($model->category_id)])->one();
$model->category_id = $category ? $category->id : 1;
$brand = Brand::find()->where(['name' => trim($model->brand_id)])->one();
$model->brand_id = $brand ? $brand->id : 0;
$result = $model->save();
if (!$result) {
//如果保存失败
array_push($errorLines, $line);
$line++;
continue;
}
$countCreate++;
//.........这里部分代码省略.........