2016-07-23 43 views
1

我有ID從模型/預先序列中有模型/訂單關係的PK(自動公司)與FK id_order,在我的CRUD動作例如actionCreate中,我不能由於訂單中的id(PK)始終爲null,因此將表預插入attibutes表。我該如何解決 ?。Yii2不能插入id(PK)的屬性(FK)

這裏是我的控制器

$cartPositions = Yii::$app->cart->getPositions(); 

    if (!$cartPositions or $cartPositions === null) { 
     return $this->redirect(['index']); 
    } 

    $dataProvider = new ArrayDataProvider([ 
     'allModels' => $cartPositions, 
    ]); 

    $model = new Order(); 
    $model_po = new Preorder(); 
    $postData = Yii::$app->request->post(); 

     if ($model->load($postData) && $model_po->load($postData)) { 
     //model->save(false); 
     $model->status = 5; 
     $model_po->id_order = $model->id; 
     $model->total_cost = Yii::$app->cart->getCost(); 
     $model->date = date('Y-m-d H:i'); 
     $model->data = Yii::$app->cart->getSerialized(); 
     $model_po->name = $model->name; 
     $model_po->phone = $model->phone; 
     $model_po->remarks = $model->message; 
     $model_po->created_at = $model->date; 
     //$model_po->save(); 
     if (Model::validateMultiple([$model, $model_po]) && $model->save(false) && $model_po->save()) { 
      Yii::$app->session->setFlash('success', 'Thank You'); 
      Yii::$app->mailer->compose('order/html', [ 
       'model' => $model, 
       //'model_po' => $model_po, 
       'dataProvider' => $dataProvider, 
      ]) 
       //->setFrom(Yii::$app->params['email']['from']) 
       // ->setTo(Yii::$app->params['email']['to']) 
       // ->setSubject('The site posted a new order') 
       // ->send(); 

       ->setFrom(Yii::$app->params['email']['from']) 
       ->setTo(Yii::$app->params['email']['to']) 
       ->setSubject('The site posted a new Preorder') 
       ->send(); 

      Yii::$app->cart->removeAll(); 
      return $this->render('orderSuccess', [ 
       'model' => $model, 
       //'model_po' => $model_po, 
      ]); 
     } 
    } else 
    {return $this->render('create_po', [ 
     'model' => $model, 
     'model_po' => $model_po, 
     'dataProvider' => $dataProvider, 
    ]);} 
    } 

機型/訂單

public function rules() 
{ 
    return [ 
     [['status', 'total_cost', 'date', 'data', 'name', 'phone'], 'required'], 
     [['code_order'], 'autonumber', 'format'=>'orderNum', 'digit'=>4], 
     [['status', 'total_cost'], 'integer'], 
     [['date'], 'safe'], 
     [['data', 'message'], 'string'], 
     [['name', 'email', 'phone'], 'string', 'max' => 255] 
    ]; 
} 

模型/預訂單

public function rules() 
{ 
    return [ 
     [['id_order', 'address'], 'required'], 
     [['id_order'], 'integer'], 
     [['created_at', 'updated_at'], 'safe'], 
     [['address', 'remarks'], 'string', 'max' => 500], 
     [['id_order'], 'exist', 'skipOnError' => true, 'targetClass' => Order::className(), 'targetAttribute' => ['id_order' => 'id']], 
    ]; 
} 

錯誤信息:

完整性約束違規 - YII \分貝\ IntegrityException SQLSTATE [23000]:INSERT INTO preorderaddressid_ordernamephoneremarkscreated_at:完整性約束違規: 正在執行的SQL是1048列 'id_order' 不能爲空)VALUES( '',NULL, '名稱', '121324325', '', '2016年7月23日17點01')

錯誤的信息:陣列 ( [0] => 23000 [1] => 1048 [2] =>列'id_order'不能爲空

我是嘗試的getPrimaryKey()和最後一個插入ID()無法正常工作和我嘗試刪除$ modelpo-> id_order = $模型 - > ID結果是填了兩個表,但id_order是0

回答

1

$模型 - > id爲空,直到成功$ model-> save(),所以你不應該在它之前將它的值賦給$ model_po-> id_order。

這是transaction的好地方,因此您可以先驗證數據,然後確保已保存訂單,然後用正確的id_order保存預訂。

請看link()方法。

+0

非常感謝你 –

0

你爲什麼不使用afterSave()?它將確定地解決您的問題

+0

謝謝你。我現在使用交易。 –