Cakephp how to create virtual field based off another table

爷,独闯天下 提交于 2019-12-06 07:42:12

问题


I have 2 tables: 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

How can I create a virtual field called projectTypeName with the name from projectType based on project_type_id and for those that project_type_id = 0, it will be ""

I have this

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

it correctly shows as "sales" but I don't want to specify it one by one.

Thank you


回答1:


This works for me

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


来源:https://stackoverflow.com/questions/20413830/cakephp-how-to-create-virtual-field-based-off-another-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!