Problem filling OPTION / COMBOBOX / DROPDOWNLIST CakePHP

0

I'm having a problem wanting to fill a Combobox / dropdownList in CakePHP

I fill it up like this:

    public function obtComunas()
{
    $Comuna = $this->ManComuna->find()->select(['codigo_comuna', 'descripcion'])->toArray();
    return $Comuna;
}

and at the hearing I show it like this:

            echo $this->Form->input('codigo_comuna',  
                                array(
                                'type'    => 'select',
                                'options' => $Comunas,
                                'empty'   => false
                                ));

The problem is that I do not know how to tell the combo what the value is and what the text is, therefore it looks like this:

Thank you in advance for any comments.

Greetings.

    
asked by Aldo 20.12.2016 в 17:39
source

3 answers

2

Thanks for the help, but I found a solution. I do not know if it will be the best or the right thing, but it worked for me.

In the consultation I added a foreach and filled in another array with the values.

    public function obtComunas()
{
    $Comuna = $this->ManComuna->find()->select(['codigo_comuna', 'descripcion']);
    $st=array();
    foreach ($Comuna as $row) {
        $st[$row['codigo_comuna']] = $row['descripcion'];
    }
    return $st;
}

I left the view as is.

Greetings.

    
answered by 20.12.2016 / 19:54
source
1

For a select list you need to use a find list Our example of conditions, can put the condition you want to meet your query and remember that the recursive indicates the level of depth to which we want to enter relationships.

controller

$this->loadModel('clase_producto');

$productos = $this->clase_producto->find('list', array('conditions' => 
array('clase_producto.idel' => 0), 'recursive' => 0));

$this->set(compact('productos'));

view

$form->input('producto_id',array('type'=>'select','options'=>$productos));
    
answered by 05.09.2018 в 21:32
0

The correct way to do it would be with find list and defining keyPath and valuePath

TuController / obtComunas

public function obtComunas()
{
    $comunas = $this->ManComuna->find('list', ['keyPath' => 'codigo_comuna','valuePath' => 'descripcion']);

    $this->set(compact('comunas'));
}

Vista

<?= $this->Form->input('codigo_comuna',['type' => 'select', 'options' => $comunas, 'empty' => false]); ?>
    
answered by 22.12.2016 в 13:32