【发布时间】:2014-01-14 03:39:19
【问题描述】:
我正在使用框架 Laravel。
我有 2 个表(用户和人员)。我想将persons表的主键person_id存储在users表的外键person_id中。目前我使用这个 $user->person_id = '1';
表用户
CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
`user_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_username` VARCHAR(45) NOT NULL,
`user_email` VARCHAR(45) NOT NULL,
`user_password` CHAR(32) NOT NULL,
`user_salt` CHAR(32) NOT NULL,
`user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_modified` TIMESTAMP NULL,
`user_deleted` TIMESTAMP NULL,
`user_lastlogin` TIMESTAMP NULL,
`user_locked` TIMESTAMP NULL,
`user_token` VARCHAR(128) NULL,
`user_confirmed` TIMESTAMP NULL,
PRIMARY KEY (`user_id`, `person_id`),
UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
INDEX `fk_users_persons1_idx` (`person_id` ASC),
CONSTRAINT `fk_users_persons1`
FOREIGN KEY (`person_id`)
REFERENCES `festival_aid`.`persons` (`person_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
桌人
CREATE TABLE IF NOT EXISTS `festival_aid`.`persons` (
`person_id` BIGINT NOT NULL AUTO_INCREMENT,
`person_firstname` VARCHAR(45) NULL,
`person_surname` VARCHAR(45) NULL,
`person_created` TIMESTAMP NOT NULL,
`person_modified` TIMESTAMP NULL,
`person_deleted` TIMESTAMP NULL,
PRIMARY KEY (`person_id`))
ENGINE = InnoDB;
索引操作
public function index()
{
$person = Person::with('user')->orderBy('person_id')->paginate(10);
return View::make('persons.index')
->with('person', $person);
}
商店操作
public function store()
{
$input = Input::all();
$rules = array();
$validator = Validator::make($input, $rules);
if($validator->passes())
{
$password = $input['user_password'];
$password = Hash::make($password);
$person = new Person();
$user = new User();
$person->person_firstname = $input['person_firstname'];
$person->person_surname = $input['person_surname'];
$user->user_username = $input['user_username'];
$user->user_email = $input['user_email'];
$user->user_password = $password;
$person->save();
$user->person()->associate($person, 'person_id', 'user_id');
$user->save();
Session::flash('message', 'Successfully created user!');
return Redirect::to('persons/index');
}
else {
return Redirect::to('persons/create')->withInput()->withErrors($validator);
}
}
用户迁移
Schema::table('users', function(Blueprint $table)
{
$table->increments('user_id');
$table->string('user_email');
$table->timestamp('user_created');
$table->timestamp('user_modified');
$table->timestamp('user_deleted');
$table->timestamp('user_lastlogin');
$table->timestamp('user_locked');
$table->foreign('person_id')
->references('id')->on('persons')
->onDelete('cascade');
});
人员迁移
public function up()
{
Schema::table('persons', function(Blueprint $table)
{
$table->increments('person_id');
$table->string('person_firstname');
$table->string('person_surname');
});
}
模型用户
class User extends Eloquent {
protected $primaryKey = 'user_id';
public function persons()
{
return $this->hasOne('Person');
}
public $timestamps = false;
}
模范人物
class Person extends Eloquent {
protected $table = 'persons';
protected $primaryKey = 'person_id';
public function users()
{
return $this->belongsTo('User');
}
public $timestamps = false;
}
我只想改变这个:$user->person_id = '1';因为对于每个用户,我将外键 person_id 设为 1。我希望 users 表中的外键 person_id 与 person 表的主键 person_id 匹配。
【问题讨论】:
-
我认为需要明确你想要什么,错误是什么,或者你正在追随但没有得到的目标。
-
我没有收到任何错误。我只想改变这个: $user->person_id = '1';因为对于每个用户,我将外键 person_id 设为 1。我希望 users 表中的外键 person_id 与 person 表的主键 person_id 匹配。
标签: php laravel foreign-keys primary-key eloquent