0

大家好 CakePhp 开发人员,

CakePHP 2.5.2

我有一个带Agreements表的数据库。它属于成人、儿童、团体、价格和课程表。

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

我的Controller有相关数据的数组,它的数组没有Children、Adults、Groups等真实姓名。只有数字 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'));

我认为这个数组 $adults, $children 只有数字。为什么他们不检索真实的项目编号。我的协议/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小时,我不知道为什么?

我检查了数组,它看起来像这样: Array ( [1] => 1 [2] => 2 )

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

4

1 回答 1

0

在您的数据库表中重命名字段:

  • 成人名 > 名,
  • 成人姓氏 > 姓氏,
  • children_first_name > first_name,
  • 儿童姓氏 > 姓氏,
  • 组>名称
  • ...

在您的模型中创建虚拟字段:

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')
        ));
于 2014-06-16T14:02:18.957 回答