Beispiel #1
0
 public function make($data)
 {
     $data = is_object($data) && $data instanceof Container ? $data->assoc() : $data;
     if (!Arrays::isAssoc($data)) {
         throw new Exception("This method needs a valid object to process.");
     }
     $e = $data['entity'] = $this->entity;
     $class = $this;
     $obj = new Container();
     $store = function () use($obj, $e) {
         $class = new Attributes($e);
         return $class->save($obj);
     };
     $remove = function () use($obj, $e) {
         $class = new Attributes($e);
         return $class->delete($obj);
     };
     $date = function ($f) use($obj) {
         return date('Y-m-d H:i:s', $obj->{$f});
     };
     $display = function ($f, $echo = true) use($obj) {
         if (false === $echo) {
             return Html\Helper::display($obj->{$f});
         } else {
             echo Html\Helper::display($obj->{$f});
         }
     };
     $obj->event('store', $store)->event('trash', $remove)->event('date', $date)->event('display', $display);
     foreach ($data as $k => $v) {
         if (!isset($obj->{$k})) {
             $obj->{$k} = $v;
         }
     }
     return $obj;
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new Attributes();
     if (isset($_POST['Attributes'])) {
         $model->attributes = $_POST['Attributes'];
         if ($model->save()) {
             if ($model->Type == 'dropdown' || $model->Type == 'checkbox') {
                 $field_type_values = $_POST['field_type_values'];
                 foreach ($field_type_values as $field_type_value) {
                     $attval = new AttributeValues();
                     $attval->AttributeId = $model->Objid;
                     $attval->AttributeValue = $field_type_value;
                     $attval->save();
                 }
             }
             $this->redirect(array('index'));
         }
     }
     $this->render('create', array('model' => $model));
 }
Beispiel #3
0
 public function actionSaveProduct()
 {
     $product = Products::model()->findByPk($_POST['productId']);
     $attributes = json_decode($_POST['attributes']);
     //удмлим все значения, связанные с текущим атрибутом и сам атрибут
     $attributes_AR = Attributes::model()->findAll("product_id=:product_id", array(':product_id' => $_POST['productId']));
     foreach ($attributes_AR as $attribute) {
         Values::model()->deleteAll("attribute_id=:attribute_id", array(':attribute_id' => $attribute->id));
         $attribute->delete();
     }
     foreach ($attributes as $attribute) {
         $attribute_AR = new Attributes();
         $attribute_AR->product_id = $product->id;
         $attribute_AR->name = $attribute->name;
         $res = $attribute_AR->save();
         if ($res == false) {
             echo "false-attr";
             return;
         }
         foreach ($attribute->values as $value) {
             $value_AR = new Values();
             $value_AR->name = $value->name;
             $value_AR->add_price = $value->add_price;
             $value_AR->attribute_id = $attribute_AR->id;
             $res = $value_AR->save();
             if ($res == false) {
                 echo "false-value";
                 return;
             }
         }
     }
     $product->discount = $_POST['discount'];
     $product->name = $_POST['name'];
     $product->text = $_POST['text'];
     if ($_POST['photo'] != '') {
         $product->photo = $_POST['photo'];
     }
     $product->main_price = $_POST['main_price'];
     $product->sostav = $_POST['sostav'];
     $res = $product->update();
     if ($res == false) {
         var_dump($product->errors);
         echo "false-prod";
         return;
     }
     echo "true";
 }