2012-09-25 21 views
1

我想在具有多個附件的模型(問題)中使用saveAssociated方法。cake-uploader - 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.')); 
      } 
    } 

我有:

  1. 用saveAssociated嘗試過,結果是一樣的;
  2. 使用附件模型的行爲測試(女巫隨插件提供)成功上傳文件;
  3. 成功保存的筆記是另一種模式是屬於關聯問題

是否應saveAssociated和白水參加審議插件實現的行爲?

與此相關的其他問題是我在表格附件中插入了兩個註冊表。一個填充字段名稱,另一個填寫字段模型。 這最後一個問題正在發生,因爲行爲沒有被考慮。使用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. 確保BASEDIR和attachement DIR存在預期的位置?
  2. 檢查目錄上的權限以確保您的應用具有 的權限,以便在該處寫入?

那些過去和我一起都是「陷阱」。

+1

我有東西,你寫,因爲上面的代碼是正確的。謝謝您的回覆。 – jplfl

+0

很高興幫助。快樂編碼! –

相關問題