Cakephp HABTM 不保存

发布于 2024-12-11 04:46:54 字数 4392 浏览 0 评论 0原文

由于某种原因,我无法保存 HABTM 关系,我检查了表单生成并传递给控制器​​的数据结构,还检查了模型中的数据结构,一切似乎都是有序的。任何帮助将不胜感激。

    CREATE TABLE `products_colors` (
      `product_id` char(36) NOT NULL DEFAULT '',
      `color_id` char(36) NOT NULL DEFAULT '',
      PRIMARY KEY (`product_id`,`color_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

       CREATE TABLE `products_sizes` (
      `product_id` char(36) NOT NULL,
      `size_id` char(36) NOT NULL DEFAULT '',
      PRIMARY KEY (`product_id`,`size_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    class ProductsController extends AppController {
        function edit($id = null)
        {
            if(!is_null($id))
            {
                $this->set('productCategories', $this->Product->Category->find('list'));    
                $this->set('sizes', $this->Product->Sizes->find('list'));   
                $this->set('colors', $this->Product->Colors->find('list'));
                $this->Product->contain(array('Sizes', 'Colors'));
                $product = $this->Product->read(null, $id);
                if(!empty($product)) {
                    if($this->RequestHandler->isPut()) {
                        $this->Product->id = $id;
                        $this->Product->begin();
                        $this->Product->set($this->data);
                        debug($this->data); die;
                        //$dbo = $this->Product->getDatasource();
                        //$dbo->fullDebug = true;
                        if($this->Product->save()) {
                            $this->Product->commit();
                            //debug($dbo->_queriesLog); die;
                            $this->Session->setFlash( __('Product updated',true), 'flash_true');
                            $this->redirect(array('action' => 'view', $this->Product->field('slug')));
                  } else {
                            $this->Product->rollback();
                    $this->Session->setFlash(__('Please correct the errors below',true), 'flash_false');
                  }         
                    } else {
                        $this->data = $product;
                    }
                    $this->render('_form');
                    return;
                }
            }   
            $this->redirect(array('controller' => 'categories', 'action' => 'index'));
        }
    }

    class Product extends AppModel 
    {
        var $hasAndBelongsToMany = array(
            'Colors' => array(
                'className' => 'Color',
                'joinTable' => 'products_colors',
                'foreignKey' => 'product_id',
                'associationForeignKey' => 'color_id',
                'unique' => false,
            ),
            'Sizes' => array(
                'className' => 'Size',
                'joinTable' => 'products_sizes',
                'foreignKey' => 'product_id',
                'associationForeignKey' => 'size_id',
            ),
        );
    }


    <?php echo $this->Form->create('Product'); ?>
        <fieldset>
            <legend><?php __('Options') ?></legend>
        <?php echo $this->Form->input('Product.id') ?>
        <?php echo $this->Form->input('Product.product_category_id', array('label' => __('Category', true))) ?>
        <?php echo $this->Form->input('Sizes', array('label' => __('Sizes', true), 'multiple' => 'checkbox')) ?>
        <?php echo $this->Form->input('Colors', array('label' => __('Colors', true), 'multiple' => 'checkbox')) ?>  
        </fieldset>
        <fieldset>
            <legend><?php __('Product') ?></legend>
            <?php echo $this->Form->input('Product.sku') ?>
            <?php echo $this->Form->input('Product.price') ?>
            <?php echo $this->Form->input('Product.name') ?>
            <?php echo $this->Form->input('Product.description') ?>
            <?php echo $this->Form->input('Product.state', array('type' => 'checkbox')) ?>
        </fieldset>
    <?php echo $this->Form->end(__('Submit', true)); ?>

For some reason i am unable to save HABTM relationships, I have checked the data structure generated by the form and passed to the controller and also check the data structure in the model, and all seems to be inorder. Any help would greatly be appreciated.

    CREATE TABLE `products_colors` (
      `product_id` char(36) NOT NULL DEFAULT '',
      `color_id` char(36) NOT NULL DEFAULT '',
      PRIMARY KEY (`product_id`,`color_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

       CREATE TABLE `products_sizes` (
      `product_id` char(36) NOT NULL,
      `size_id` char(36) NOT NULL DEFAULT '',
      PRIMARY KEY (`product_id`,`size_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    class ProductsController extends AppController {
        function edit($id = null)
        {
            if(!is_null($id))
            {
                $this->set('productCategories', $this->Product->Category->find('list'));    
                $this->set('sizes', $this->Product->Sizes->find('list'));   
                $this->set('colors', $this->Product->Colors->find('list'));
                $this->Product->contain(array('Sizes', 'Colors'));
                $product = $this->Product->read(null, $id);
                if(!empty($product)) {
                    if($this->RequestHandler->isPut()) {
                        $this->Product->id = $id;
                        $this->Product->begin();
                        $this->Product->set($this->data);
                        debug($this->data); die;
                        //$dbo = $this->Product->getDatasource();
                        //$dbo->fullDebug = true;
                        if($this->Product->save()) {
                            $this->Product->commit();
                            //debug($dbo->_queriesLog); die;
                            $this->Session->setFlash( __('Product updated',true), 'flash_true');
                            $this->redirect(array('action' => 'view', $this->Product->field('slug')));
                  } else {
                            $this->Product->rollback();
                    $this->Session->setFlash(__('Please correct the errors below',true), 'flash_false');
                  }         
                    } else {
                        $this->data = $product;
                    }
                    $this->render('_form');
                    return;
                }
            }   
            $this->redirect(array('controller' => 'categories', 'action' => 'index'));
        }
    }

    class Product extends AppModel 
    {
        var $hasAndBelongsToMany = array(
            'Colors' => array(
                'className' => 'Color',
                'joinTable' => 'products_colors',
                'foreignKey' => 'product_id',
                'associationForeignKey' => 'color_id',
                'unique' => false,
            ),
            'Sizes' => array(
                'className' => 'Size',
                'joinTable' => 'products_sizes',
                'foreignKey' => 'product_id',
                'associationForeignKey' => 'size_id',
            ),
        );
    }


    <?php echo $this->Form->create('Product'); ?>
        <fieldset>
            <legend><?php __('Options') ?></legend>
        <?php echo $this->Form->input('Product.id') ?>
        <?php echo $this->Form->input('Product.product_category_id', array('label' => __('Category', true))) ?>
        <?php echo $this->Form->input('Sizes', array('label' => __('Sizes', true), 'multiple' => 'checkbox')) ?>
        <?php echo $this->Form->input('Colors', array('label' => __('Colors', true), 'multiple' => 'checkbox')) ?>  
        </fieldset>
        <fieldset>
            <legend><?php __('Product') ?></legend>
            <?php echo $this->Form->input('Product.sku') ?>
            <?php echo $this->Form->input('Product.price') ?>
            <?php echo $this->Form->input('Product.name') ?>
            <?php echo $this->Form->input('Product.description') ?>
            <?php echo $this->Form->input('Product.state', array('type' => 'checkbox')) ?>
        </fieldset>
    <?php echo $this->Form->end(__('Submit', true)); ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2024-12-18 04:46:54

对于面临此问题的其他人,Cakephp 仅支持 INT() 或 CHAR(36) 的 id。使用非标准 ID 也会导致类似的问题。

For anyone else facing this issue, Cakephp only supports id's that are either INT() or CHAR(36). Using a non standard ID will result in a similar issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文