dql7588 2012-03-20 11:01
浏览 33
已采纳

CakePHP在订购后选择框值不对

//My Owner Model
var $belongsTo = array(
    'Owner' => array(
        'className' => 'User',
        'foreignKey' => 'owner',
    ),
);

//My Tickets Controller

//Find the owners of the ticket to build select box
$owners = $this->Ticket->Owner->find('list', array(
         'fields' => array('Owner.id', 'Owner.username')
         'order' => array('Owner.username' => 'asc')
));

//Add "All" to the array of owners at 0 (no owner ids are 0)
array_splice($owners, 0, 0, "All");
//Set owner variable in my view.
$this->set(compact('owners'));

//My template file or view
echo $this->Form->create('Ticket', array(
    'url' => array_merge(array('action' => 'find'), $this->params['pass'])));
    echo $this->Form->input('title', array('div' => false));
    echo "<br /><br />";
            //Build the selectbox ordered by username
    echo $this->Form->input('owner', array('div' => false, 'options' => $owners, 'id' => 'EndUser'));
    echo "<br /><br />";
    echo $this->Form->submit(__('Search', true), array('div' => false));
    echo $this->Form->end();

When the select box is built the select box is sorted by username like I would like - but their ids do not switch with the username.

If I do a print_r($owners) before the array_splice($owners, 0, 0, "All"); the array is correct. When i print_r($owners) after the array_splice($owners, 0, 0, "All"); the array is incorrect - but I don't know why... I am assuming array_splice renumbers the array... How could add "Any" to the array without messing up my relationships?

  • 写回答

2条回答 默认 最新

  • dongxikuo5171 2012-03-20 11:34
    关注

    You'd best use the empty option of the input method of the Form helper. You can pass along a key to go with it, so change:

    echo $this->Form->input('owner', array('div' => false, 'options' => $owners, 'id' => 'EndUser'));
    

    To this:

    echo $this->Form->input('owner', array(
        'div' => false,
        'options' => $owners,
        'empty' => array(0 => 'All'), // Add this line
        'id' => 'EndUser'
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名