1

每个实体都被保存并有一个 ai id。只有第 34 行的测试没有通过。

<?php
class AssignmentTest extends TestCase
{
    protected $assignment;

    public function setUp()
    {
        parent::setUp();
        $this->assignment = new Assignment;
    }

    public function testAssignmentAssociations()
    {
        $facility = Facility::create(array(
        'name' => 'test facility',
        'code' => 'test-facility'
        ));

        $user = new User;
        $user->id = 1;

        $shift = Shift::create(array(
        'name' => 'test shift',
        'start_time' => 0,
        'end_time' => 600
        ));

        $this->assignment->facilities()->save($facility);
        $this->assignment->shift()->associate($shift);
        $this->assignment->user()->associate($user);
        $this->assignment->save();

        $this->assertEquals(1, $facility->id);
        $this->assertEquals(1, $this->assignment->facilities()->count());
        $this->assertEquals(1, $this->assignment->shift_id);
        $this->assertEquals(1, $this->assignment->user_id);
    }
}

// Facility model
public function assignments()
{
    return $this->belongsToMany('Assignment', 'assignment_facilities');
}

// Assignment model
public function facilities()
{
    return $this->belongsToMany('Facility', 'assignment_facilities');
}

编辑

显然删除此行将使测试成功。现在我很感兴趣,为什么在保存分配时关联将不再返回成功计数?

<?php
$this->assignment->save();
4

1 回答 1

0

我还没有测试过——但在你尝试将它们与作业相关联之前,你不需要自己“保存”班次和用户记录吗?

    $user = new User;
    $user->id = 1;

    $shift = Shift::create(array(
    'name' => 'test shift',
    'start_time' => 0,
    'end_time' => 600
    ));
    $shift->id = 1;


    $this->assignment->facilities()->save($facility);
    $this->assignment->shift()->save($shift);
    $this->assignment->user()->save($user);
    $this->assignment->shift()->associate($shift);
    $this->assignment->user()->associate($user);
    $this->assignment->save();
于 2013-07-15T04:33:29.210 回答