使用cakephp在下拉列表中显示名称 [英] Getting names to show in dropdown list with cakephp

查看:187
本文介绍了使用cakephp在下拉列表中显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下拉列表中显示我们所有项目负责人的名字。



项目领导只是在公司工作的一些员工。 / p>

这是我的表:

  project_leaders 
, ---,----------------,
| id | hr_employee_id |
| ---- | ---------------- |
| 1 | 18 |
'----'----------------'

projects
,----,---- - ,-------------------,
| id |名称| project_leader_id |
| ---- | ------ | ------------------- |
| 1 |蛋糕| 1 |
'----' ------'-------------------'

hr_employees
,----,------,---------,
| id |名称|姓氏
| ---- | ------ | ---------
| 18 | joe |污垢|
'----' ------'---------'

我的ProjectsController看起来像这样:

  public function add(){
if - > request-> is('post')){
$ this-> Project-> create();
if($ this-> Project-> save($ this-> request-> data)){
$ this-> _sendProjectRequestEmail($ this-& getLastInsertID());
$ this-> Session-> setFlash(__('项目已保存'));
$ this-> redirect(array('action'=>'index'));
} else {
$ this-> Session-> setFlash(__('无法保存项目,请重试。
}
}
$ this-> set('projectLeaders',$ this-> Project-> ProjectLeader-> find('list');
}

这只返回项目领导者的id,而不是名字和姓氏。 ,它返回1。



我试过做$ this-> Project-> ProjectLeader-> HrEmployee-> find('list'),但列出所有的员工。



我也试过指定字段,但返回未知的字段错误。



我做错了?

解决方案

  $ this-> set('projectLeaders',$ this-> Project-> ProjectLeader-> find('list')); 

将只列出 project_leaders 表中的记录,很可能是因为表本身不包含名称/标题字段(该蛋糕将自动作为 displayField )如下所示:

  ; 1 



使用适当的查找



要获得有意义的项目负责人名单,您需要确保您在 project_leaders hr_employees 一种方法是使用可包含的行为,并简单地指定

  $ projectLeaders = $ this-> Project-> ProjectLeader-> find list',array(
'contains'=> array(
'HrEmployee'
),
'fields'=> array(
'ProjectLeader.id',
'HrEmployee.name',

));
$ this-> set(Compact('projectLeaders'));您的数据库结构是否适合您的需要?


有一个表格相当于说这个用户是管理员可能不是最好的主意 - 这将更容易避免数据问题,并给你更简单的查询,如果表 project_leaders hr_employees 表和 project_leader_id 中指向一个布尔字段 hr_employee 表,不是一些其他数据抽象。



当然,我不知道你的整个模式,非常有可能是有一个单独的 project_leaders 表的好理由。



或者 - denormalize < h2>

如果您向 project_leaders 添加一个名称字段 - 您不需要加入就可以知道项目领导或任何funkiness:

  alter table project_leaders添加名称varchar(255) 
update project_leaders set name =(select name from hr_employees where hr_employees.id = hr_employee_id);

通过这种方式,您可以通过一个查询/联接知道谁是相关项目潜在客户,做两个连接以获得员工姓名。


I want to show the names of all our project leaders in a dropdown list.

The project leaders are only some of the employees that work in the company.

Here are my tables:

project_leaders
,----,----------------,
| id | hr_employee_id |
|----|----------------|
| 1  |  18            |
'----'----------------'

projects
,----,------,-------------------,
| id | name | project_leader_id |
|----|------|-------------------|
| 1  | cake |         1         |
'----'------'-------------------'

hr_employees
,----,------,---------,
| id | name | surname |
|----|------|---------|
| 18 | joe  | dirt    |
'----'------'---------'

My ProjectsController looks like this:

public function add() {
    if ($this->request->is('post')) {
        $this->Project->create();
        if ($this->Project->save($this->request->data)) {
            $this->_sendProjectRequestEmail($this->Project->getLastInsertID() );
            $this->Session->setFlash(__('The project has been saved'));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The project could not be saved. Please, try again.'));
        }
    }
    $this->set('projectLeaders',$this->Project->ProjectLeader->find('list');
}

This only returns the id of the Project Leaders, not the name and surname. So instead of Joe Dirt, it returns 1.

I've tried doing $this->Project->ProjectLeader->HrEmployee->find('list') but that lists all the employees.

I've also tried specifying the fields, but it returns a unknown field error.

What am I doing wrong?

解决方案

$this->set('projectLeaders',$this->Project->ProjectLeader->find('list'));

This will just list the records from the project_leaders table and most likely, as the table doesn't itself contain a name/title field (which cake would pick up automatically as the displayField) be like so:

array(
    1 => 1
)

Use an appropriate find

To get a meaningful list of project leaders, you need to ensure you get a join between project_leaders and hr_employees one way to do that is using the containable behavior and simply specifying which fields to use in the list:

$projectLeaders = $this->Project->ProjectLeader->find('list', array(
    'contain' => array(
        'HrEmployee'
    ),
    'fields' => array(
        'ProjectLeader.id',
        'HrEmployee.name',
    )
));
$this->set(compact('projectLeaders'));

Is your db structure appropriate for your needs?

Having a table equivalent to saying "this user is admin" may not be the best idea - it would be easier to avoid data problems, and give you simpler queries, if the table project_leaders was (only) a boolean field on your hr_employees table and project_leader_id pointed at the hr_employee table, not some other data-abstraction.

Of course I don't know your whole schema, there very well may be a good reason for having a seperate project_leaders table.

Alternatively - denormalize

If you add a name field to project_leaders - you don't need a join to know the name of a project leader or any funkiness:

alter table project_leaders add name varchar(255) after id;
update project_leaders set name = (select name from hr_employees where hr_employees.id = hr_employee_id);

In this way you can know who is the relevant project lead with one query/join instead of needing to do two joins to get an employee's name.

这篇关于使用cakephp在下拉列表中显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆