1

我正在尝试在具有许多附件的模型(问题)中使用 saveAssociated 方法。

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' => ''
        )
    );

模型 Attachments $actsAs AttachmentBehavior (来自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' => ''
            )
        )
    );

当我尝试添加带有附件的问题时,信息保存在数据库中,但文件未上传到预期的文件夹:

    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.'));
            }
    }

我有:

  1. 用saveAssociated试过了,结果是一样的;
  2. 使用附件模型的行为测试(插件附带)成功上传文件;
  3. 成功保存了属于另一个模型的笔记

saveAssociated 和 saveAll 是否应该考虑插件实现的行为?

与此相关的其他问题是我在附件表上插入了两个注册表。一个填充了字段名称,另一个填充了字段模型。

最后一个问题正在发生,因为没有考虑该行为。使用 bahaviours 测试只保存一个注册表。

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'
    )
)
4

1 回答 1

1

您是否检查了以下内容:

  1. 确保 basedir 和 attachement 目录存在于预期的位置?
  2. 检查目录上的权限以确保您的应用程序有权在那里写入?

过去这些都是我的“陷阱”。

于 2013-01-16T17:29:01.777 回答