通过保存和错误 array_merge() 陷入 HasMany

问题描述 投票:0回答:1

我正在尝试建立一个非常简单的网站来创建和编辑食谱(法语为“Recettes”)。

我是一名经验丰富的前端开发人员,对 php 有很好的了解(不是高级),并且我过去一直与开发团队一起开发 CakePhp。我正在学习从头开始一个项目的基础,这确实不是那么容易。 我已经建立了一个项目、数据库(Ingredients、Recette 和 IngredientRecette)并使用 Bake 生成大部分代码。

我的 3 个模型有一个有效的 CRUD 工作流程,唯一要做的就是能够从 Recette/add 页面添加成分,但我被卡住了。我按照cake php网站上的说明进行操作(http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#hasmany-through-the-join-model)和其他网站(因为 cakephp 文档的帮助确实没有那么有用),我相信我已经设置了正确的 Hasmany Through 关系。 但是 saveAll 函数不会在连接模型 (IngredientRecette) 中保存任何内容,并且在我成功解决另一个花了我几个小时才解决的错误后,我遇到了一个特定错误(见下文)! 所以我有一种感觉,我已经检查过、双重检查过、三次检查过我代码中的所有内容,并且我已经阅读了论坛等上的每个问题和答案......

我被这个问题困扰了,也许有一些明显的我没有想到的东西,或者也许并且绝对不明白 Cakephp 到底是如何表达的:( 无论如何,提前感谢那些能够就此提供建议或帮助的人:

这是我提交 /recettes/add 后收到的错误(我添加了一些表单元素以向我的 Recette 添加成分和数量):

警告 (2): array_merge() [function.array-merge]: 参数 #2 不是数组 [CORE\Cake\Model\Model.php, 第 2250 行]

这是我传递给控制器中的 saveAll() 方法的数组(来自 debug() 的输出):

    array(
    'Recette' => array(
    'auteur' => '7',
    'nom' => 'Nouvelle Recette',
    'cout' => 'Pas cher',
    'niveau' => '5',
    'tps_realisation' => '30 min',
        'nb_personnes' => '6',
        'description' => 'Description data',
        'remarque' => ''
    ),
    'AssociatedIngredient' => array(
        'id_ingredient' => '6',
        'quantite' => '70 cl',
        'id_recette' => '7654'
    )
)

这是我的控制器代码:

<?php
App::uses('AppController', 'Controller');
/**
 * Recettes Controller
 *
 * @property Recette $Recette
 */
class RecettesController extends AppController {


/**
 * index method
 *
 * @return void
 */
    public function index() {
        $this->Recette->recursive = 0;
        $this->set('recettes', $this->paginate());
    }

/**
 * view method
 *
 * @param string $id
 * @return void
 */
    public function view($id = null) {
        $this->Recette->id = $id;
        if (!$this->Recette->exists()) {
            throw new NotFoundException(__('Invalid recette'));
        }
        $this->set('recette', $this->Recette->read(null, $id));
    }

/**
 * add method
 *
 * @return void
 */
    public function add() {

        if ($this->request->is('post')) {
            debug($this->request->data);
            $this->Recette->create();
            if ($this->Recette->saveAll($this->request->data)) {
                $this->Session->setFlash(__('The recette has been saved'));
                //$this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The recette could not be saved. Please, try again.'));
            }
        }

        $this->loadModel('Ingredient');
        $liste_ingr = $this->Ingredient->find('all');

        $this->set('liste_ingr', $liste_ingr);
    }

IngredientRecette 模型

<?php
App::uses('AppModel', 'Model');
/**
 * Recette Model
 *
 * @property IngredientRecette $ingredient
 */
class Recette extends AppModel {
/**
 * Use database config
 *
 * @var string
 */
    public $useDbConfig = 'default';
/**
 * Use table
 *
 * @var mixed False or table name
 */
    public $useTable = 'recette';
/**
 * Primary key field
 *
 * @var string
 */
    public $primaryKey = 'id_recette';
/**
 * Display field
 *
 * @var string
 */
    public $displayField = 'nom';
    public $recursive = 2;


/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'id_recette' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'auteur' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'nom' => array(
            'notempty' => array(
                'rule' => array('notempty'),
            ),
        ),
        'niveau' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),
        'nb_personnes' => array(
            'numeric' => array(
                'rule' => array('numeric'),
            ),
        ),

    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'AssociatedIngredient' => array(
            'className' => 'IngredientRecette',
            'foreignKey' => 'id_recette',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

和 IngredientRecette 模型(连接模型)

<?php
App::uses('AppModel', 'Model');
/**
 * IngredientRecette Model
 *
 * @property ingredient $ingredient
 * @property recette $recette
 */
class IngredientRecette extends AppModel {
/**
 * Use database config
 *
 * @var string
 */
    public $useDbConfig = 'default';
/**
 * Use table
 *
 * @var mixed False or table name
 */
    public $useTable = 'ingredient_recette';
/**
 * Primary key field
 *
 * @var string
 */
    public $primaryKey = 'id_ingredient_recette';
    public $recursive = 2;

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'IngredientLiaison' => array(
            'className' => 'Ingredient',
            'foreignKey' => 'id_ingredient',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'RecetteLiaison' => array(
            'className' => 'Recette',
            'foreignKey' => 'id_recette',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );
}

所以连接模型中没有保存任何内容,我收到此警告...

任何帮助表示赞赏,当然,如果有任何不清楚的地方请告诉我! 非常感谢!!

php cakephp cakephp-2.0
1个回答
0
投票

我认为您的数据格式不正确。试试这个怎么样(注意我在关联成分中放入的额外数组)。由于菜谱有很多关联成分,因此关联成分的数据应该是一系列数组。

 array(
'Recette' => array(
'auteur' => '7',
'nom' => 'Nouvelle Recette',
'cout' => 'Pas cher',
'niveau' => '5',
'tps_realisation' => '30 min',
    'nb_personnes' => '6',
    'description' => 'Description data',
    'remarque' => ''
),
'AssociatedIngredient' => array( 
     array( 
        'id_ingredient' => '6',
        'quantite' => '70 cl',
        'id_recette' => '7654'
     ),
     array( /*ingredient 2 data*/ ),
   )
);
© www.soinside.com 2019 - 2024. All rights reserved.