【问题标题】:cake-uploader - saveAssociatedcake-uploader - saveAssociated
【发布时间】:2023-03-11 06:10:01
【问题描述】:

我正在尝试在具有许多附件的模型(问题)中使用 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'
    )
)

【问题讨论】:

    标签: cakephp cakephp-2.2


    【解决方案1】:

    您是否检查了以下内容:

    1. 确保 basedir 和 attachement 目录存在于预期的位置?
    2. 检查目录的权限以确保您的应用具有 允许在那里写吗?

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

    【讨论】:

    • 我不得不说你是写的,因为上面的代码是正确的。感谢您的回复。
    • 很高兴为您提供帮助。编码快乐!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多