Yii2 - 完整性约束违规 - yii \ db \ IntegrityException

时间:2017-06-05 07:32:12

标签: mysql yii2

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

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查询时出现的外键的顺序时,第一个失败,这意味着模型的所有外键都失败。

EDITED

这是我在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 ::在客户模型中创建功能

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 ::在地块模型中创建功能

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

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

}

1 个答案:

答案 0 :(得分:0)

很抱歉打扰你们。我能够解决我出错的问题。在Parcels模型的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'];

抱歉因为我的疏忽而花时间。