Doctrine 学说关系

Doctrine 学说关系,doctrine,models,foreign-key-relationship,Doctrine,Models,Foreign Key Relationship,我有两张桌子。用户表和配置文件表。配置文件表有一个users\u id外键。表的模型通过一对一关系设置。当我尝试保存一些数据时,会出现以下错误: Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'users_id' cannot be null' in 如

我有两张桌子。用户表和配置文件表。配置文件表有一个users\u id外键。表的模型通过一对一关系设置。当我尝试保存一些数据时,会出现以下错误:

Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'users_id' cannot be null' in
如果我只是将数据保存到users表中,那么我知道它会自动递增并生成一个新的id值。但是,由于某种原因,当我尝试将这些表链接在一起或调用$users->id时,它会返回NULL值

这是我的密码:

$u = new Users();
// Users table.
        $u->username   = $username;
        $u->password   = $password;
        $u->email      = $email;
        $u->groups_id   = $group_id;
        $u->ip_address = $ip_address;
        $u->last_login = now();
        $u->active     = 1;

    if ($this->store_salt)
    {
        $u->salt = $salt;
    }
    $u->save();

    // Profile table.
    $p = new Profile(); 
    $p->first = $additional_data['first'];
    $p->last = $additional_data['last'];
    $p->role = $additional_data['role'];
    $p->Users = $u;
    $p->save(); return;
以下是模型:

    • @属性整数$id
    • @属性字符串$username
    • @属性字符串$password
    • @属性字符串$ip\U地址
    • @属性日期$created\u在
    • @物业日期$updated\u于
    • @属性字符串$salt
    • @属性字符串$email
    • @属性字符串$U代码
    • @属性字符串$U密码\U代码
    • @属性字符串$member\u代码
    • @属性整数$last\u登录
    • @属性整数$active
    • @属性整数$groups\U id
    • @属性组$组
      • @套餐##
    • @分装##
    • @作者姓名
    • @版本SVN:$Id:Builder.php 6401 2009-09-24 16:12:04Z Guillermeblanco$ */
}在
$p->save()之前,拥有
$p->users\u id=$u->id

另外,为什么要调用表用户(复数)?这只会让代码变得非常混乱


同样,如果您在sfGuardPlugin中看到了不同的用户信息(用户和用户配置文件),所以您有不同的表来保存这些信息,请不要这样做!这是一个荒谬的想法,只会让你以后后悔。我不知道Symfony/Doctrine团队想到这个想法时喝了什么…

给这只猫剥皮有很多方法。我创建了一个简化的示例,大致基于从您的问题中收集到的信息

首先,这里是我用来为一对一模型类生成的YAML:

Identity:
  columns:
    username: string(50)
    password: string(50)
    email: string(50)

Profile:
  columns:
    identity_id: integer(10)
    firstname: string(50)
    lastname: string(50)
  relations:
    Identity:
      foreignType: one
现在,在PHP中,我可以创建一个新身份(或您的用户),并通过以下方式添加相关的配置文件数据:

        $identity = new Identity();
        $identity->username = 'james';
        $identity->password = 'secret';
        $identity->email = 'james@bond.com';
        //now adding the related data
        $identity->Profile->firstname = 'james';
        $identity->Profile->lastname = 'bond';
        $identity->save();
希望这个例子能对你有所帮助

编辑:

以下是从YAML生成的类,以防也有帮助:

BaseIdentity.php

<?php
abstract class BaseIdentity extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('identity');
        $this->hasColumn('username', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
        $this->hasColumn('password', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
        $this->hasColumn('email', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasOne('Profile', array(
             'local' => 'id',
             'foreign' => 'identity_id'));
    }
}

你能提供你的条令模型(PHP或YAML)来帮助回答它应该回答的问题吗。我必须仔细检查一下你的代码,看看还有什么发生。你能为你的模特弹出YAML吗这比实际的PHP生成的类要容易一点,我会试试的!我以前从未生成过YAML文件。有没有简单的方法?另外,我不在mo的电脑旁,所以可能会有延迟!我在尝试将我的模型转换为带有条令的yaml时遇到了许可问题。我假设Doctrine将尝试在web根目录中保存新文件,所以我不明白为什么不允许这样做!有点奏效了。。。错误消失,但配置文件表中的users\u id列的值为0,而不是users.id。至于您的其他建议,该代码实际上基于codeigniter的ion_auth库。这张桌子叫“用户”,因为我没有时间去更改它。我已经分离了数据,以使auth所需的数据与用户的常规数据分开。将概要文件存储在单独的表中不只是基本的数据库规范化吗?为什么这很可笑?
Identity:
  columns:
    username: string(50)
    password: string(50)
    email: string(50)

Profile:
  columns:
    identity_id: integer(10)
    firstname: string(50)
    lastname: string(50)
  relations:
    Identity:
      foreignType: one
        $identity = new Identity();
        $identity->username = 'james';
        $identity->password = 'secret';
        $identity->email = 'james@bond.com';
        //now adding the related data
        $identity->Profile->firstname = 'james';
        $identity->Profile->lastname = 'bond';
        $identity->save();
<?php
abstract class BaseIdentity extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('identity');
        $this->hasColumn('username', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
        $this->hasColumn('password', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
        $this->hasColumn('email', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasOne('Profile', array(
             'local' => 'id',
             'foreign' => 'identity_id'));
    }
}
<?php

abstract class BaseProfile extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->setTableName('profile');
        $this->hasColumn('identity_id', 'integer', 10, array(
             'type' => 'integer',
             'length' => '10',
             ));
        $this->hasColumn('firstname', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
        $this->hasColumn('lastname', 'string', 50, array(
             'type' => 'string',
             'length' => '50',
             ));
    }

    public function setUp()
    {
        parent::setUp();
        $this->hasOne('Identity', array(
             'local' => 'identity_id',
             'foreign' => 'id'));
    }
}