我有很多理解CakePHP做他的關聯的方式的問題。 考慮以下數據庫配置:CakePHP關聯問題
我做在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對這個問題您非常注重細節,其中包含你的數據庫設計的能效比,更多的人應該學會自己張貼類似這樣的問題! :) – Oldskool 2012-02-09 21:08:38
非常感謝你的讚美。我喜歡清楚我的代碼和所有外圍設備。我也覺得人們應該更經常地考慮這樣做,因爲這樣做可以讓彼此的幫助變得更容易。 – Craimasjien 2012-02-10 11:19:26