【问题标题】:cakePHP saving data for first time (hasMany relationship)cakePHP第一次保存数据(有很多关系)
【发布时间】:2013-01-17 06:43:36
【问题描述】:

好吧,我正在尝试包含 hasMany 关系的第一个 saveAll(或 saveMany 或 saveAssociated 不确定哪个是“正确的”),但我似乎无法弄清楚 cake 需要这些数据的方式。我可以更新主表上的 name 字段,但无法更新 hasMany 关系。

我只是在寻找一个正确的方向,我渴望学习,但是用我的头在墙上挖一个洞的想法看起来越来越诱人。

只是一点背景我有两个表“供应商”表和“vendor_payment_types”表。 “vendors”表有很多“vendor_payment_types。结构如下:

供应商表 id、company_id、姓名

vendor_payment_types id、vendor_id、类型

目前我发送给控制器的数组是(如果支付类型有一个 id 表示它已经存在于数据库中并且不需要发生任何事情,如果没有 id 它需要添加,第三部分是如果它存在于数据库中但不在此数组中,则需要从数据库中删除它):

Array
(
[0] => Array
    (
        [Vendor] => Array
            (
                [id] => 7
                [company_id] => 2
                [name] => Test Vendor
                [VendorPaymentTypes] => Array
                    (
                        [0] => Array
                            (
                                [vendor_id] => 7
                                [id] => 13
                                [type] => payable
                            )

                        [1] => Array
                            (
                                [vendor_id] => 7
                                [type] => check
                            )

                        [2] => Array
                            (
                                [vendor_id] => 7
                                [id] => 14
                                [type] => cash
                            )

                    )

            )

    )

我需要做的是 a) 更新名称 b) 添加任何新的 VendorTypes c) 删除任何不在 POST'ed 数组中但存在于数据库中的 VendorTypes。以下是我的 VendorController 中的内容:

public function save() {
   $vendors = array();
    $companyid = $this->Auth->user('company_id');
    $needle = 'new';
    $i = 0;

    // Setup array and also if id of payment_type includes "new" unset the id so cake will add a new and not try and match id and update

    if ($this->request->is('post')) {
        foreach ($this->request->data['Vendor'] as $vendorId => $vendor) {
            $vendors[$vendorId]['Vendor']['id'] = $vendorId;
            $vendors[$vendorId]['Vendor']['company_id'] = $companyid;
            $vendors[$vendorId]['Vendor']['name'] = $vendor['name'];
            foreach ($vendor['VendorTypes'] as $type => $val) {
                if ($val != '0') {
                    $vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['vendor_id'] = $vendorId;
                    $vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['id'] = $val;
                    $vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['type'] = $type;

                    if (strstr($vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['id'], $needle)) {
                        unset($vendors[$vendorId]['Vendor']['VendorPaymentTypes'][$i]['id']);
                    }
                    $i++;
                }
            }
        }

        // Clean up array to make it an array starting at 0 and not based on vendor['id']

        $cleanedarr = array();
        foreach ($vendors as $vendor => $data) {
            $cleanedarr[] = $data;
        }

        // Currently this only updates the name

        if ($this->Vendor->saveAll($cleanedarr, array('deep' => TRUE) )) {
            $this->Session->setFlash('Updated Vendors!');
            die(pr($this->Vendor->getDataSource()->getLog()));
        } else {
            $this->Session->setFlash('Failed to Update');
        }
    }
}

供应商模型设置为

public $name = 'Vendor';

public $hasMany = array(
    'VendorPaymentTypes' => array(
        'className' => 'VendorPaymentTypes',
        'foreignKey' => 'vendor_id',
        'dependent' => true
    )
);

我什至还没有开始处理删除部分,因为我无法添加新的 vendor_types。在无数次“尝试”上度过了美好的一天,但都一无所获。

感谢您一百万次的帮助!

【问题讨论】:

  • 我也面临同样的问题。和你一样,我开始觉得我做错了什么,因为一个人写了很多预处理的东西。从this site 我得到的印象是,使用 hasandbelongstomany 会给我一种更原生的感觉,尽管对于多选来说感觉有点不必要的复杂。

标签: cakephp


【解决方案1】:

关联的日期(供应商类型)不应是供应商数据内部的数组,而应是外部的。

array(
    'Vendor' => 
           array('name' => 'test', 'id' => 7),
    'VendorPaymentType => array(
         array('id' => 13, 'type' => 'payable'),
         array('type' => 'check')
    )
);

所以 VendorPaymentType 不是 Vendor 数据的一部分。为了进一步混淆你,更深层次的关联应该是“父”数组的一部分。

还有:

  • 您不必在 VendorPaymentType 记录中重复 Vendor 的 ID
  • 您不应该使用模型名称 VendorPaymentType(单数)吗?
  • 在您的代码中,您可以一次保存所有供应商。如果您在 foreach 循环中放置一个 saveAssociated 调用,您可以让自己更轻松。如果需要(并使用 InnoDB),您可以将其包装在事务中。
  • 如果一次保存关联太难,您可以只保存供应商,然后保存对 de VendorPaymentType 的更改,如果需要创建新的。在循环中,可能在事务中。

希望这会有所帮助。 :-)

另见:http://book.cakephp.org/2.0/en/models/saving-your-data.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多