bmeulmeester
bmeulmeester

Reputation: 1117

CakePHP Association problems

I'm having quite some issues understanding the way CakePHP does his association. Consider the following database configuration:

enter image description here

I'm doing the relationships in the CakePHP code, like this:

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'
        )
    );
}

How come that whenever I get to my Add view of the TargetComponents I can't select a List Component? How do I access those objects? All the other relationships are working normally and I can select the ID that corresponds to a record in my database.

This is my controller's add function:

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'));
    }

and my view:

<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>

I'm stuck, and clearly doing something horrendously wrong.

Help is MUCH appreciated!

Thanks in advance

-B

Upvotes: 1

Views: 456

Answers (1)

Oldskool
Oldskool

Reputation: 34877

I've tested this on my local Cake environment with some dummy values in the database (1 for int fields and Test for varchar fields). It turns out that everything is actually properly associated, but the problem lies within the view.

For some reason Cake doesn't automagically put the values in the list_components_id dropdown (while by convention it should put the $list_components array in there). Forcing the options makes it work for me. So, in the view, replace:

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

With this:

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

This does the trick for me and populates the dropdown with the id's from the ListComponents model.

Upvotes: 3

Related Questions