【问题标题】:CakePhp $hasAndBelongsToMany not saving multiple select items as expectedCakePhp $hasAndBelongsToMany 未按预期保存多个选择项
【发布时间】:2014-01-27 19:45:23
【问题描述】:

我有以下代码设置(为简洁起见)

class BasePackage extends AppModel {
    public $name = 'BasePackage';
    public $hasAndBelongsToMany = array('ProductSubtype', 'ProductType');
}

class ProductType extends AppModel {
    public $name = 'ProductType';
}

class ProductSubtype extends AppModel {
    public $name = 'ProductSubtype';    
}

以上是简单的模型类。

/*  tables in database  */
base_packages
product_types
product_subtypes
base_packages_product_types
base_packages_product_subtypes

第一个表是用户使用表单创建的主包,product_* 表预先加载了适当的类型和子类型(它们不会经常更改),最后两个是 CakePhp 的 Join 表想拥有

/*  in BasePackage/add.ctp  */
//  ...
<ul class="nwblock">
    <li>
    <?php 
        echo $this->Form->input('ProductType.product_type_id', array(
            'label' => 'Choose Product Type',
            'type'  => 'select',
            'class' => 'form-control',
            'style' => 'width:300px; margin-bottom:20px;',
            'options' => $protypes
        ));
    ?>
    </li>
</ul>
<ul class="nwblock">
    <li>
    <?php 
        echo $this->Form->input('ProductSubtype.product_subtype_id', array(
            'label' => 'Choose Subtype(s)',
            'multiple' => 'multiple',
            'type'  => 'select',
            'class' => 'form-control',
            'style' => 'width:300px;height:390px;margin-bottom:20px;',
            'options' => $subtypes
        ));
    ?>
    </li>
</ul>
//  ...

我们在上面看到了从 product_* 表中加载的两个控件。类型是单选下拉列表,子类型是多选列表。

/*  in BasePackageController.php    */
public function add() {
    $protypes = $this->BasePackage->ProductType->find('list', 
            array('fields' => array('ProductType.id', 'ProductType.display')));
    $subtypes = $this->BasePackage->ProductSubtype->find('list', 
            array('fields' => array('ProductSubtype.id', 'ProductSubtype.display')));
    $this->set('protypes', $protypes);
    $this->set('subtypes', $subtypes);

    if ($this->request->is('post')) {
        $this->BasePackage->create();           
        if (!empty($this->request->data)) {
            $this->BasePackage->saveAll($this->request->data, array('deep' => true));
        }
    }
}

过程如下,当用户创建一个新的 BasePackage 时,他们从下拉框中选择一个 ProductType,并从一个多选列表中选择一对多的 ProductSubtype。当调用 $this->BasePackage->saveAll() 时,将正确插入要插入到 base_packages 和 base_packages_product_types 表中的数据。但是,base_packages_product_subtypes 表保持不变。

更新:

如果我从表单->输入选项中删除'multiple' =&gt; 'multiple',,代码会同时保存产品类型和产品子类型(如预期的那样)。这显然是不够的,因为我需要保存一对多。有人知道如何激活 HABTM 的“Many”部分吗?

【问题讨论】:

    标签: cakephp associations multi-select has-and-belongs-to-many


    【解决方案1】:

    对我来说BasePackage &lt;&gt; ProductType 看起来更像是一个多对一的关系,即BasePackage belongsTo ProductType

    无论如何...请遵循食谱中描述的约定:

    http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-habtm

    表单助手应该输入模型名称,即ProductSubtype,而视图变量应该是驼峰式复数形式,即productSubtypes,这样CakePHP 会自动为您完成剩下的工作。

    public function add() {
        // ...
        $this->set('productSubtypes', $subtypes);
        // ...
    }
    
    echo $this->Form->input('ProductSubtype', array(
        'label' => 'Choose Subtype(s)',
        'class' => 'form-control',
        'style' => 'width:300px;height:390px;margin-bottom:20px;'
    ));
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 2011-06-24
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2017-09-18
      • 2021-12-20
      相关资源
      最近更新 更多