cakephpcakephp-2.2

cake-uploader - saveAssociated


I'm trying to work with saveAssociated method in a model (Questions) that hasMany Attachments.

class Attachment extends AppModel {
    public $belongsTo = array(
        'Question' => array(
            'className' => 'Question',
            'foreignKey' => 'question_id',
        ),
    );

class Question extends AppModel {
public $hasMany = array(
    'Attachment' => array(
        'className' => 'Attachment',
        'foreignKey' => 'foreign_key',
        'dependent' => false,
        'conditions' => array('Attachment.model' => 'Question'),
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
        )
    );

The model Attachments $actsAs AttachmentBehavior (from Cake-uploader Plugin)

class Attachment extends AppModel {
...
    public $actsAs = array(
        'Uploader.FileValidation' => array(
            'file' => array(
                'extension' => array(
                    'value' => array('gif', 'jpg', 'jpeg'),
                    'error' => 'Only gif, jpg and jpeg images are allowed!'
                ),
                'required' => true
            ),
            'import' => array('required' => true),
            'file' => array('required' => true),
        ),
        'Uploader.Attachment' => array(
            'file' => array(
                'name' => 'uploaderFilename',
                'baseDir' => '/******',
                'uploadDir' => '/****/',
                'dbColumn' => 'real_name',
                'maxNameLength' => 30,
                'overwrite' => true,
                'stopSave' => false,
                'transforms' => ''
            )
        )
    );

When I try to add a Question with an attachment the information is saved in the database but the file isn't uploaded to the expected folder:

    public function add() {
        if (!empty($this->request->data) && $this->request->is('post')) {

            App::import('Vendor', 'Uploader.Uploader');

            $this->Mensagen->create();
            $this->request->data['Anexo'][0]['model'] = $this->modelClass;

            debug($this->request->data);

            if ($this->Mensagen->saveAll($this->request->data)) {
                $this->Session->setFlash(__('Success'));
            } else {
                $this->Session->setFlash(__('Try again.'));
            }
    }

I've:

  1. tried with saveAssociated, and the result is the same;
  2. successfully uploaded files using the behaviours test (which came with the plugin) for the Attachment Model;
  3. successfully saved notes that is another Model that belongsTo Questions

Should saveAssociated and saveAll take in consideration the behaviour implemented by the plugin?

Other issue related to this is that I'm getting tow inserted registries on the table Attachment. One with the field name filled and other with the field model filled.

This last issue is happening because the behaviour is not being taken in consideration. Using the bahaviours test only one registry is saved.

array(
    'Question' => array(
        'state_id' => '1',
        'from_id' => '2',
        'grup_id' => '1',
        'action' => 'add',
        'question' => 'test file 2'
    ),
    'Attachment' => array(
        'file' => array(
            'name' => 'foto0001.jpg',
            'type' => 'image/jpeg',
            'tmp_name' => '/tmp/phpPuVx3P',
            'error' => (int) 0,
            'size' => (int) 140773
        ),
        'model' => 'Question'
    )
)

Solution

  • Have you checked the following:

    1. Make sure the basedir and attachement dir exist where expected?
    2. Check the permissions on the directory to ensure your app has permission to write there?

    Those have both been "gotchas" with me in the past.