与Yii的递归多对多关系

与Yii的递归多对多关系,yii,many-to-many,gii,yii-cactiverecord,cactiverecord,Yii,Many To Many,Gii,Yii Cactiverecord,Cactiverecord,我与目标之间存在递归多对多关系。我的关系模型是: 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.

我与
目标
之间存在递归多对多关系。我的关系模型是:

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生成了我的模型,如下所示:

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'),
    );
}
<div class="row">
    <?= $form->checkBoxList($model,'parents', 
                            CHtml::listData(Objective::model()->findAll(array('order' => 'id')), 'id', 'name')
            ) ?>
</div>
目标/\u form.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'),
    );
}
<div class="row">
    <?= $form->checkBoxList($model,'parents', 
                            CHtml::listData(Objective::model()->findAll(array('order' => 'id')), 'id', 'name')
            ) ?>
</div>

在我提交表单时的视图中,不保存RelatedObjectors表中的关系


我做错了什么?

相关模型不会与父模型一起保存。您必须在父级
beforeSave()
afterSave()
钩子中手动保存它们。还有一些扩展可以自动完成这项工作。查找有关该主题的一些信息