错误:未找到列1054未知列'数组'在'字段列表'在cakephp

时间:2014-08-17 18:16:58

标签: cakephp

这是我的帖子控制器

public function add()
{
     if ($this->request->is('post')) {  
     $this->request->data['Post']['user_id'] = $this->Auth->user('id');

     $data = $this->request->data['Post'];
     //pr($this->data);
     debug($this->request);
     if ($this->Post->save($this->request->data)) {
     // print_r($data);
      move_uploaded_file($data['image_url']['tmp_name'], WWW_ROOT . 'img' . DS . 'uploads' .DS .$data['image_url']['name']);  

     $this->Session->setFlash(__('Your post has been saved.'));

     return $this->redirect(array('action' => 'add'));

     }
     }
     }

这是我的add.ctp

    <?php echo $this->Session->flash('auth');
    echo $this->Form->create('Post',array('type' => 'file'));
    echo $this->Form->input('text', array('cols' => '6','rows' => '10'));
    echo $this-> Form-> input('image_url', array('type' => 'file'));

    echo $this->Form->end('Save Post');
    ?>

这是我的index.ctp for show post

    <?php echo $this->Html->image('uploads/'.$post['Post']['image_url']);`

1 个答案:

答案 0 :(得分:0)

我在视图文件中有以下输入,

echo $this->Form->input('filename',array('type'=>'file'));

在控制器文件中添加功能,

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

            if(!empty($this->data['Post']['filename']['name']))
            {
                $file=$this->data['Post']['filename'];
                $ary_ext=array('png','jpg','gif','pdf'); //array of allowed extensions
                $ext = substr(strtolower(strrchr($file['name'], '.')), 1); //get the extension
                if(in_array($ext, $ary_ext))
                {
                    move_uploaded_file($file['tmp_name'], WWW_ROOT . 'img'. DS . 'uploads' .DS . time().$file['name']);
                    $this->request->data['Post']['filename'] = time().$file['name'];
                }
            }
            $this->Post->create();
            $this->request->data["Post"]["user_id"] = $this->Auth->user("id");
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('The post has been saved'), 'success');
                $this->redirect(array('action' => 'add'));
            } else {
                $this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'error');
            }
        }
    }

根据您的字段名称进行了必要的更改。我希望这可以帮助您。