Example #1
0
 /**
  * format item prop data to json format from post
  * @param $itemProps
  * @return array
  * @author Lujie.Zhou(gao_lujie@live.cn, qq:821293064).
  */
 protected function handleItemProps($itemProps)
 {
     $props = array();
     $props_name = array();
     foreach ($itemProps as $pid => $vid) {
         $itemProp = Yii::createObject(ItemProp::className());
         $itemProp = $itemProp::findOne(['prop_id' => $pid]);
         $pname = $itemProp->prop_name;
         if (is_array($vid)) {
             $props[$pid] = array();
             $props_name[$pname] = array();
             foreach ($vid as $v) {
                 $props[$pid][] = $pid . ':' . $v;
                 $propValue = Yii::createObject(PropValue::className());
                 $propValue = $propValue::findOne(['value_id' => $v]);
                 $vname = $propValue ? $propValue->value_name : $v;
                 $props_name[$pname][] = $pname . ':' . $vname;
             }
         } else {
             $props[$pid] = $pid . ':' . $vid;
             $propValue = Yii::createObject(PropValue::className());
             $propValue = $propValue::findOne(['value_id' => $vid]);
             $vname = $propValue ? $propValue->value_name : $vid;
             $props_name[$pname] = $pname . ':' . $vname;
         }
     }
     return array(json_encode($props), json_encode($props_name));
 }
Example #2
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPropValues()
 {
     return $this->hasMany(PropValue::className(), ['prop_id' => 'prop_id']);
 }
Example #3
0
 protected function savePropValue($prop_id)
 {
     if (isset($_POST['PropValue'])) {
         $propValues = $_POST['PropValue'];
         unset($_POST['PropValue']);
         if (is_array($propValues['value_name']) && ($count = count($propValues['value_name']))) {
             for ($i = 0; $i < $count; $i++) {
                 if (isset($propValues['value_id'][$i]) && $propValues['value_id'][$i]) {
                     $propValue = Yii::createObject(PropValue::className());
                     $propValue = $propValue::find()->where(['value_id' => $propValues['value_id'][$i]])->one();
                 } else {
                     $propValue = Yii::createObject(PropValue::className());
                 }
                 $propValue->setAttributes(array('prop_id' => $prop_id, 'value_name' => $propValues['value_name'][$i], 'sort_order' => $i, 'value_alias' => $propValues['value_name'][$i], 'status' => 1));
                 if (isset($propValues['value_id'][$i]) && $propValues['value_id'][$i]) {
                     $propValue->update();
                 } else {
                     $propValue->save();
                 }
                 $propValues['value_id'][$i] = $propValue->value_id;
             }
             $propValueModel = Yii::createObject(PropValue::className());
             //删除
             $models = $propValueModel::findAll(['prop_id' => $prop_id]);
             $delArr = array();
             foreach ($models as $k1 => $v1) {
                 if (!in_array($v1->value_id, $propValues['value_id'])) {
                     $delArr[] = $v1->value_id;
                 }
             }
             if (count($delArr)) {
                 $propValueModel = Yii::createObject(PropValue::className());
                 $propValueModel::deleteAll('value_id IN (' . implode(', ', $delArr) . ')');
             }
         } else {
             //已经没有属性了,要清除数据表内容
             $propValueModel = Yii::createObject(PropValue::className());
             $propValueModel::deleteAll('prop_id = ' . $prop_id);
         }
     }
 }