3

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

Enzero
  • 1,141
  • 2
  • 17
  • 36
Harts
  • 4,023
  • 9
  • 54
  • 93
  • Seems to me that you should be storing ProjectType name on your ProjectType model, and then just get the associated ProjectType when you get your Project in order to know the ProjectType name. – Kai Dec 06 '13 at 01:01
  • that does not work, because I plan to use pagination through other model, where I can not go deeper to ProjectType. that's why I plan to use virtual field. I have questions in here but since no reply, that's why I plan to do other way http://stackoverflow.com/questions/20412999/containable-to-do-show-deeper-data-or-join-table/20413134?noredirect=1#20413134 – Harts Dec 06 '13 at 01:14

1 Answers1

4

This works for me

public $virtualFields = array(
 'projectTypeName' => 'SELECT name FROM project_types where id = Project.project_type_id'
);    
Harts
  • 4,023
  • 9
  • 54
  • 93