duanhuang1967 2014-11-30 13:32
浏览 48

CakePHP virtualField和displayField用于多个对象

I have a little problem with the displayField in combination with virtualFields.

I have a File that belongsTo 2 different Users.

public $belongsTo = array(
    'Creator' => array(
        'className' => 'User',
        'foreignKey' => 'creator_id',
    ),
    'Editor' => array(
        'className' => 'User',
        'foreignKey' => 'editor_id',
    )
);

As virtualField and displayField in the User Model. I use the following code:

public $virtualFields = array(
  'full_name' => "CONCAT(first_name, ' ',last_name)"
);

public $displayField = 'full_name';

Creating a new File works fine, but if I want to view all my files I get the following error:

Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'first_name' in field list is ambiguous

I already read about it a little here: http://book.cakephp.org/2.0/en/models/virtual-fields.html At the end there are some limitations about virtualFields, so I tried to add this to the Controller:

$this->virtualFields += $this->Creator->virtualFields;
$this->virtualFields += $this->Editor->virtualFields;

But somehow this doesn't change anything.

Hope you can help me with this little problem.

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongwei8729 2014-11-30 22:40
    关注

    I guess you need 2 virtual fields:

    $this->File->virtualFields = array(
        'Creator__full_name' => "CONCAT(Creator.first_name, ' ',Creator.last_name)",
        'Editor__full_name' => "CONCAT(Editor.first_name, ' ',Editor.last_name)"
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题