递归与Yii有很多很多关系

时间:2014-05-24 19:07:58

标签: yii many-to-many gii yii-cactiverecord cactiverecord

我与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
)

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

Objective.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>

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

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