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

Friday, February 18, 2022

[FIXED] Grouping a select list (optgroup) in CakePHP 3

 February 18, 2022     cakephp, cakephp-3.0, php     No comments   

Issue

I am trying to make a list of grouped things in CakePHP 3, to create a grouped list of things in a select list in a form. I'm not sure if I am missing something or if I'm expecting too much of Cake and should be doing more myself.

I have a controller called Issues and a self-referencing column called RelatedIssues. Each Issue belongs to a System, and it's the systems I want the issues grouped by.

In my IssuesTable.php:

$this->belongsTo('RelatedIssues', [
    'className' => 'Issues',
    'foreignKey' => 'issue_id'
]);

$this->belongsTo('Systems', [
    'foreignKey' => 'system_id',
    'joinType' => 'INNER'
]);

...and in my IssuesController's edit method:

$relatedIssues = $this->Issues->RelatedIssues->find('list', [
    'groupField' => 'system_id'
]);

When I get to the drop-down list, items are grouped by system_id as specified, but I cannot figure out how to get them grouped by the System's title field. Is this even possible, or do I have to write a nice nested foreach structure to do this myself?


Solution

should be (can'try it now):

$relatedIssues = $this->Issues->RelatedIssues->find('list', [
    'groupField' => 'system.title'
])->contain('Systems');


Answered By - arilia
  • 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