create an optgroup with cakephp 3.x

Previous Topic Next Topic
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

create an optgroup with cakephp 3.x

V Ian
i'm novice in cakephp, and i'm having dificulties in something like this:

starting with this sql query:

select as service_group_name, as service_group_id, as campaign_name, as campaign_id
      from group_service
      join group_service_campaign on = group_service_campaign.group_service_id
      join campaign on group_service_campaign.campaign_id =
      where = 'Y'
      and in ($in)
      and campaign.company_id = ${_SESSION['companyId']}
      order by,

In result, i have something like this:

service_group_name | service_group_id | campaign_name | campaign_id |
service_1 | 01 | travel to sicilia | 14 |
service_1 | 01 | travel to greece | 15 |
service_2 | 02 | play the xyz game | 22 |
service_2 | 02 | play the return of xyz game | 234 |

and so on....

I converted it to this in my controller:

$var_campaign = $this->loadModel('Service_Group');
$var_campaign = $this->Group_Service->find('list', ['keyfield' => 'campaign_id', 'valueField' => 'campaign_name', 'groupField' => 'service_group_name'])
            ->join(['table' => 'service_group_campaign',
                'type' => 'inner',
                'conditions' => ' = service_group_campaign.service_group_id'
            ->join(['table' => 'campaign',
        'type' => 'inner',
        'conditions' => 'service_group_campaign.campaign_id ='
    $var_campaign->where(['' => 'Y']);

As can see, one thing i don't found an equivalent AS in querybuilder in cake, this is my first difficult.

My other difficult is to how use this to create an select form element with optgroup.

I've searched for examples and questions similar, but all i tried don't work.

My view:

echo $this->Form->create();
echo $this
->Form->input('id', array('label' => 'Services :', 'type' => 'select', 'empty'=> '-- Select --', 'options' => $var_campaign));
echo $this
->Form->select("field", $var_campanha);
echo $this

When i use find('list' ...) in query statement, i get a blank response, if i use find('all' ...) i get a not complete response, because all campaign names dont come in response, only the services group names with their id's.

My optgroup show a plain text like:

{"id": -1, "name": "Active Proposals","mod": "W","company_id": 10}

and so on.

Where i'm going wrong ?

thanks in advance.

Like Us on FaceBook
Find us on Twitter

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at
For more options, visit