【问题标题】:Laravel - store the value of primary key from persons table in the foreign key of users tableLaravel - 将个人表中的主键值存储在用户表的外键中
【发布时间】: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


【解决方案1】:

使用查询生成器,您可以获得插入的最后一条记录的 id:

$id = DB::table('users')->insertGetId(array('email' => 'john@example.com', 'votes' => 0));

但是如果你使用 Eloquent,我认为你应该使用 associate 方法,这是取自 Laravel 帮助的代码示例,你应该将它适应你的用户,个人逻辑:

$account = Account::find(10);
$user->account()->associate($account);
$user->save()

希望有所帮助。

【讨论】:

  • 现在我正在使用 $user->person()->associate($person);它一直告诉我 SQLSTATE[23000]:完整性约束违规:1048 列“person_id”不能为空
  • 我需要放置 $user->person()->associate($person, 'person_id', 'user_id'); $person()->保存之间;和 $user()->保存;
猜你喜欢
  • 1970-01-01
  • 2017-07-26
  • 2021-10-25
  • 1970-01-01
  • 2018-03-29
  • 2021-08-05
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多