简体   繁体   English

CakePHP关联问题

[英]CakePHP Association problems

I'm having quite some issues understanding the way CakePHP does his association. 我有很多问题需要了解CakePHP的关联方式。 Consider the following database configuration: 考虑以下数据库配置:

在此输入图像描述

I'm doing the relationships in the CakePHP code, like this: 我正在做CakePHP代码中的关系,像这样:

AbstractComponent 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 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 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 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? 每当我进入TargetComponents的Add视图时,为什么我不能选择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. 所有其他关系都正常工作,我可以选择与数据库中的记录对应的ID。

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

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). 我在我的本地Cake环境中测试了这个,在数据库中有一些虚拟值(1表示int字段, Test表示varchar字段)。 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). 由于某种原因,Cake不会自动将值放在list_components_id下拉列表中(按照惯例,它应该将$list_components数组放在那里)。 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. 这对我有用,并使用ListComponents模型中的ID填充下拉列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM