开发者

Joins in cakephp using find

开发者 https://www.devze.com 2023-02-09 00:52 出处:网络
I have used join query which joins three tables employee, places and employee_places.The join query gives me all the results as I have used find(\'all\').But in the view page I just want to show emplo

I have used join query which joins three tables employee, places and employee_places.The join query gives me all the results as I have used find('all').But in the view page I just want to show employee name from employee table - place name from place table in a dropdown. e.g:- emp1-place1,emp2-place2 and so on. Where should I give the fields name in find. Look at the following code :

$options['joins'] = array( array('table' => 'employees', 'alias' => 'emp', 'type' => 'inner', 'conditions' => array( 'emp.id = EmployeePlace.employee_id ' ) ), array('table' => 'places', 'alias' => 'pl', 'type' => 'inner', 'conditions' => array( 'pl.id = EmployeeP开发者_C百科lace.place_id' ) ) );

$empTables = $this->Bill->EmployeePlace->find('all', $options);

$this->set(compact('empTables'));

The above query results the following array:-

Array ( [0] => Array ( [EmployeePlace] => Array ( [id] => 1 [employee_id] => 1 [place_id] => 1 [Date] => 2011-02-02 )

        [Employee] => Array
            (
                [id] => 1
                [image] => 
                [firstName] => Andy
                [lastName] => Murray
                [date_of_joining] => 2010-09-02
                [date_of_leaving] => 2011-02-02
                [date_of_birth] => 1991-08-10
                [gender] => Male
                [supervisor] => 0
                [designation] => Manager
                [user_id] => 0
            )

        [Place] => Array
            (
                [id] => 1
                [placeName] => table-1
                [section_id] => 1
                [position] => Left
                [seating_capacity] => 4
            )


    )

)

I just want firstName from employee - placeName from place table in the dropdown. How do I do this using condition in find.


Use containable: http://book.cakephp.org/view/1323/Containable

You will then only be dealing with data that you require.

Loop over the results and build the strings you want. Add the strings to an array indexed against the value you want the selection to return. Pass the array out to the view.

How are you going to handle the values? What do you want to do when a user has made a selection?

OR:

You could create a db view, which is a table that combines data from other tables on the fly. CakePHP can access this as a normal table.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号