0

Kohana ORM模型之间有如下关系:

  1. has_one
  2. 有很多
  3. has_many_through

例如,我定义了以下内容:

class Model_ORM_Text extends ORM {

    protected $_has_one = array(
        'compiledData' => array('model' => 'CompiledText', 'foreign_key' => 'idText'),
    );

    protected $_has_many = array(
        'translations' => array('model' => 'TextTranslation', 'foreign_key' => 'idText')
    );

    protected $_has_many_through = array(
        'tags' => array('model' => 'TextTranslation', 'through' => 'rl_text_tags')
    );
}

我需要为这些关系中的每一个创建一个新的相关模型。我只addORM类中找到了允许添加通过has_many_through关系链接的相关模型的方法,如下所示:

$text->add("tags", $tagId);

但是我在任何地方都找不到如何为has_one简单has_many关系添加相关模型。可能吗?

4

1 回答 1

1

问题的关键在于,在每个has_manyand的“另一面”has_one都是一个belongs_to. 这是保存信息的模型。

在您的情况下Model_CompiledText,有列idText(在特定别名下)。要(取消)设置关系,您需要操作此字段。假设您belongs_to在 name 下有一个text,这就是您的操作方式:

$compiledText = ORM::factory('CompiledText');

// set text
// Version 1
$compiledText->text = ORM::factory('Text', $specificId);
// Version 2
$compiledText->text = ORM::factory('Text')->where('specificColumn', '=', 'specificValue')
    ->find();

// unset text
$compiledText->text = null

// save
$compiledText->save();

在 a 的情况下,has_one您可以直接通过父级访问它,因此也可以

$text->compiledData->text = ...;
于 2015-02-17T21:53:46.817 回答