我有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', '')"
);
它正确显示为“销售”,但我不想逐一指定。
谢谢
答案 0 :(得分:4)
这对我有用
public $virtualFields = array(
'projectTypeName' => 'SELECT name FROM project_types where id = Project.project_type_id'
);