Cakephp 2.3: How to not use 'id' as the label of $belongsTo <select> options -


i run dilemma lot cakephp, , i'm prettty sure it's consequence of not following/understanding convention somewhere. in kind of belongsto (hasand or otherwise), find since id tends foreign key, ends being displayed in dropdown menus, , of course semantically useless.

here's example of i've done; i'm hoping can set me straight: (i'm making these tables because make template general problem.)

suppose:

users table has columns id|firstname|lastname

accounts has id|user_id

(nothing i'm doing simple, general form of problem)

using cake bake all, accounts/add view builds dropdown user_id literally displays id numbers. have found couple of ways around this, involve assembling information myself. i've either done building, in controller, $options array user_id dropdown of accounts/add so:

controller

$users = $this->account->user->find('list');

$options = set::combine($users, '{n}.user.id', array('{0} {1}', '{n}.user.firstname', '{n}.user.lastname'));

view

echo $this->form->input('user_id', array( 'div' => array( 'id' =>'userid'), 'options'=>$options,'type'=>'select' ) )."\n\n";

or else by:

controller

$users = $this->account->user->find('all', array('recursive' => 1) );

and parsing data @ view in same fashion. in either case, i've had lingering certainty building models in such way addresses this—it seems basic need have not been addressed such extensive framework.

for single column tables, simple $displayfield = 'column_name' answer.

but concatenations of columns, there's bit more code involved:

public $virtualfields = array("full_name"=>"concat(first_name, ' ' ,last_name)"); public $displayfield = 'full_name'; 

if complicated $displayfield, afterfind solution may necessary, cases solution.

hope helps.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -