Cakephp如何创建基于另一个表的虚拟字段

Cakephp如何创建基于另一个表的虚拟字段

本文介绍了Cakephp如何创建基于另一个表的虚拟字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:projects,project_types。

 关系如下:
Project =>属于=> ProjectType
ProjectType => hasMany =>项目

列如下:
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','')
);

它正确显示为sales,但我不想一一指定。 / p>

谢谢

解决方案

b
$ b

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


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

解决方案

This works for me

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

这篇关于Cakephp如何创建基于另一个表的虚拟字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 13:45