2015-03-19 96 views
1
 /* ShootsTable.php Meta Table */ 

     public function initialize(array $config) 
     { 
      $this->table('shoots'); 
      $this->displayField('title'); 
      $this->primaryKey('id'); 
      $this->hasMany('ShootMeta'); 
     } 


    /* ShootMetaTable.php Meta Table */ 

    public function initialize(array $config) 
     { 
      $this->table('shoot_meta'); 
      $this->displayField('id'); 
      $this->primaryKey('id'); 
      $this->belongsTo('Shoots'); 
     } 
     public function buildRules(RulesChecker $rules) 
     { 
      $rules->add($rules->existsIn(['shoots_id'], 'Shoots')); 
      return $rules; 
     } 

/* Shoots.php Controller */ 

public function add() 
    { 
     $shoot = $this->Shoots->newEntity(null); 
     if ($this->request->is('post')) { 
      $this->Shoots->patchEntity($shoot, $this->request->data,[ 
       'associated' => ['ShootMeta'] 
      ]); 
      $shoot->set('created_by', 1); 
      debug($shoot); 
      if ($this->Shoots->save($shoot,['associated' => ['ShootMeta']])) { 
       $this->Flash->success('The shoot has been saved.'); 
       // return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error('The shoot could not be saved. Please, try again.'); 
      } 
     } 
     $this->set(compact('shoot')); 
     $this->set('_serialize', ['shoot']); 
    } 

/* Add.ctp Template */ 

<div class="shoots form large-10 medium-9 columns"> 
    <?= $this->Form->create($shoot); ?> 
    <fieldset> 
     <legend><?= __('Add Shoot') ?></legend> 
     <?php 
      echo $this->Form->input('title'); 
      echo $this->Form->input('content'); 
      echo $this->Form->input('datetime', ['label' => 'Date/Time Of Shoot']); 

      echo $this->Form->input('shoot_meta.0.meta_key', ['type' => 'hidden', 'value' => 'photographer_spaces']); 
      echo $this->Form->input('shoot_meta.0.meta_value',['label' => 'Photographer Spaces', 'type' => 'number']); 
     ?> 
    </fieldset> 
    <?= $this->Form->button(__('Submit')) ?> 
    <?= $this->Form->end() ?> 
</div> 

/* debug($shoots) output */ 

object(App\Model\Entity\Shoot) { 

    'new' => true, 
    'accessible' => [ 
     'created_by' => true, 
     'title' => true, 
     'content' => true, 
     'datetime' => true, 
     'shoot_meta' => true 
    ], 
    'properties' => [ 
     'title' => '123', 
     'content' => '123', 
     'datetime' => object(Cake\I18n\Time) { 

      'time' => '2015-03-19T07:04:00+0000', 
      'timezone' => 'UTC', 
      'fixedNowTime' => false 

     }, 
     'shoot_meta' => [ 
      (int) 0 => object(App\Model\Entity\ShootMetum) { 

       'new' => true, 
       'accessible' => [ 
        'shoots_id' => true, 
        'meta_key' => true, 
        'meta_value' => true, 
        'shoot' => true 
       ], 
       'properties' => [ 
        'meta_key' => 'photographer_spaces', 
        'meta_value' => '123' 
       ], 
       'dirty' => [ 
        'meta_key' => true, 
        'meta_value' => true 
       ], 
       'original' => [], 
       'virtual' => [], 
       'errors' => [ 
        'shoots_id' => [ 
         '_required' => 'This field is required' 
        ] 
       ], 
       'repository' => 'ShootMeta' 

      } 
     ], 
     'created_by' => (int) 1 
    ], 
    'dirty' => [ 
     'title' => true, 
     'content' => true, 
     'datetime' => true, 
     'shoot_meta' => true, 
     'created_by' => true 
    ], 
    'original' => [], 
    'virtual' => [], 
    'errors' => [], 
    'repository' => 'Shoots' 

} 

正如你所看到的,需要現場shoots_id,我本來以爲會被自動地傳遞下去(儘管在這一點上沒有任何執行的MySQL )。的CakePHP 3的hasMany將不父ID傳遞給相關的兒童

我覺得我可能已經走錯了這條路,但花了整整兩天的時間試圖讓它正確。那些日子之一是我試圖弄清楚爲什麼在烘焙後它已經將很多對ShootMeta的引用命名爲ShootMetum,我認爲它實際上已經損壞了它。

我最大的問題之一是知道在哪裏使用shoot_meta,ShootMeta,shootmeta,shootmetum,ShootMetum等。它感覺就像一個雷區!

/更新

下面的保存對象的轉儲。它清楚地分配它,它似乎不是在SQL中執行它?

'shoot_meta' => [ 
      (int) 0 => object(App\Model\Entity\ShootMetum) { 

       'new' => false, 
       'accessible' => [ 
        'shoots_id' => true, 
        'meta_key' => true, 
        'meta_value' => true 
       ], 
       'properties' => [ 
        'meta_key' => 'photographer_spaces', 
        'meta_value' => '123', 
        'shoot_id' => '2', 
        'id' => '3' 
       ], 
       'dirty' => [], 
       'original' => [], 
       'virtual' => [], 
       'errors' => [], 
       'repository' => 'ShootMeta' 

      }, 
+0

英語不是我的母語,如此忍受我,但真的可以用來作爲某種獨立的單詞「元」替代「元數據」?接下來的問題是「Meta」是否真的是複數(「Metas」任何人?),並且「Metum」(僅在拉丁語中聽說過這個)是否是正確的單數。 – ndm 2015-03-19 08:48:06

+0

我想你的表不僅有表規則,還有驗證規則(必需),對嗎? – ndm 2015-03-19 08:49:45

回答

1

找到了。

它指的是shoot_id當我調試保存

'shoot_meta' => [ 
      (int) 0 => object(App\Model\Entity\ShootMetum) { 
       'new' => false, 
       'accessible' => [ 
        'shoots_id' => true, 
        'meta_key' => true, 
        'meta_value' => true 
       ], 
       'properties' => [ 
        'meta_key' => 'photographer_spaces', 
        'meta_value' => '123', 
        'shoot_id' => '2', 
        'id' => '3' 
       ], 
       'dirty' => [], 
       'original' => [], 
       'virtual' => [], 
       'errors' => [], 
       'repository' => 'ShootMeta' 

      }, 

因爲它用的是單數名該協會的一些原因。在Shoots.php模型中更改。

$this->hasMany('ShootMeta'); 

$this->hasMany('ShootMeta',[ 
    'foreignKey' => 'shoots_id' 
]); 
0

刪除驗證規則shoots_id。驗證適用於從表單發佈的數據,在這種情況下,無法從表單中發佈外鍵。您在buildRules()方法中已經有規則確保在保存前傳遞該值,因此刪除驗證是100%安全的。

+0

刪除驗證可讓其通過,但shoots_id未填充。 INSERT INTO shoot_meta(meta_key,meta_value)VALUES('photographer_spaces','123') – 2015-03-19 22:05:45

+0

如果在'buildRules()'中有一個規則,如果該列不存在,那麼您是如何得到'INSERT'的? – 2015-03-19 22:10:08

+0

這就是SQL日誌中出現的內容,檢查了表並執行它,將shoots_id設置爲0。 – 2015-03-19 22:15:12

0

我有一個像這樣以同樣的問題,現在我的解決方案是發送相關數據到其他功能/梅索德和保存。 如

**

public function add() { 
     $kantor = $this->Kantor->newEntity($this->request->data); 
     if ($this->request->is('post')) { 
      $kantor = $this->Kantor->patchEntity($kantor, $this->request->data); 
      $rgndata = $this->request->data['Telpkantor']; 
      $this->request->session()->write('rgndata', $rgndata); 
      if ($this->Kantor->save($kantor)) { 
       $result = $this->Kantor->save($kantor); 
       $this->addTelpKantor($rgndata, $result->id); 
       $this->Flash->success('The kantor has been saved.'); 
       return $this->redirect(['action' => 'index']); 
      } else { 
       $this->Flash->error('The kantor could not be saved. Please, try again.'); 
      } 
     } 
     $reffKota = $this->Kantor->ReffKota->find('list', ['limit' => 200]); 
     $statusKantor = $this->Kantor->StatusKantor->find('list', ['limit' => 200]); 
     $pimpinan = $this->Kantor->Pimpinan->find('list', ['limit' => 200]); 
     $jenisTelp = $this->Kantor->Telpkantor->Jenistelp->find('list', ['limit' => 200]); 
     $this->set(compact('kantor', 'reffKota', 'statusKantor', 'pimpinan', 'jenisTelp')); 
     $this->set('_serialize', ['kantor']); 
    } 
    public function addTelpKantor($rgndata = null, $kantor_id=null) { 
     if (!empty($rgndata[0]['noTelp'])) { 
      $this->loadModel('Telpkantor'); 
      foreach ($rgndata as $rgndata) { 
       $rgndata['kantor_id'] =$kantor_id; 
       $rgndatasave = $this->Telpkantor->newEntity($rgndata); 
       $this->Telpkantor->save($rgndatasave); 
      } 
     } 
    } 

**