cakephp查找('list')选择框

时间:2011-11-26 07:31:56

标签: cakephp cakephp-1.3

我正在尝试在我的网站中实现选择框。该选择框的选项在我的控制器中设置,如下所示

$tmp_user = $this->User->find('first',array('id'=>$this->Auth->user('id')));
                $zip_info = $this->Zipcode->find('first',array('id'=>@$tmp_user['User']['zip_id']));
                $region_admins = $this->AdminRegion->find('all',array('conditions'=>array('AdminRegion.region_id'=>@$zip_info['Zipcode']['region_id'])));

if(!empty($region_admins)){         
                    foreach($region_admins as $radmn):
                        //pr($radmn);
                        $admin_user = $this->User->find('list',array('conditions'=>array('id'=>$radmn['AdminRegion']['user_id']),'fields'=>array('id','username')));                
                        pr($admin_user);
                        $this->set('users',$admin_user);
                    endforeach;

当我从控制器打印时,我正在获得这样的价值

Array
(
    [137] => governmentuser1
)
Array
(
    [198] => testadmin
)
Array
(
    [215] => adminregion
)
Array
(
    [224] => testcompany1234
)
Array
(
    [225] => testuser12345678
)

但只有最后一个值在选择框中设置.... 我哪里弄错了?

2 个答案:

答案 0 :(得分:1)

这样的事情怎么样:

<?php
    $tmp_user = $this->User->find('first', array(
                    'id' => $this->Auth->user('id')
                ));

    $zip_info = $this->Zipcode->find('first', array(
                    'id' => @$tmp_user['User']['zip_id']
                ));
    $region_admins = $this->AdminRegion->find('list', array(
                        'conditions' => array(
                            'AdminRegion.region_id' => @$zip_info['Zipcode']['region_id']
                        ),
                        'fields' => array('AdminRegion.user_id', 'AdminRegion.user_id')
                     ));

    $admin_users = $this->User->find('list', array(
                        'conditions' => array(
                            'id' => $region_admins
                        ),
                        'fields' => array(
                            'id','username'
                        )
                    ));

    ?>

一些附注:

  • 为什么要对用户数据进行另一次查询?在会议中不是全部吗?
  • 我不会在任何地方使用@,它会更慢并且会抑制错误。这也将使调试成为一场噩梦。

答案 1 :(得分:0)

我认为你错过了阵列:

$admin_user[] = $this->User->find('list',array('conditions'=>array('id'=>$radmn['AdminRegion']['user_id']),'fields'=>array('id','username')));                

希望有所帮助