PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, March 10, 2022

[FIXED] Wrong table displayField being baked even though a name getter contained in entity

 March 10, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

This is the current displayField being set in my table:

$this->setDisplayField('id');

I need it to be:

$this->setDisplayField('name');

However, my database table has bilingual fields instead, such as name_en and name_fr, where I use a getter to populate name in the entity:

protected $_virtual = ['name'];

protected function _getName()
{
    if (Configure::read('wetkit.lang') == 'fr'){
        return $this->_properties['name_fr'];
    } else {
        return $this->_properties['name_en'];
    }
}
  • Should the baked table, with the virtual field in the entity, have displayField set to name during baking?

  • Perhaps the getter isn't available during the table bake process to determine the displayField and defaults to id?

  • Do I have to modify the table.twig file to output accordingly, depending on my structure having name_en and name_fr?


Solution

Ended up modifying the table.twig file:

{%- set setBilingualDisplayField = false %}
{%- for bf in bilingualFields %}
    {%- if bf.name == 'name' %}
        {%- set setBilingualDisplayField = true %}
    {%- endif %}    
{%- endfor %}
{% if setBilingualDisplayField %}
        $this->setDisplayField('name');
{% else %}
        $this->setDisplayField('{{ displayField }}');
{% endif %}


Answered By - TechFanDan
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing