【问题标题】:Kohana 3.2 has_one model not loadedKohana 3.2 has_one 模型未加载
【发布时间】:2012-05-23 15:41:07
【问题描述】:

我有 2 张桌子:

Profiles
------
id

Stats
------
profile_id
v1
v2

和 2 个模型:

Profile_Model
$_has_one = array( 'stat' => array() );

Stat_Model
$_belongs_to = array( 'profile' => array() );

我愿意:

$profile = ORM::Factory( 'profile', $id );
$profile->stat->v1 = 123;
$profile->stat->save();

我希望更新或创建具有 profile_id = $id 的 Stats 行。相反,它总是尝试插入记录,即使它存在(因为它认为该记录未加载)。

我该如何解决?

【问题讨论】:

    标签: kohana-orm kohana-3.2


    【解决方案1】:

    您应该按照 Kohana 的方式将 id 添加到 Stats 表中,或者在 Model_Profile 定义中指定外键:

    $_has_one = array('stat' => array('foreign_key' => 'profile_id'));
    

    【讨论】:

    • 谢谢!不过,在将 Stats 中的 PK 重命名为 id 后,我不得不在两个模型中使用“id”外键。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多