【发布时间】:2019-05-27 12:53:27
【问题描述】:
我有两个型号Customer 和Address。我的Customer 具有非增量主键,类型为string,即customer_id。这两个模型之间是一对多的关系,即对于单个customer 多个addresses 例如:发票地址、收货地址、当前地址等。我的Customer 模型如下图:
Customer.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $keyType = 'string';
protected $primaryKey = 'customer_id';
public $incrementing = false;
public function addresses()
{
return $this->hasMany('App\Address','customer_id');
}
}
而我的地址模型如下图:
地址.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Address extends Model
{
//
public $timestamps = false;
// protected $table = "addresses";
public function customer()
{
return $this->belongsTo('App\Customer');
}
}
下面显示了我的客户表的迁移
客户迁移表
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->string('customer_id');
$table->string('name');
$table->string('type');
$table->date('dob');
$table->type('country_code');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('customers');
}
}
要注意的另一件事是,我的customer_id 是增量的,因为我创建了单独的表,即customer_sequence,它是自动增量的,在插入记录之前,我使用触发器附加两个字符代码,然后将其放入我的customers 表。
我的customer_sequence迁移如下图
customer_sequence 迁移
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSequenceCustomers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sequence_customers', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sequence_customers');
}
}
我用于插入递增字符串 id 的触发器如下:
customer_id 触发器的迁移
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTriggerCustomers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
DB::unprepared("
CREATE TRIGGER tg_customer_insert
BEFORE INSERT ON customers
FOR EACH ROW
BEGIN
INSERT INTO sequence_customers(id) VALUES (NULL);
IF NEW.type ='Private' THEN
SET NEW.customer_id = CONCAT(NEW.country_code, LPAD(LAST_INSERT_ID(), 5, '0'));
ELSEIF NEW.type='Business' THEN
SET NEW.customer_id = CONCAT(NEW.country_code, LPAD(LAST_INSERT_ID(), 5, '0'));
ELSEIF NEW.type='Reseller' THEN
SET NEW.customer_id = LPAD(LAST_INSERT_ID(), 5, '0');
ELSEIF NEW.type='Distributor' THEN
SET NEW.customer_id = LPAD(LAST_INSERT_ID(), 5, '0');
ELSEIF NEW.type='Other' THEN
SET NEW.customer_id = LPAD(LAST_INSERT_ID(), 5, '0');
END IF;
IF NEW.credit_amount > NEW.credit_limit THEN
SET NEW.credit_limit_exceeded=TRUE;
ELSE
SET NEW.credit_limit_exceeded=FALSE;
END IF;
END
");
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::unprepared('DROP TRIGGER IF EXISTS tg_customer_insert');
}
}
现在,当我保存客户的数据并尝试从客户模型中获取 id 时,它返回给我 null。我的控制器如下图:
CustomerController.php
public function store(Request $request)
{
$customer = new Customer;
$invoiceAddress = new Address;
$deliveryAddress = new Address;
$customer->name = $request->name;
$customer->type = $request->type;
$customer->dob = $request->dob;
$customer->country_code=$request->country_code;
$customer->save();
$deliveryAddress->street_name_no = $request->street_name_no;
$deliveryAddress->city = $request->city;
$deliveryAddress->country = $request->country;
//This throws error customer_id cannot be null integrity constraint
$deliveryAddress->customer_id = $customer->customer_id;
$deliveryAddress->save();
}
【问题讨论】: