3

我是蛋糕 PHP 新手,我有一些问题

共有三种模式:Guardian、Student 和 StudentFee。

关系如下所示:Guardian hasMany Student,Student hasMany StudentFee,StudentFee 属于 Guardian,Student。

我的问题是除了插入学生和监护人详细信息外,没有插入 StudentFee 详细信息。

我的模型就像

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() 如下代码

$this->Guardian->set($this->request->data);
$this->Guardian->saveAssociated($this->Guardian->data, array('validate' => false)); 

请帮助解决此问题,在此先感谢。

4

1 回答 1

0

对我来说正确的是:

楷模

// 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')
    );

您的数据(POST)可能是这样的:

[Guardian] => array(
),
['Student'] => array(
  [0] => array(
    ['StudentFee'] => array(
    )
  ),
  [1] => array(
    ['StudentFee'] => array(
    )
  )
)

所以,只需保存:

// use the deep option to save items after the second level
$this->Guardian->saveAssociated($this->request->data, array('deep' => true));

我希望这对你有帮助。请尝试,如果有效,请告诉我。

于 2013-09-24T20:00:53.397 回答