2012-02-09 246 views
1

我有很多理解CakePHP做他的關聯的方式的問題。 考慮以下數據庫配置:CakePHP關聯問題

enter image description here

我做在CakePHP的代碼的關係,就像這樣:

AbstractComponent

<?php 
App::uses('AppModel', 'Model'); 
/** 
* AbstractComponent Model 
* 
*/ 
class AbstractComponent extends AppModel { 
/** 
* Display field 
* 
* @var string 
*/ 
    public $displayField = 'id'; 

    public $hasOne = array(
     'DetailComponent' => array(
      'className' => 'DetailComponent', 
      'foreignKey' => 'component_id' 
     ) 
    ); 

    public $hasMany = array(
     'TargetComponent' => array(
      'className' => 'ListComponent', 
      'foreignKey' => 'target_component_id' 
     ), 
     'ListComponent' => array(
      'className' => 'ListComponent', 
      'foreignKey' => 'component_id' 
     ) 
    ); 

} 

ListComponent

<?php 
App::uses('AppModel', 'Model'); 
/** 
* ListComponent Model 
* 
*/ 
class ListComponent extends AppModel { 
/** 
* Display field 
* 
* @var string 
*/ 
    public $displayField = 'id'; 

    public $belongsTo = array(
     'AbstractComponent' => array(
      'className' => 'AbstractComponent', 
      'foreignKey' => 'component_id' 
     ) 
    ); 

    public $hasOne = array(
     'TargetComponent' => array(
      'className' => 'TargetComponent', 
      'foreignKey' => 'list_component_id' 
     ) 
    ); 
} 

TargetC omponent

<?php 
App::uses('AppModel', 'Model'); 
/** 
* TargetComponent Model 
* 
*/ 
class TargetComponent extends AppModel { 
/** 
* Display field 
* 
* @var string 
*/ 
    public $displayField = 'id'; 

    public $belongsTo = array(
     'ListComponent' => array(
      'className' => 'ListComponent', 
      'foreignKey' => 'list_component_id' 
     ) 
    ); 
} 

DetailComponent

<?php 
App::uses('AppModel', 'Model'); 
/** 
* DetailComponent Model 
* 
*/ 
class DetailComponent extends AppModel { 
/** 
* Display field 
* 
* @var string 
*/ 
    public $displayField = 'title_label_text'; 

    public $belongsTo = array(
     'AbstractComponent' => array(
      'className' => 'AbstractComponent', 
      'foreignKey' => 'component_id' 
     ) 
    ); 
} 

爲什麼,每當我到TargetComponents我添加視圖我不能選擇列表組件? 如何訪問這些對象?所有其他關係正常工作,我可以選擇與我的數據庫中的記錄相對應的ID。

這是我的控制器的add函數:

public function add() { 
     if ($this->request->is('post')) { 
      $this->TargetComponent->create(); 
      if ($this->TargetComponent->save($this->request->data)) { 
       $this->Session->setFlash(__('The target component has been saved')); 
       $this->redirect(array('action' => 'index')); 
      } else { 
       $this->Session->setFlash(__('The target component could not be saved. Please, try again.')); 
      } 
     } 

     $list_components = $this->TargetComponent->ListComponent->find('list'); 
     $this->set(compact('list_components')); 
    } 

和我的觀點:

<div class="targetComponents form"> 
<?php echo $this->Form->create('TargetComponent');?> 
    <fieldset> 
     <legend><?php echo __('Add Target Component'); ?></legend> 
    <?php 
     echo $this->Form->input('type'); 
     echo $this->Form->input('list_components_id'); 
    ?> 
    </fieldset> 
<?php echo $this->Form->end(__('Submit'));?> 
</div> 
<div class="actions"> 
    <h3><?php echo __('Actions'); ?></h3> 
    <ul> 

     <li><?php echo $this->Html->link(__('List Target Components'), array('action' => 'index'));?></li> 
    </ul> 
</div> 

我堅持,並清楚地做一些窘況錯。

幫助非常感謝!

在此先感謝

-B

+1

+1對這個問題您非常注重細節,其中包含你的數據庫設計的能效比,更多的人應該學會自己張貼類似這樣的問題! :) – Oldskool 2012-02-09 21:08:38

+0

非常感謝你的讚美。我喜歡清楚我的代碼和所有外圍設備。我也覺得人們應該更經常地考慮這樣做,因爲這樣做可以讓彼此的幫助變得更容易。 – Craimasjien 2012-02-10 11:19:26

回答

3

我與數據庫(1 int字段和Test爲VARCHAR字段)一些虛擬值測試了這個在我的本地蛋糕環境。事實證明,一切事實上都是正確聯繫的,但問題在於這個觀點。

由於某些原因,Cake不會自動將值放入list_components_id下拉列表中(儘管按照慣例它應該將$list_components數組放在那裏)。強制選項使它適用於我。因此,在視圖中,將:

echo $this->Form->input('list_components_id'); 

有了這個:

echo $this->Form->input('list_components_id', array('options' => $list_components)); 

這是卓有成效的,我和填充用的ID從ListComponents模型的下拉列表。

+0

感謝您的回答!很不清楚爲什麼CakePHP不能'自動'找到綁定。 – Craimasjien 2012-02-10 11:19:59

+1

@Bryan是的,我不明白爲什麼。 AFAIK這種行爲在2.0中並沒有改變,所以在你首先想到的方式中沒有任何問題:) – Oldskool 2012-02-10 12:18:40

+0

感謝您幫助我解決這個特定問題。有沒有更容易溝通的方法?我想我可以從你身上學到很多東西! – Craimasjien 2012-02-11 23:06:50