2012-03-20 11:01


  • php
//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.

//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?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享