我在尝试向数据库中添加数据时遇到了一个很奇怪的错误。它不断告诉我存在违反诚信的行为,即
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`ticketing_system`.`parcels`, CONSTRAINT `FK_parcels_customers_customer_id` FOREIGN KEY (`sender_id`) REFERENCES `customers` (`customer_id`))
The SQL being executed was: INSERT INTO `parcels` (`parcel_id`) VALUES (DEFAULT)
我检查了要插入的值,并从引用表中选择了所有值。有趣的是,当我回显这些值并使用它们通过PHPMyAdmin上的SQL查询直接更新数据库时,该数据库可以很好地填充。同样,当我尝试更改外键在执行的SQL查询中出现的顺序时,第一个失败,这意味着该模型的所有外键都失败了。
已编辑
这是我在ParcelsController中的actionCreate
public function actionCreate()
{
$model = new Parcels();
$customerModel = new Customers();
//checking whether we are getting the logged in user id value
Yii::info("User id=".Yii::$app->user->id);
$model->received_by_id = Yii::$app->user->id;
if (Yii::$app->request->post()) {
$data = Yii::$app->request->post('Customers');
$sender_id = Customers::create([
'name' => $data['name'],
'mobile_number' => $data['mobile_number'],
'sex' => $data['sex'],
'address' => $data['address'],
'registered_by_id' => $model->received_by_id,
]);
$data = Yii::$app->request->post('Parcels');
$receiver_id = Customers::create([
'name' => $data['name'],
'mobile_number' => $data['mobile_number'],
'sex' => $data['sex'],
'address' => $data['address'],
'registered_by_id' => $model->received_by_id,
]);
$model->consignment_number = $model->generateUniqueRandomString("consignment_number");
$model->source_id = $model->receivedBy->station_id;
$model->parcel_id = Parcels::create([
'consignment_number' => $model->consignment_number,
'sender_id' => $sender_id,
'receiver_id' => $receiver_id,
'source_id' => $model->source_id,
'destination_id' => $data['destination_id'],
'type_id' => $data['type_id'],
'weight' => $data['weight'],
'cost' => $data['cost'],
'parcel_info' => $data['parcel_info'],
'received_by_id' => $model->received_by_id,
]);
// return $this->redirect(['view', 'id' => $model->parcel_id]);
} else {
return $this->render('create', [
'model' => $model, 'customerModel' => $customerModel,
]);
}
}
客户:在客户模型中创建功能
public static function create($data)
{
$model = new self;
$mobile_number = $data['mobile_number'];
$exists = $model->find()->where( [ 'mobile_number' => $mobile_number ] )->exists();
if($exists) {
$existing_customer = Customers::find()
->where('mobile_number = :mobile_number', [':mobile_number' => $mobile_number])
->one();
return $existing_customer['customer_id'];
}
else {
$model->name = $data['name'];
$model->registered_by_id = $data['registered_by_id'];
$model->mobile_number = $data['mobile_number'];
$model->sex = $data['sex'];
$model->address = $data['address'];
$model->status = 10;
$model->save();
return $model->getPrimaryKey();
}
}
Parcels模型中的Parcels :: create函数
public static function create($data)
{
$model = new self;
$consignment_number = $data['consignment_number'];
$receiver_id = $data['receiver_id'];
$sender_id = $data['sender_id'];
$source_id = $data['source_id'];
$destination_id = $data['destination_id'];
$type_id = $data['type_id'];
$weight = $data['weight'];
$cost = $data['cost'];
$parcel_info = $data['parcel_info'];
$received_by_id = $data['received_by_id'];
$model->save(false);
return $model->getPrimaryKey();
// echo var_dump($data);
// echo "Consignment Number: ".$consignment_number." - Sender: ".$sender_id." - Receiver: ".$receiver_id." - From: ".$source_id." - To: ".$destination_id." - Type: ".$type_id." - Weight: ".$weight." - Cost: ".$cost." - Parcel Info: ".$parcel_info." - Received By: ".$received_by_id;
}
这是宗地模型中的函数generateUniqueRandomString()
public function generateUniqueRandomString($attribute, $length = 10) {
$randomString = Yii::$app->getSecurity()->generateRandomString($length);
$randomString = strtoupper($randomString);
if(!$this->findOne([$attribute => $randomString]))
return $randomString;
else
return $this->generateUniqueRandomString($attribute, $length);
}
最佳答案
抱歉打扰你们。我能够解决我要去哪里的问题。在宗地模型的create()函数中,我仅创建了新变量,但未能将其绑定到模型,即
$consignment_number = $data['consignment_number'];
$receiver_id = $data['receiver_id'];
$sender_id = $data['sender_id'];
$source_id = $data['source_id'];
$destination_id = $data['destination_id'];
$type_id = $data['type_id'];
$weight = $data['weight'];
$cost = $data['cost'];
$parcel_info = $data['parcel_info'];
$received_by_id = $data['received_by_id'];
本来应该:
$model->consignment_number = $data['consignment_number'];
$model->weight = $data['weight'];
$model->cost = $data['cost'];
$model->parcel_info = $data['parcel_info'];
$model->received_by_id = $data['received_by_id'];
$model->status = $data['status'];
$model->receiver_id = $data['receiver_id'];
$model->sender_id = $data['sender_id'];
$model->source_id = $data['source_id'];
$model->destination_id = $data['destination_id'];
$model->type_id = $data['type_id'];
对不起,由于我的粗心,抽出宝贵的时间。