1

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

/控制器.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,
        )
    );
}
4

2 回答 2

0

我可以看到 $item 变量吗?

我认为你需要做这样的事情:

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

因为您只是将一些属性设置为 Timeline 并且 Calendar 项目为空,这就是为什么您将 calendar_id 设置为 0。您在日历表上有一个新项目吗?

于 2013-12-13T09:56:35.177 回答
0

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

$model = \Timeline\Model_Timeline::forge($timeline_properties);
$model->calendar = \Timeline\Model_Calendar::forge($calendar_properties);
$model->save();
于 2013-12-17T20:47:21.123 回答