I need to make a dropdown list of a staff. In dorpdown i need to put last name and first name. I already have a column in database name which contain first name and last name. But i need to show order last name then first name.
I tied to put CONCAT in query its working but the array result not showing properly. In this case I found that if i put a virtual fields in model that help me to fetch proper data.
var $virtualFields = array('dropdown_name' => 'CONCAT(Staff.last_name, " ", Staff.first_name)');
In controller query:
// Generate list $staff_list = $this->Staff->find("list", array( "fields" => array("id", "dropdown_name"), "order" => array("Staff.name ASC"), "conditions" => array('Staff.status' => '1') ));
The above code give me the proper list of staff.
In view to generate dropdown
echo $form->input("Reports.staff_id", array( 'options' => $staff_list, 'empty' => '--Please Select--' , 'label'=> false, ));
In this way we can generate dropdown with different column in a one field.
The following I tried
$staff_list = $this->Staff->find("list", array( "fields" => array("Staff.id", "CONCAT(`Staff`.`last_name`, ' ', `Staff`.`first_name`)"), "order" => array("AgencyStaff.name ASC"), "conditions" => array('Staff.status' => '1') ));
The query is runing properly but the result array doesn’t keep the CONACT fields, it only keep the id balnk.
But I run the query in cakephp and check what actually give it the result .
$list = $this->Staff->query("SELECT `Staff`.`id` , CONCAT( `Staff`.`last_name` , ' ', `Staff`.`first_name` ) FROM `staffs` AS `Staff` WHERE `Staff`.`status` =1 ORDER BY `Staff`.`name` ASC");
Result is the following
Array (  => Array ( [Staff] => Array ( [id] => 5 )  => Array ( [CONCAT( `Staff`.`last_name` , ' ', `Staff`.`first_name` )] => Johnson Ashley ) ) ................ ................
Thats why cakephp couldn’t make the proper array if we use their ORM.
I tried to use CONCAT( `Staff`.`last_name` , ‘ ‘, `Staff`.`first_name` ) as `Staff`.`name`
but it still not working.
SO I choose the virtual fields concept and it working properly for me 🙂
Shruti Seo said:
The CakePHP framework provides a robust base for your application. It can be handle every aspect, from the user’s initial request all the way to the final rendering of a web page. And since the framework follows the principles of MVC, This concept is simple but powerful. It allows you to easily customize and extend most aspects of your application. The framework also provides a basic organizational structure, from filenames to database table names, keeping your entire application consistent and logical.
Contact Person:-Sanjay Sahoo
Contact No:-+1 646-652-6895(toll free for UK/USA)
Mobile No:- 9811406536
carpet cleaning st paul said:
Thanks for the $virtualFields! It saved me time. 🙂
You’r unquestionably right with this piece!!!
Pingback: CONCAT in query , Virtual Fields and make a drop down « PHP Solutions
Many thanks for your elegant and simple solution!
Other solutions have talked about overriding find or performing some post find processing, but yours simply does what I would have done in pure SQL, and that’s what I wanted.
It’s a shame your solution hasn’t received more comments. Maybe because some have done the overriding and post-processing as advised!
Thanks for the virtual_fields information it saved my time.
Ladder GolfAnother tossing game, this one requiring a ladder-like structure, ladder golf is also known in some parts as Bolo Toss. Players take turns tossing a bolo–two golf balls connected by a string–at a three-rung ladder in an attempt to get the bolo to wrap around and stay on one of the rungs. Each rung is assigned a point value, with the top rung being worth one point, the middle run two points and the bottom rung three points. Players toss three bolos each round in an attempt to get as many as possible on the rungs. A player who successfully gets one bolo on each rung earns a bonus point. The first player to 21 points wins. foakleys
Pingback: CakePHP Concatenation | Rocketships
I have noticed you don’t monetize your page, don’t waste your traffic, you can earn additional
cash every month because you’ve got high quality content.
If you want to know how to make extra $$$, search for: Boorfe’s tips
best adsense alternative
Your style is really unique compared to other people I’ve read stuff from.
Thanks for posting when you have the opportunity,
Guess I’ll just book mark this page.
Thank for saving time $virtualFields 😉