首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CakePHP -复缔合

CakePHP -复缔合
EN

Stack Overflow用户
提问于 2013-08-22 14:46:37
回答 1查看 117关注 0票数 3

我刚开始接触PHP,我有一些问题

有三种模式:守护者,学生和StudentFee。

的关系看起来像:守护者hasMany学生,学生hasMany StudentFee,StudentFee belongsTo守护者,学生。

,我的问题是,的StudentFee细节不是被插入的,而是插入了学生和监护人的详细信息。

我的模特就像

代码语言:javascript
复制
class Guardian extends AppModel {
    public $name = 'Guardian';

    public $recursive =2;
    public $hasMany = array(
            'Student' => array(
                'className'     => 'Student',
                'dependent'     => true
            )
    );

}

class Student extends AppModel {
    public $name = 'Student';

        public $hasMany = array(
            'StudentFee' => array(
                'className'     => 'StudentFee',
                'dependent'     => true
            )

    );
}

class StudentFee extends AppModel {
    public $name = 'StudentFee';
    public $belongsTo = array(
                    'Guardian' => array(
                    'className'     => 'Guardian',
                    'dependent'     => true
            ),
                'Student' => array(
                    'className'     => 'Student',
                    'dependent'     => true
            )
    );
}

在我的控制器中,我使用了saveAssociated(),如下所示

代码语言:javascript
复制
$this->Guardian->set($this->request->data);
$this->Guardian->saveAssociated($this->Guardian->data, array('validate' => false)); 

请帮忙解决这个问题,谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-24 20:00:53

对我来说正确的是:

模型

代码语言:javascript
复制
// Guardian
    $hasMany = array(
      'Student' => array('clasName' => 'Student', 'foreignKey' => 'guardian_id')
    );

// Student
    $belongsTo = array(
      'Guardian' => array('clasName' => 'Guardian', 'foreignKey' => 'guardian_id')
    );

    $hasMany = array(
      'StudentFee' => array('clasName' => 'StudentFee', 'foreignKey' => 'student_id')
    );

// StudentFee
    $belongsTo = array(
      'Student' => array('clasName' => 'Student', 'foreignKey' => 'student_id')
    );

你的数据(帖子),也许是这样的:

代码语言:javascript
复制
[Guardian] => array(
),
['Student'] => array(
  [0] => array(
    ['StudentFee'] => array(
    )
  ),
  [1] => array(
    ['StudentFee'] => array(
    )
  )
)

所以,只需保存:

代码语言:javascript
复制
// use the deep option to save items after the second level
$this->Guardian->saveAssociated($this->request->data, array('deep' => true));

希望这能帮到你。请试一试,如果有效,请告诉我。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18383755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档