【问题标题】:CAKE-PHP how to save dynamic array in the MYSQL tableCAKE-PHP如何在MYSQL表中保存动态数组
【发布时间】:2015-02-05 12:49:03
【问题描述】:

我正在创建一个动态数组并保存到这里的表格中是我的保存模型

型号: 我有类别,类别有很多产品,然后产品有很多子产品,子产品可能有很多选项

这是我创建动态复选框的 VIEW 代码

<tr>
                <td style="background-color:#f3f5f6;">
                  <?php // debug($Product);exit; ?>  

                <?php

                    //foreach ($Product as $Products):  ?>  
                    <?php echo $this->Form->checkbox('Artdetail.products.', array('value'=>$Product['Product']['id'],'hiddenField'=>false)); ?>
                        <?php echo $Product['Product']['product_name'].'<br/>' ?>

                    <?php 

                    foreach ($Product['SubProduct'] as $subproducts):?>

                   <?php echo $this->Form->checkbox('Artdetail.products.'.$Product['Product']['id'].'.subproducts.', array('value'=>$subproducts['id'],'hiddenField'=>false)); ?>
                        <?php echo $subproducts['subproduct_name'].'<br/>' ?>




                    <?php 

                    foreach ($subproducts['Option'] as $options):  ?>


                    <?php echo $this->Form->checkbox('Artdetail.products.'.$Product['Product']['id'].'.subproducts.'.$subproducts['id'].'.options.', array('value'=>$options['id'],'hiddenField'=>false)); ?>
                        <?php echo $options['optname'].'<br/>' ?>

                            <?php 

                            endforeach;?>

                    <?php 

                    endforeach; ?>

               <?php  endforeach;?>
               </td>
            </tr>

这是我的控制器代码

if ($this->request->is('post')) {
                    $this->Artproject->create();


            $this->request->data['Artproject']['ainum'] = 'AI-' . $this->request->data['Artproject']['ainum'];
            $this->request->data['Artproject']['createdby'] = $this->Auth->user('id');

            if ($this->Artproject->save($this->request->data['Artproject'])) {
                  $artid = $this->Artproject->getLastInsertID();

               foreach ($this->request->data['Artdetail']['products'][$prdid]['subproducts'] as $subproducts):

                  //debug($subproducts);

                 $this->request->data['Artdetaill']['category_id'] = $sportid;
                 $this->request->data['Artdetaill']['product_id'] = $prdid;
                 $this->request->data['Artdetaill']['subproduct_id'] = $subproducts;
                  $this->request->data['Artdetaill']['user_id'] = $this->request->data['Artproject']['user_id'];
                 $this->request->data['Artdetaill']['art_id'] = $artid;

                 if(!empty($subproducts['options'])){

                 foreach ($subproducts['options'] as $options):

                $this->request->data['Artdetaill']['option_id'] = $options;
                 $this->Artdetail->saveAll($this->request->data['Artdetaill']);  
                 endforeach;

                 }else{

                      $this->Artdetail->saveAll($this->request->data['Artdetaill']);  
                 }


                 endforeach; 
           exit;
            }

        }

提交后我得到这个数组

array(
    'Artproject' => array(
        'ainum' => '1024',
        'teamname' => 'Basketball',
        'user_id' => '10',
        'createdby' => ''
    ),
    'Artdetail' => array(
        'products' => array(
            (int) 0 => '1',
            (int) 1 => array(
                'subproducts' => array(
                    (int) 0 => '1',
                    (int) 1 => array(
                        'options' => array(
                            (int) 0 => '1',
                            (int) 1 => '2'
                        )
                    ),
                    (int) 2 => '2',
                    (int) 3 => '3'
                )
            )
        )
    )
)

我想把它保存在艺术细节表中 身份证
用户ID
art_id 类别ID
产品编号
子产品ID
option_id
已创建

![这是我提交的表单 Click Here to view image

【问题讨论】:

  • 是屏幕底部的 ArtDetails 字段名称吗?如果是这样,您应该将表单字段命名为与模型字段相同的名称,以使 save 起作用。另外,根据你的关系结构,如果你的 ArtDetail 只能有 1 个 option_id,那么你不需要保存 subproduct_id,也不需要保存 product_id。

标签: php mysql arrays cakephp


【解决方案1】:

为什么不通过序列化数组来存储数据? 简单地序列化($yourarr);并使用要识别数组的唯一键存储

【讨论】:

  • 感谢回复将其保存在表格中,但是当我选择选项时,它会造成混乱,并且不会在 option_id 列中保存选项
  • 取两列一作为 option_id 是主要的和自动递增的。第二个作为数据类型为文本的数据,并将序列化数据存储在文本列中。但我会建议使用主键作为一些值,让您轻松遍历,例如:ainum 和产品 id 复合键
  • 我不会将它保存为序列化数组......你不能再真正在数据库中搜索你的值了。或者至少这是a ** e的痛苦。而是学习 cakephp 的真正工作原理
  • 是的@conehead 我们不能在表中搜索值
【解决方案2】:

哇....代码太多了。

array(
    'Artproject' => array(
        'ainum' => '1024',
        ...
        'Artdetail' => array(...)
    ),    
)

将您的 Artdetail 对象放入您的 Artproject 中。 那么你只需要在你的控制器中使用这个代码:

    if ($this->request->is('post')) {
        $this->Artproject->create();
        $this->request->data['Artproject']['ainum'] = 'AI-' . $this->request->data['Artproject']['ainum'];
        $this->request->data['Artproject']['createdby'] = $this->Auth->user('id');
        if ($this->Artproject->saveAssociated($this->request->data['Artproject'], array('deep' => true))) { }

    }

只要确保你的模型关联是正确的。

【讨论】:

  • 感谢您的回复,您能告诉我关于我的模特协会的相关信息吗
  • array( 'ainum' => 'AI-1024', 'teamname' => '篮球', 'user_id' => '10', 'Artdetail' => array( 'products' = > 数组((整数)0 => '1',(整数)1 => 数组('子产品' => 数组((整数)0 => '1',(整数)1 => 数组('选项' = > 数组( (int) 0 => '1', (int) 1 => '2' ) ), (int) 2 => '2', (int) 3 => '3' ) ) ) ), ' createdby' => '7' 现在这是我将 Artdetail 对象放入我的 Artproject 后的数组。
  • 请显示您的模型的一些代码。猜猜你需要这样的东西:产品HABTM类别产品HABTM产品(子产品)产品HABTM选项。嗯...取决于你想要什么。它可能只是一个 belongsTo 或 hasMany 关系。
【解决方案3】:
  ARTDETAIL MODEL:  
public $belongsTo = array(
            'User' => array(
                'className' => 'User',
                'foreignKey' => 'user_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),
            'Artproject' => array(
                'className' => 'Artproject',
                'foreignKey' => 'artproject_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),
            'Category' => array(
                'className' => 'Category',
                'foreignKey' => 'category_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),
            'Product' => array(
                'className' => 'Product',
                'foreignKey' => 'product_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),
            'Subproduct' => array(
                'className' => 'Subproduct',
                'foreignKey' => 'subproduct_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            ),
            'Option' => array(
                'className' => 'Option',
                'foreignKey' => 'option_id',
                'conditions' => '',
                'fields' => '',
                'order' => ''
            )
        );

艺术项目模型:

public $hasMany = array(
          'Artdetail' => array(
              'className' => 'Artdetail',
              'foreignKey' => 'artproject_id'
          )  
        );

    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),

    );

【讨论】:

  • 嗯,这看起来有点奇怪。 Artdetail 的用户是否可能与 Artproject 不同?如果没有,为什么 Artdetail 也有用户?每个 Artdetail 只能有一个类别、一个产品和一个子产品? (顺便说一句......我不认为 ArtDetail '属于'一个产品......它不是'有一个'吗?) 子产品本身是一个产品吗?你有什么选择?老实说,我并没有真正得到您真正想要实现的目标;)在我看来,这就像一个小商店系统。如果是这种情况,那么您也许应该寻找不同的架构。
猜你喜欢
  • 2022-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多