Your Location is: Home > Php

CakePHP Upload Plugin - Field 'photo_dir' doesn't have a default value

From: Skopje View: 3240 Harsha M V 

Question

I am trying to set up an old cakePHP application in a new hosting and all of sudden am getting this error on the shared godaddy hosting. It works fine in the old hosting. Its been 2 years i have worked on CakePHP and dont seem to understand why this is coming. I am using the following upload plugin cakephp-upload

Error: SQLSTATE[HY000]: General error: 1364 Field 'photo_dir' doesn't have a default value
SQL Query: INSERT INTO `prayag_cms`.`galleries` (`branch_id`, `name`, `album_id`, `display_photo`, `modified`, `created`) VALUES (0, 'Photo', 1, 'CpLMQ28WYAEOnow.jpg_large.jpg', '2016-12-06 10:17:14', '2016-12-06 10:17:14')

This is my confirguration

public $actsAs = array(
            'Containable',
    'Upload.Upload' => array(
        'display_photo' => array(
            'fields' => array(
                'dir' => 'photo_dir'
            ),
                            'thumbnailSizes' => array(
                'small' => '250x250',
            ),
                            'thumbnailMethod' => 'php',
                            'path' => '{ROOT}webroot{DS}img{DS}{model}{DS}',
        ),

    )
);

Controller:

public function admin_add() {
    if ($this->request->is('post')) {
        $this->Gallery->create();
        if ($this->Gallery->save($this->request->data)) {
            $this->Session->setFlash(__('The gallery has been saved.'), 'admin/flash_success');
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The gallery could not be saved. Please, try again.'), 'admin/flash_error');
        }
    }
    $branches = $this->Gallery->Branch->find('list');
    $albums = $this->Gallery->Album->find('list');
    $this->set(compact('branches', 'albums'));
}

Best answer

It's may be happening for folder not exist(webroot/img/users[model]) or not have enough permission to create new folder or access folder.

  1. for this reason photo is not uploaded
  2. if photo is not uploaded then photo_dir assigned NULL
  3. But your database field doesn't accept NULL value
  4. Then you get those errors

hope your problem will be solved