У меня довольно много проблем с пониманием того, как 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'
)
);
}
TargetComponent
<?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'
)
);
}
Почему каждый раз, когда я попадаю в представление Add для TargetComponents, я не могу выбрать компонент List?
Как мне получить доступ к этим объектам? Все остальные отношения работают нормально, и я могу выбрать идентификатор, соответствующий записи в моей базе данных.
Это функция добавления моего контроллера:
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