gpt4 book ai didi

mysql - Yii2 - 违反完整性约束 – yii\db\IntegrityException

转载 作者:行者123 更新时间:2023-11-29 18:50:51 25 4
gpt4 key购买 nike

我在尝试将数据添加到数据库时遇到了一个相当奇怪的错误。它一直告诉我存在完整性违规,即

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,
]);
}
}

Customers::create 客户模型中的函数

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();
}

}

地 block 模型中的 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;
}

这是地 block 模型中的函数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);

}

最佳答案

抱歉打扰大家了。我能够找到我哪里出错了的问题。在Parcels Model的create()函数中,我只创建了新变量,并没有将它们绑定(bind)到模型,即

$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'];

很抱歉因为我的粗心耽误了您的时间。

关于mysql - Yii2 - 违反完整性约束 – yii\db\IntegrityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44363905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com