Cakephp如何根据另一个表创建虚拟字段

时间:2013-12-06 00:41:46

标签: mysql sql database cakephp cakephp-2.0

我有2个表:projects,project_types。

The relations are as follow:
    Project => belong to => ProjectType    
    ProjectType => hasMany   => Project

The columns are as follow
    Project => id, project_type_id, name, description  
    ProjectType => id, name

如何创建名为projectTypeName的虚拟字段,其名称来自projectType,基于project_type_id,而对于那些project_type_id = 0,它将是“”

我有这个

public $virtualFields = array(
    'projectTypeName' => "IF(Project.project_type_id = 9, 'sales', '')"     
);    

它正确显示为“销售”,但我不想逐一指定。

谢谢

1 个答案:

答案 0 :(得分:4)

这对我有用

public $virtualFields = array(
 'projectTypeName' => 'SELECT name FROM project_types where id = Project.project_type_id'
);