当前位置: 首页>>代码示例>>PHP>>正文


PHP Product::beforeSave方法代码示例

本文整理汇总了PHP中common\models\Product::beforeSave方法的典型用法代码示例。如果您正苦于以下问题:PHP Product::beforeSave方法的具体用法?PHP Product::beforeSave怎么用?PHP Product::beforeSave使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在common\models\Product的用法示例。


在下文中一共展示了Product::beforeSave方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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();
     $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]);
     }
 }
开发者ID:md-nurul-islam,项目名称:simple-ecom,代码行数:55,代码来源:ProductController.php


注:本文中的common\models\Product::beforeSave方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。