我正在尝试在包含多个附件的模型(问题)中使用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' => ''
)
);
模型附件$ 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.'));
}
}
我已经:
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'
)
)
答案 0 :(得分:1)
您是否检查了以下内容:
过去,这些都与我“陷入困境”。