使用 find 加入 cakephp

发布于 2024-10-16 13:16:10 字数 1932 浏览 5 评论 0原文

我使用了连接查询,该查询连接三个表employee、places 和employee_places。连接查询为我提供了所有结果,因为我使用了find('all')。但在视图页面中,我只想显示员工表中的员工姓名 -下拉列表中地点表中的地点名称。 例如:- emp1-place1、emp2-place2 等等。我应该在哪里给出 find.txt 中的字段名称? 看下面的代码:

$options['joins'] = array( array('表' => '员工', '别名' => '雇员', '类型' => '内', '条件' =>大批( 'emp.id = EmployeePlace.employee_id ' ) ), array('表' => '地点', '别名' => 'pl', '类型' => '内', '条件' =>大批( 'pl.id = EmployeePlace.place_id' ) ) );

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

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

上述查询结果如下数组:-

数组 ( [0] =>大批 ( [员工地点] =>大批 ( [id] => 1 [employee_id] => 1 [地点 ID] => 1 [日期] => 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
            )


    )

)

我只想要来自员工的名字 - 来自下拉列表中的地点表的地点名称。我如何在 find 中使用条件来做到这一点?

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 = EmployeePlace.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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2024-10-23 13:16:10

使用可包含的:http://book.cakephp.org/view/1323/Containable

然后将只处理您需要的数据。

循环结果并构建您想要的字符串。将字符串添加到根据您希望选择返回的值进行索引的数组中。将数组传递给视图。

你将如何处理这些价值观?当用户做出选择后你想做什么?

或者:

您可以创建一个 db 视图,它是一个表动态组合来自其他表的数据。 CakePHP 可以像访问普通表一样访问它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文