0

我有一个递归的多对多关系objective。我的关系模型是:

CREATE TABLE master.objectives
(
  id serial NOT NULL,
  name character varying(100),
  CONSTRAINT pkey_objectives PRIMARY KEY (id),
  CONSTRAINT fk_objectives_perspective FOREIGN KEY (perspective_id)
      REFERENCES master.perspective (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

CREATE TABLE master.relatedobjectives
(
  parent integer NOT NULL,
  child integer NOT NULL,
  CONSTRAINT relatedobjectives_pkey PRIMARY KEY (parent, child),
  CONSTRAINT fk_child_objectives FOREIGN KEY (child)
      REFERENCES master.objectives (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_paret_objectives FOREIGN KEY (parent)
      REFERENCES master.objectives (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

我已经在 Yii 框架中使用 gii 生成了我的模型,如下所示:

目标.php

public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'parents' => array(self::HAS_MANY, 'Relatedobjectives', 'parent'),
        'children' => array(self::HAS_MANY, 'Relatedobjectives', 'child'),
    );
}

目标/_form.php

<div class="row">
    <?= $form->checkBoxList($model,'parents', 
                            CHtml::listData(Objective::model()->findAll(array('order' => 'id')), 'id', 'name')
            ) ?>
</div>

在我提交表单时的视图中,不保存相关目标表中的关系。

我究竟做错了什么?

4

1 回答 1

1

相关模型不会与父模型一起保存。您必须手动将它们保存在父母beforeSave()afterSave()挂钩中。还有一些扩展可以自动执行此操作。在此处查找有关该主题的一些信息。

于 2014-05-24T19:13:00.277 回答