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)"
    );
    
    评论

报告相同问题?