控制器可以查看相关字段,但不会在添加或编辑操作中显示它

时间:2014-06-16 12:57:02

标签: php mysql cakephp orm cakephp-2.5

Hello CakePhp开发人员,

CakePHP 2.5.2

我有一个带有Agreements表的数据库。它属于Adults,Children,Groups,Price and Courses表。

我的控制器有3个操作:indexaddedit。 第一个,index,工作正常,在一个页面中显示所有相关的表。 但问题在于编辑和添加操作。

我的控制器具有包含相关数据的数组,其数组的实际名称不包括子项,成人,组等。只有数字1,2,3等

我的AgreementsController.php正在通过以下命令向View发送数据:

$this->Agreement->recursive = 1;
$adults = $this->Agreement->Adult->find('list');
$childrens = $this->Agreement->Children->find('list');
$groups = $this->Agreement->Group->find('list');
$prices = $this->Agreement->Price->find('list');
$courses = $this->Agreement->Course->find('list');
$this->set(compact('adults', 'childrens', 'groups', 'prices', 'courses'));

我认为这个阵列$成人,$孩子,只有数字。他们为什么不检索真实的物品编号。我的协议/ add.ctp是:

<?php echo $this->Form->create('Agreement'); ?>

    <?php
        echo $this->Form->input('date_of_agreement');
        echo $this->Form->input('number');
        echo $this->Form->input('year');
        echo $this->Form->input('adult_id', array (                      
                        'type' => 'select', 
                        'options' => $adults 
                        ) 
                );
                print_r($adults);
        echo $this->Form->input('children_id');
        echo $this->Form->input('status_id');
        echo $this->Form->input('city_id');
        echo $this->Form->input('price_id');
        echo $this->Form->input('course_id');
        echo $this->Form->input('group_id');
    ?>

<?php echo $this->Form->end(__('Submit')); ?>

即使添加'选项'参数也无法解决问题。 我花了50个小时,我不知道为什么?

我检查了数组它看起来像这样: 数组([1] =&gt; 1 [2] =&gt; 2)

你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

在db表中重命名字段:

  • 成人first_name&gt; first_name,
  • adult_last_name&gt;姓氏,
  • children_first_name&gt;如first_name,
  • children_last_name&gt;姓氏,
  • 组&gt;名称
  • ...

在模型中创建虚拟字段:

public $virtualFields = array(
'name' => "CONCAT(Adult.first_name, ' ', Adult.last_name)"
);

清除缓存和测试。

修改

或者您可以在查找列表方法中添加选项,例如:

$adults = $this->Agreement->Adult->find('list',array(
        'fields'=>array('Adult.id','Adult.first_name')
        ));