【问题标题】:cakephp hasmany form datacakephp有很多表单数据
【发布时间】:2012-11-15 10:55:28
【问题描述】:

我想为一个产品保存多个类别。

我有模型:

Category.php
public $hasAndBelongsToMany = array(
        'Product' => array(
            'className' => 'Product',
            'joinTable' => 'product_categories',
            'foreignKey' => 'category_id',
            'associationForeignKey' => 'product_id',
            'unique' => 'keepExisting',
        )
    );

Product.php
public $hasMany = array(
        'ProductCategory' => array(
            'className' => 'ProductCategory',
            'foreignKey' => 'product_id',
            'dependent' => false,

        ),

ProductCategory.php
public $belongsTo = array(
        'Product' => array(
            'className' => 'Product',
            'foreignKey' => 'product_id',

        ),
        'Category' => array(
            'className' => 'Category',
            'foreignKey' => 'category_id',
        )
    );

所以在产品/添加视图中,我通过以下方式添加了一组类别复选框:

echo $this->Form->input('ProductCategory.category_id',array(
        'label' => __('Category',true),
        'type' => 'select',
        'multiple' => 'checkbox',
        'options' => $categories
    )); 

但这会产生一组输入名称为:name="data[ProductCategory][category_id][]" 而不是 name="data[ProductCategory][0][category_id]" 递增的零。

如果它们的格式是模型和字段之间的键,那么我可以使用 saveAll() 吗? 因为它是我得到的格式,所以我必须操纵请求->数据以将其转换为我希望能够保存的形式()

我是否以正确的方式解决这个问题?或者我的模型设置不正确?

另外,编辑 hasMany 数据会发生什么?例如,如果我取消选中一个选项并添加另一个选项怎么办? cake 会在添加新记录之前自动删除所有关联记录吗?

编辑。

基本上我要问的是有没有更好或更快的方法来做到这一点,现在可行:

if ($this->Product->save($this->request->data)) {
    $this->Product->ProductCategory->deleteAll(array('ProductCategory.product_id' => $this->Product->id));
    foreach ($this->request->data['ProductCategory']['category_id'] as $cat_id) {
        $this->Product->ProductCategory->create();
        $this->Product->ProductCategory->set(array(
            'product_id' => $this->Product->id,
            'category_id' => $cat_id
        ));
        $this->Product->ProductCategory->save();
    }
}   

【问题讨论】:

    标签: cakephp


    【解决方案1】:

    在你的表单中迭代你希望沿线显示的许多

    for/while/do()
    {       
        $counter++
        $this->Form->text('Product.'.$counter.'.price');
        $this->Form->text('Product.'.$counter.'.description');
    
    }
    

    【讨论】:

    • 这是用于 cakephp 2.x 或 3.x 的?
    • 很久以前这是 2.x
    【解决方案2】:

    我会为此使用saveAll(),因为它旨在自动为您保存记录及其所有相关记录,假设您的 id 在数据中并且格式正确。

    http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveall-array-data-null-array-options-array

    【讨论】:

    • 据我了解,数据必须采用以下格式:[Model][0][fieldname] 以便 saveAll() 处理多条记录。如果我只能让 FormHelper 以这种方式输出复选框输入...
    • 是的。对不起,你为什么不试试呢?这种声音不像是现场环境,所以只需清空数据库表,添加产品,单击一些类别,点击提交并查看数据库。如果它不适用于您的代码,请查看上面提供的文档链接。无论如何,在使用 cakephp 进行开发之前,您至少应该阅读过该部分。它对于您使用任何 cakephp 应用程序所做的 80% 以上的工作都是必不可少的。
    • 如果您对自己的视图有疑问,请烘焙一些测试视图,看看 Cake 将如何形成代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-18
    • 2015-08-01
    • 2010-10-16
    相关资源
    最近更新 更多