PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] how can i concat first_name and last_name two different database fields to assign to drop down list in cakephp using 'list' in find statement

 January 20, 2022     cakephp     No comments   

Issue

$swimmer = $this->Swimmer->find('list', array(
    'conditions' => array('Swimmer.group' => $this->data['Swimmer']['group_id']),
    'order' => array('Swimmer.first_name ASC'),
    'fields' => 'Swimmer.first_name'
));

Solution

First of all, setup a virtual field in your Swimmer model as suggested by Ann Pham. eg:

var $virtualFields = array(
    'name' => "CONCAT(Swimmer.first_name, ' ', Swimmer.last_name)"
);

Then, fetch the data for your dropdown list like so: (assuming Swimmers controller)

$this->Swimmer->find('list', array('fields' => array('Swimmer.id', 'Swimmer.name')));

You could also try doing this in your SwimmerModel: var $displayName = 'Swimmer.name'; (not 100% sure if this would work). If it does work, you won't need the 'fields' array in the find.



Answered By - Dunhamzzz
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing