Home

April 9th, 2008

09:14 pm
replacing generateList()

OK, so to get more detailed into this. From the controller you want to pass along the dropdown list, just do this. You have two models "Incident" and "User". User model contains "firstname" and "lastname" field, but you want them to show up as "firstname, lastname" when the select is automagically generated.:
class IncidentsController extends AppController { 
...
	function add() {
		...
		$users = $this->Incident->User->find('all',
			array('fields' => array('id','lastname','firstname')), 0);
		$users = Set::combine($users,
				'{n}.User.id', /* key */
				array('{0}, {1}', /* formatting, note the comma in the middle */
					'{n}.User.lastname','{n}.User.firstname')); /* mapping to the format */
		...
		$this->set(compact('users', ... )); /* if it has just $this->set('foo'); replace with compact */
	}
...

Repeat those $users lines as often as you need to pass it to $this->set(); however, I would create a local private function and call the private function.
...
        function __genUserList() {
                $users = $this->Incident->User->find('all',
                        array('fields' => array('id','lastname','firstname')), 0);
                $users = Set::combine($users,
                                '{n}.User.id',
                                array('{0}, {1}','{n}.User.lastname','{n}.User.firstname'));
                return $users;
        }
...
	function add() {
		...
		$users = $this->__genUserList();
		$this->set(compact('users', ... ));
	}
...