我在以一对多关系插入对象时遇到问题。这是我的代码:
/**
* Migrations
**/
// Create user__groups table
Schema::create('user__groups', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 50);
});
// Create users table
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->timestamps(); // created_at, updated_at DATETIME
$table->string('username', 40);
$table->string('password', 20);
$table->integer('user_group_id')->unsigned();
$table->foreign('user_group_id')->references('id')->on('user__groups');
});
/**
* Models
**/
class User extends Ardent {
protected $table = 'users';
public $timestamps = true;
protected $hidden = array('password');
public $autoPurgeRedundantAttributes = true;
protected $fillable = array(*);
// Validation rules for fields in this entity
public static $rules = array(
'username' => 'required|unique:users',
'password' => 'required|alpha_dash|min:6|max:20|confirmed',
'password_confirmation' => 'required|alpha_dash|min:6|max:20'
);
// Relations
public static $relationsData = array(
'userGroup' => array(self::BELONGS_TO, 'UserGroup')
);
// Model mock data for test purposes
public static $factory = array(
'username' => 'string',
'password' => '123123',
'password_confirmation' => '123123',
'user_group_id' => 'factory|UserGroup'
);
}
class UserGroup extends Ardent {
protected $table = 'user__groups';
public $timestamps = false;
public $autoPurgeRedundantAttributes = true;
protected $fillable = array('*');
// Validation rules for fields in this entity
public static $rules = array(
'name' => 'required|unique:user__groups|alpha_dash'
);
// Relations
public static $relationsData = array(
'users' => array(self::HAS_MANY, 'User')
);
// Model mock data for test purposes
public static $factory = array(
'name' => 'string'
);
}
PHP单元测试
public function test_assignUserToGroup() {
/* @var $user User */
$user = FactoryMuff::instance('User');
// Test assigning user to group 1
$group1 = FactoryMuff::create('UserGroup');
$this->assertTrue($group1->users()->save($user) !== false, "User model did not save!".$user->errors());
// Test assigning user to group 2 (this fails)
$group2 = FactoryMuff::create('UserGroup');
$this->assertTrue($group2->users()->save($user) !== false, "User model did not update!".$user->errors()); // <-- The save method always returns false
}
测试运行将反映用户对象不会被更新。为什么?我究竟做错了什么?我希望下面的代码// Test assigning user to group 2
对现有对象执行更新,但DB::getQueryLog()
只显示选择和插入。这真的很烦人。
- 编辑 -
实际上是验证阻止了我。我在上面的测试中添加了对 Model->errors() 的调用。这指出保存后,包含在 User 对象中的用户名不再是唯一的 - 因为它发现自己在数据库中(WTF?)。它还说 password_confirmation 字段被标记为必需,但为空 - 因为它在上次保存时被删除。
这只是愚蠢的,我不知道是Eloquent,Ardent还是我的错。如果它在保存前有效,那么它在保存后也应该有效 - 对吧?我将更改问题的标题以反映问题的真正原因。