FuelPHP ORM一对一关系不能保存?

时间:2013-12-06 21:32:31

标签: php fuelphp fuelphp-orm

我正在尝试执行以下操作,该数组包含一些应保存到时间轴表中的数据,其余数据应保存到日历表中。但是,在正确更新时间轴时,只将默认值或空数据保存到日历中,calendar_id字段设置为0.

/controller.php

$model = \Timeline\Model_Timeline::forge();
$model->calendar = \Timeline\Model_Calendar::forge();
$model->set($item);
$model->save();

/model/timeline.php

namespace Timeline;

class Model_Timeline extends \Orm\Model
{
    protected static $_properties = array(
        'id',
        'user_id' => array(
            'data_type' => 'int',
            'form' => array('type' => false),
        ),
        'calendar_id' => array(
            'data_type' => 'int',
            'form' => array('type' => false),
            'default' => '0'
        ),
        'created_at' => array(
            'data_type' => 'int',
            'form' => array('type' => false),
            'default' => '0'
        ),
        'updated_at' => array(
            'data_type' => 'int',
            'form' => array('type' => false),
            'default' => '0'
        )
    );

    protected static $_has_one = array(
        'calendar' => array(
            'key_from' => 'calendar_id',
            'model_to' => 'Timeline\\Model_Calendar',
            'key_to' => 'id',
            'cascade_save' => true,
            'cascade_delete' => true,
        )
    );

/model/calendar.php

namespace Timeline;

class Model_Calendar extends \Orm\Model
{
    protected static $_properties = array(
        'id',
        'start',
        'end',
        'recurrenceRule',
        'recurrenceException',
        'isAllDay',
        'created_at',           
        'updated_at'
    );

    protected static $_belongs_to = array(
        'calendar' => array(
            'key_from' => 'id',
            'model_to' => 'Timeline\\Model_Timeline',
            'key_to' => 'calendar_id',
            'cascade_save' => true,
            'cascade_delete' => true,
        )
    );
}

2 个答案:

答案 0 :(得分:0)

我可以看到$ item变量。

我认为您需要做类似的事情:

$model = \Timeline\Model_Timeline::forge();
$model->calendar = \Timeline\Model_Calendar::forge();
$model->calendar->set($calendar_properties);
$model->set($timeline_properties);
$model->save();

因为您只是将一些属性设置为时间轴并且日历项目为空,这就是您将calendar_id设置为0的原因。您是否在日历表上有新项目?

答案 1 :(得分:0)

假设这些变量是列数组=>值,你可以直接做

$model = \Timeline\Model_Timeline::forge($timeline_properties);
$model->calendar = \Timeline\Model_Calendar::forge($calendar_properties);
$model->save();