php - cakephp:使用一种形式保存到多个模型

标签 php cakephp error-handling cakephp-2.4 cakephp-model

我有一种形式可以保存一个具有许多部件和多种颜色的人,但它不保存部件和颜色;只救人(主模型)。

在我的列表中 View ,脚本的一部分:

echo $this->Form->input('Person.person_name', array('required' => true, 'placeholder' => 'Name Your Person', 'label' => false) );
echo $this->Form->input('Part.0.part_name', array('required' => true, 'placeholder' => 'Add Part', 'label' => false) ); 
echo $this->Form->input('Color.0.color_name', array('required' => true, 'placeholder' => 'Enter a Color', 'label' => false) ); 

在我的列表 Controller 上:

if ($this->request->is('post')) {
            $created = $this->Person->saveAll($this->request->data, array('validate'=>'first'));
            if (!empty($created)) {
                $this->Session->setFlash(__('The person and his/her parts and colors have been saved.'));
                return $this->redirect(array('action' => 'index'));                 
            } else {
                $this->Session->setFlash(__('The person could not be saved. Please, try again.'));
            }
        }

在我的人物模型上:

public $hasMany = array(
    'PersonParts' => array(
        'className' => 'Part',
        'foreignKey' => 'part_person_id'
    ),
    'PersonColors' => array(
        'className' => 'Color',
        'foreignKey' => 'color_person_id'
    )
);

尝试将 saveAll 替换为 saveAssociated 但结果是一样的。 我在这里做错了什么?非常感谢任何帮助。

最佳答案

您必须在表单中添加别名的原始名称(如 hasMany 变量中的别名):

echo $this->Form->input('Person.person_name');
echo $this->Form->input('PersonParts.0.part_name'); 
echo $this->Form->input('PersonColors.0.color_name');

关于php - cakephp:使用一种形式保存到多个模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20741415/

相关文章:

html - cakephp 2.x 如何包含来自外部模板 css 的文件

python-3.x - 找到错误并修复。该程序将查找数组中3的倍数之和

php - Laravel 迁移部署

PHPExcel 将 xls DATETIME 更改为数字

php - 我应该使用什么 SQL 语句来显示另一个表中的正确 ID

mysql - CakePHP:虚拟字段可以在查找中使用吗?

php - cakephp 调试信息意外出现

error-handling - 如何处理 Read::read_to_end 方法的错误?

php - 简单的 PHP 错误

php - MySQL 正则表达式 LIKE