【发布时间】:2011-04-08 05:45:30
【问题描述】:
我有两张桌子。一个用户表和一个配置文件表。 profile 表有一个外键 users_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 表中,那么我知道它是 auto_incrementing 并生成一个新的 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;
以下是模型:
/**
* BaseUsers
*
* This class has been auto-generated by the Doctrine ORM Framework
- @property 整数 $id
- @property 字符串 $username
- @property 字符串 $password
- @property 字符串 $ip_address
- @property 日期 $created_at
- @property 日期 $updated_at
- @property 字符串 $salt
- @property 字符串 $email
- @property 字符串 $activation_code
- @property 字符串 $forgotten_password_code
- @property 字符串 $remember_code
- @property 整数 $last_login
- @property 整数 $active
- @property 整数 $groups_id
- @property 组 $Groups
- @package ##PACKAGE##
- @subpackage ##SUBPACKAGE##
- @author ##NAME##
- @version SVN: $Id: Builder.php 6401 2009-09-24 16:12:04Z guilhermeblanco $ */
abstract class BaseUsers extends Doctrine_Record
{
public function setTableDefinition()
{
$this->actAs("Timestampable");
$this->setTableName('users');
$this->hasColumn('id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'unsigned' => 0,
'primary' => true,
'autoincrement' => true,
));
$this->hasColumn('username', 'string', 45, array(
'type' => 'string',
'length' => 45,
'fixed' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('password', 'string', 45, array(
'type' => 'string',
'length' => 45,
'fixed' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('ip_address', 'string', 16, array(
'type' => 'string',
'length' => 16,
'fixed' => true,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('created_at', 'date', null, array(
'type' => 'date',
'primary' => false,
'notnull' => true,
'autoincrement' => false,
));
$this->hasColumn('updated_at', 'date', null, array(
'type' => 'date',
'primary' => false,
'notnull' => true,
'autoincrement' => false,
));
$this->hasColumn('salt', 'string', 40, array(
'type' => 'string',
'length' => 40,
'fixed' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('email', 'string', 40, array(
'type' => 'string',
'length' => 40,
'fixed' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('activation_code', 'string', 40, array(
'type' => 'string',
'length' => 40,
'fixed' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('forgotten_password_code', 'string', 40, array(
'type' => 'string',
'length' => 40,
'fixed' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('remember_code', 'string', 40, array(
'type' => 'string',
'length' => 40,
'fixed' => false,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('last_login', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'unsigned' => 1,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('active', 'integer', 1, array(
'type' => 'integer',
'length' => 1,
'unsigned' => 1,
'primary' => false,
'notnull' => false,
'autoincrement' => false,
));
$this->hasColumn('groups_id', 'integer', 4, array(
'type' => 'integer',
'length' => 4,
'unsigned' => 1,
'primary' => true,
'autoincrement' => false,
));
}
public function setUp()
{
parent::setUp();
$this->hasOne('Groups', array(
'local' => 'groups_id',
'foreign' => 'id'));
$this->hasOne('Profile', array(
'local' => 'id',
'foreign' => 'users_id'));
}
}
<?php
/** * 基本配置文件 * * 这个类是由 Doctrine ORM 框架自动生成的 * * @property 整数 $id * @property 字符串 $role * @property 字符串 $first * @property 字符串 $last * @property 字符串 $email * @property 字符串 $phone_1 * @property 字符串 $phone_2 * @property 字符串 $address * @property 字符串 $postcode * @property 日期 $created_at * @属性日期 $updated_at * @property Doctrine_Collection $Member * @property Doctrine_Collection $Post * @property Doctrine_Collection $ThreadHasProfile * @property Doctrine_Collection $UserSetting * * @package ##PACKAGE## * @subpackage ##SUBPACKAGE## * @author ##NAME## * @version SVN: $Id: Builder.php 6401 2009-09-24 16:12:04Z guilhermeblanco $ */ 抽象类 BaseProfile 扩展 Doctrine_Record { 公共函数 setTableDefinition() { $this->actAs("时间戳"); $this->setTableName('profile'); $this->hasColumn('id', 'integer', 4, array( '类型' => '整数', '长度' => 4, '无符号' => 0, '主要' => 真, '自动增量' => 真, )); $this->hasColumn('users_id', 'integer', 4, array( '类型' => '整数', '长度' => 4, '无符号' => 0, '主要' => 假, '自动增量' => 假, )); $this->hasColumn('role', 'string', null, array( '类型' => '字符串', '固定' => 假, '主要' => 假, 'notnull' => 真, '自动增量' => 假, )); $this->hasColumn('first', 'string', 45, array( '类型' => '字符串', '长度' => 45, '固定' => 假, '主要' => 假, 'notnull' => 真, '自动增量' => 假, )); $this->hasColumn('last', 'string', 45, array( '类型' => '字符串', '长度' => 45, '固定' => 假, '主要' => 假, 'notnull' => 真, '自动增量' => 假, )); $this->hasColumn('phone_1', 'string', 45, array( '类型' => '字符串', '长度' => 45, '固定' => 假, '主要' => 假, 'notnull' => 假, '自动增量' => 假, )); $this->hasColumn('phone_2', 'string', 45, array( '类型' => '字符串', '长度' => 45, '固定' => 假, '主要' => 假, 'notnull' => 假, '自动增量' => 假, )); $this->hasColumn('address', 'string', 200, array( '类型' => '字符串', '长度' => 200, '固定' => 假, '主要' => 假, 'notnull' => 假, '自动增量' => 假, )); $this->hasColumn('postcode', 'string', 10, array( '类型' => '字符串', '长度' => 10, '固定' => 假, '主要' => 假, 'notnull' => 假, '自动增量' => 假, )); $this->hasColumn('created_at', 'date', null, array( '类型' => '日期', '主要' => 假, 'notnull' => 真, '自动增量' => 假, )); $this->hasColumn('updated_at', 'date', null, array( '类型' => '日期', '主要' => 假, 'notnull' => 真, '自动增量' => 假, )); }
public function setUp()
{
parent::setUp();
$this->hasMany('Member', array(
'local' => 'id',
'foreign' => 'profile_id'));
$this->hasMany('Post', array(
'local' => 'id',
'foreign' => 'profile_id'));
$this->hasMany('ThreadHasProfile', array(
'local' => 'id',
'foreign' => 'profile_id'));
$this->hasMany('UserSetting', array(
'local' => 'id',
'foreign' => 'profile_id'));
$this->hasOne('Users', array(
'local' => 'users_id',
'foreign' => 'id'));
}
}
【问题讨论】:
-
您能否提供您的学说模型(PHP 或 YAML 格式)以帮助回答问题
标签: doctrine models foreign-key-relationship