0

The sections are related to the level and degree. So I need to show on the field select level, grade and section. Reading the cakephp 3 book found that using "valueField" I can show the field I want. But this time I need to show two fields.

In my controller:

$secciones =  $this->Users->Secciones->find('list', [
    'keyField' => 'id',
    'valueField' => ['nivele.nivel']



])->contain(['Niveles', 'Grados']);

When I put in the valueField ['nivele.nivel','grado.grado'] only show a semicolon.

How can display two relationship in the select field?

Thanks.

I readed the solution in How do I create a keyValue pair by combining/having two fields in CakePHP 3? but in that case no relation. In my case i need to use two fields with relation.

Reading the solution in How do I create a keyValue pair by combining/having two fields in CakePHP 3? I did this:

     $secciones = $this->User->Secciones
->find()
->select(['id', 'nivele_id', 'grado_id'])
->formatResults(function($results) {
    /* @var $results \Cake\Datasource\ResultSetInterface|\Cake\Collection\CollectionInterface */
    return $results->combine(
        'id',
        function($row) {
            return $row['nivele.nivel'] . ' ' . $row['grado.grado'];
        }
    );
})
->contain(['Niveles', 'Grados']);

But I have a fatal error :-(

Community
  • 1
  • 1
  • Possible duplicate of [How do I create a keyValue pair by combining/having two fields in CakePHP 3?](http://stackoverflow.com/questions/32999490/how-do-i-create-a-keyvalue-pair-by-combining-having-two-fields-in-cakephp-3) – ndm Oct 13 '15 at 02:04
  • "_... but in that case no relation ..._" Unless there is more to this than what you are actually describing in your question, it is _exactly_ the same problem, and whatever your associations look like, the former mentioned solution is definitely applicable. – ndm Oct 13 '15 at 12:46
  • It is not the same problem. I do not know how to place the relationship in the query – Jose Rafael Camejo Oct 13 '15 at 12:52
  • "_... how to place the relationship in the query_" I have no clue what exactly that means, and you haven't mentioned anything like that initially. Please add some proper explanation to your question. – ndm Oct 13 '15 at 12:55
  • Sorry my english. I updated the question. – Jose Rafael Camejo Oct 13 '15 at 13:19
  • Besides the fact that this is not how you access nested properties/array keys, which should be more like `$row['nivele']['nivel']`... this should not cause a fatal error. When you receive an error, then please post the complete, exact error message (and for non-fatal errors also the stracktrace). ps, depending on the type of association (I can only assume that these are `belongsTo` ones), you may need to add the fields that you want to combine to the `select()` list, ie `Niveles.nivel` and `Grados.grado`. – ndm Oct 13 '15 at 14:57

0 Answers0