cakephp 保存来自不同表的数据

cakephp saving data from different tables

我在保存包和图像中输入的数据时遇到问题 table,因为我想同时保存两者的数据。我有一个 table 用于包装和图像。图像 table 由(id、package_id 和图像)组成。包有很多图像和图像属于包。

这是函数add_package:

if ($this->request->is('post')) {
        $this->Package->create();
        if ($this->Package->save($this->request->data)) {
            $this->Image->create();
            if(empty($this->data['Image']['image']['name'])) {
                unset($this->request->data['Image']['image']);
            }
            if(!empty($this->data['Image']['image']['name'])) {
                $filename = $this->request->data['Image']['image']['name'];
                $new_filename = STring::uuid().'-'.$filename;
                $file_tmp_name = $this->request->data['Image']['image']['tmp_name'];
                $dir = WWW_ROOT.'img'.DS.'uploads';

                move_uploaded_file($file_tmp_name,$dir.DS.$new_filename);

                $this->request->data['Image']['package_id'] = $this->Package->id;
                $this->request->data['Image']['image'] = $new_filename;
                if($this->Package->Image->save($this->request->data)) {
                    return $this->redirect(array('action' => 'admins'));
                }
            }
            //return $this->redirect(array('action' => 'admins'));

        }else {
            $this->Session->setFlash(__('The package could not be saved. Please, try again.'));
        }
    }

这是 add_package 函数的视图:

<?php echo $this->Form->create('Package');?>
                <fieldset>
                <legend><?php echo __('Create a Package'); ?></legend>
                    <table cellpadding=12>
                        <?php
                            echo $this->Html->tableCells(array(
                                array(
                                    'Name of the Package: ',
                                    $this->Form->input('name', array('label' => false, 'class' => 'textbox'))
                                ),
                                array(
                                    'Description: ', 
                                    $this->Form->input('description', array('label' => false, 'rows' => '15'))
                                ),
                                array(
                                    'Places: ', 
                                    $this->Form->input('Place', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $places))
                                ),
                                array(
                                    'Inclusions: ', 
                                    $this->Form->input('Inclusion', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $inclusions))
                                ),
                                array(
                                    'Departure Time: ',
                                    $this->Form->input('departure_time', array('label' => false, 'class' => 'drop'))
                                ),
                                array(
                                    'Price: ',
                                    $this->Form->input('price', array('label' => false, 'class' => 'textbox'))
                                ),
                            ));
                        ?>
                    </table>
                    <?php 
                        echo $this->Form->create('Image',array('enctype'=>'multipart/form-data'));
                        echo $this->Form->input('image', array('label'=> false, 'type' => 'file'));
                    ?>
                    <?php 
                        echo $this->Form->Submit(__('Create Package'), array('class' => 'button'));
                    ?>
                </fieldset>

我试图同时保存包和图像。最初,我所做的是保存包的数据,然后将图像添加到包中,但现在我想同时保存包和图像,但我做不对。我已经这样做了半天了。任何事情都会有帮助,谢谢。随意问的问题。

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

在你的控制器中使用:

if($this->Package->saveAll($this->request->data)) {
                return $this->redirect(array('action' => 'admins'));
            }

您的表格将是:

<?php echo $this->Form->create('Package',array('type' => 'file');?>
            <fieldset>
            <legend><?php echo __('Create a Package'); ?></legend>
                <table cellpadding=12>
                    <?php
                        echo $this->Html->tableCells(array(
                            array(
                                'Name of the Package: ',
                                $this->Form->input('name', array('label' => false, 'class' => 'textbox'))
                            ),
                            array(
                                'Description: ', 
                                $this->Form->input('description', array('label' => false, 'rows' => '15'))
                            ),
                            array(
                                'Places: ', 
                                $this->Form->input('Place', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $places))
                            ),
                            array(
                                'Inclusions: ', 
                                $this->Form->input('Inclusion', array('label'=>false, 'type'=>'select', 'multiple' => 'checkbox', 'options' => $inclusions))
                            ),
                            array(
                                'Departure Time: ',
                                $this->Form->input('departure_time', array('label' => false, 'class' => 'drop'))
                            ),
                            array(
                                'Price: ',
                                $this->Form->input('price', array('label' => false, 'class' => 'textbox'))
                            ),
                        ));
                    ?>
                </table>
                <?php 

                    echo $this->Form->input('Image.image', array('label'=> false, 'type' => 'file'));
                ?>
                <?php 
                    echo $this->Form->Submit(__('Create Package'), array('class' => 'button'));
                ?>
            </fieldset>
<?php 
                    echo $this->Form->end();
                ?>