cakephp一次添加多个记录到数据库,其他表与assoceatded id形成?

时间:2014-03-18 16:58:39

标签: php cakephp-2.0

我正在使用cakephp 2.0我正在尝试将多个图像添加到我的图像表中,并将功能名称和内容添加到内容tbl并获取要在图像表中使用的ID。我已经管理将我需要的信息添加到功能tbl并返回id。但它不会添加到图像表。在我使用'multiple'=>的形式中图库中的“多个”允许用户为图库选择多个图像。

我的问题是,如上所述,它不会仅保存我的图像功能部分。我需要帮助了解如何在一个功能ID下保存多个图片,并确保我的隐藏的placement_id与我的图片正确匹配。

提前谢谢。

控制器:

 public function add() {
    if ($this->request->is('post')) {

        // parse the image file name to get a name without the ext to use for an alt tage.
        $file_name = array();
        $file = $this->request->data['Image']['image'];

        foreach ($file as $value) {
            $temp_file = ($value['name']);

            $info = pathinfo($temp_file);
            $temp_name =  basename($temp_file,'.'.$info['extension']);

            // set veriable to the array of name generated
            $file_name[] = $temp_name; 
        }

        if(!empty($this->request->data)){
            //save the featuer name and content to feature tbl and return id            
            $feature = $this->Feature->save($this->request->data);
            //debug($feature);

            if(!empty($feature)){
                //set feature_id to id returned above and added to data 
                $this->request->data['Image']['Feature_id'] = $this->Feature->id;
                debug($this->request->data);

                //set name the file_name generated above and added to data
                $this->request->data['Image']['name'] = $file_name;
                debug($this->request->data);

                $this->Feature->Image->saveAssociated($this->request->data);
            }
        }
    }
}

查看/ add.ctp

<?php   
echo $this->Form->create('Feature', array(
        'class' => 'form-horizontal',
        'role' => 'form',
        'type' => 'file',
        'inputDefaults' => array(
            'format' => array('before', 'label', 'between', 'input', 'error', 'after'),
            'div' => array('class' => 'form-group'),
            'class' => array('form-control'),
            'label' => array('class' => 'col-lg-2 control-label'),
            'between' => '<div class="col-lg-6">',
            'after' => '</div>',
            'error' => array('attributes' => array('wrap' => 'span', 'class' => 'help-inline')),
        ))); ?>
<?php
    echo '<fieldset>';
    echo '<legend>Add Features</legend>';

    echo '<div class="row"><div class="col-md-4">';
    echo $this->Form->input('Feature.name');    
    echo '</div></div>';
    echo '</fieldset>';
    //----------------------- Image Block -----------------------------//   
    // group main image //
    echo '<fieldset>';
    echo '<div class="row"><div class="col-md-12">';
    echo '<legend>Main Image</legend>';

    echo $this->Form->input(
            'Image.image.', array(              
                    'type' => 'file',
                    'class' =>'main_image')); 

    echo $this->Form->input(
            'Image.placement_id.', array(
                    'type' => 'hidden',
                    'value' => '1',
                    'class'=> 'main_image'));

    echo '</div></div>';

    // bottom left image //
    echo '<div class="row"><div class="col-md-12">';
    echo '<legend>Bottom Left</legend>';
    // group image 2//
    echo $this->Form->input(
            'Image.image.', array(                  
                    'type' => 'file',
                    'class' =>'bl')); 

    echo $this->Form->input(
            'Image.placement_id.', array(          
                            'type'  => 'hidden',
                            'value' => '2',
                            'class' => 'bl'));
    echo '</div></div>';

    // bottom right //
    echo '<div class="row"><div class="col-md-12">';
    echo '<legend>Bottom Right</legend>';

        echo $this->Form->input(
            'Image.image.', array(                  
                    'type' => 'file',
                    'class' =>'br')); 

    echo $this->Form->input(
            'Image.placement_id.', array(          
                            'type'    => 'hidden',
                            'value' => '3',
                            'class' => 'br'));      
    echo '</div></div>';

    //----------------------- Gallery Block -----------------------------//     
    echo '<div class="row"><div class="col-md-12">';        
    echo '<legend>Gallery</legend>';
    echo '<h4><em>Uploade multiple image for the gallery once.</em></h4>';
    echo $this->Form->input(
            'Image.image.', array(                  
                    'type' => 'file',
                    'multiple' => 'multiple',
                    'class' =>'gallery')); 

    echo $this->Form->input(
            'Image.placement_id.', array(          
                            'type'  => 'hidden',
                            'value' => '4',
                            'class' => 'gallery'));
    echo '</div></div>';
    //----------------------- Content Block -----------------------------//     
    echo '<div class="row"><div class="col-md-12">';
    echo '<legend>Content</legend>';
    echo $this->Form->textarea('Feature.content', array('rows' => '5', 'cols' => '55'));
    echo '</div></div>';

    echo '</fieldset>';
    echo '<div class="row"><div class="col-md-2 col-md-offset-10">';
    echo $this->Form->end('Save Content');
    echo '</div></div>';

特征模型:

<?php
class Feature extends AppModel{
    public $useTable = "features";
    public $displayField = "name";

    public $hasMany = array(
        'Image'=>array('classname' => 'Image', 'foreignkey' => 'Feature_id'),

    );
}

图片模型:

<?php
class Image extends AppModel {
    public $actsAs = array(
        'Uploader.Attachment' => array(
            'image' => array(
                'tempDir' => TMP,
                'uploadDir' => 'img/uploads/',
                'finalPath' => 'img/uploads/'
            )
        )
    );

    public $useTable = "images";
    public $displayField = "image";
    public $belongsTo = array(
        'Placement' => array('classname' => 'Placement', 'foreignkey' => 'Placement_id'),
        'Wheel'     => array('classname' => 'Wheel', 'foreignkey' => 'Wheel_id'),
        'Button'    => array('classname' => 'Button', 'foreignkey' => 'Button_id'),
        'Feature'   => array('classname' => 'Feature', 'foreignkey' => 'Feature_id')

    );

    public $validate = array(
        'url' => array(
            'rule' => 'notEmpty'
        ),
        'name' => array(
            'rule' => 'notEmpty'
        ),
    );
}

我的第二个if子句的调试输出:

array(
'Feature' => array(
    'name' => 'Test1',
    'content' => 'testing',
    'id' => '254'
),
'Image' => array(
    'placement_id' => array(
        (int) 0 => '1',
        (int) 1 => '2',
        (int) 2 => '3',
        (int) 3 => '4'
    ),
    'image' => array(
        (int) 0 => array(
            'name' => 'vector-fp5-wheels.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/php5gK2Bb',
            'error' => (int) 0,
            'size' => (int) 323233
        ),
        (int) 1 => array(
            'name' => 'vector-jeep-jk.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/php6AyLrN',
            'error' => (int) 0,
            'size' => (int) 318954
        ),
        (int) 2 => array(
            'name' => 'vector-jeep-jk.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/phpMPyOhp',
            'error' => (int) 0,
            'size' => (int) 318954
        ),
        (int) 3 => array(
            'name' => 'vector-fp8-afw.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/phpalo870',
            'error' => (int) 0,
            'size' => (int) 294194
        )
    )
)

调试我的第三个if子句:

array(
'Feature' => array(
    'name' => 'Test1',
    'content' => 'testing'
),
'Image' => array(
    'placement_id' => array(
        (int) 0 => '1',
        (int) 1 => '2',
        (int) 2 => '3',
        (int) 3 => '4'
    ),
    'image' => array(
        (int) 0 => array(
            'name' => 'vector-fp5-wheels.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/php5gK2Bb',
            'error' => (int) 0,
            'size' => (int) 323233
        ),
        (int) 1 => array(
            'name' => 'vector-jeep-jk.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/php6AyLrN',
            'error' => (int) 0,
            'size' => (int) 318954
        ),
        (int) 2 => array(
            'name' => 'vector-jeep-jk.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/phpMPyOhp',
            'error' => (int) 0,
            'size' => (int) 318954
        ),
        (int) 3 => array(
            'name' => 'vector-fp8-afw.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/phpalo870',
            'error' => (int) 0,
            'size' => (int) 294194
        )
    ),
    'Feature_id' => '254'
)

array(
'Feature' => array(
    'name' => 'Test1',
    'content' => 'testing'
),
'Image' => array(
    'placement_id' => array(
        (int) 0 => '1',
        (int) 1 => '2',
        (int) 2 => '3',
        (int) 3 => '4'
    ),
    'image' => array(
        (int) 0 => array(
            'name' => 'vector-fp5-wheels.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/php5gK2Bb',
            'error' => (int) 0,
            'size' => (int) 323233
        ),
        (int) 1 => array(
            'name' => 'vector-jeep-jk.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/php6AyLrN',
            'error' => (int) 0,
            'size' => (int) 318954
        ),
        (int) 2 => array(
            'name' => 'vector-jeep-jk.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/phpMPyOhp',
            'error' => (int) 0,
            'size' => (int) 318954
        ),
        (int) 3 => array(
            'name' => 'vector-fp8-afw.png',
            'type' => 'image/png',
            'tmp_name' => '/tmp/phpalo870',
            'error' => (int) 0,
            'size' => (int) 294194
        )
    ),
    'Feature_id' => '254',
    'name' => array(
        (int) 0 => 'vector-fp5-wheels',
        (int) 1 => 'vector-jeep-jk',
        (int) 2 => 'vector-jeep-jk',
        (int) 3 => 'vector-fp8-afw'
    )
)

0 个答案:

没有答案