sábado, 30 de janeiro de 2016

DROPDOWNLIST - You want to show the name, not the id. Me too. That's how I did it.

Hi!

Straight to the point.

        First, I was studying the tutorial. And was working with treelist. This is no good for me. So, I asked myself,  how do make this dropdownlist show the name? The standard answer is, "YOU SHOULD USE VIRTUAL FIELD". And I found it really annoying. So, I found out this solution.

        //You write this in your controller

        $query = $this->Articles->Categories->find('all', array('fields' => array('id', 'name')));
        foreach($query as $row)
        {
            $id = $row['id'];
            $name = $row['name'];
            $categories[$id] = $name;
        }

       //You write this in  your template
     
       echo $this->Form->input('category_id', array('type' => 'select','options'=> $categories));

And you are done! And of story!

Good luck!

PS: I'm new to cakePHP. This was really annoying to find out.

More informations click on the link bellow

http://stackoverflow.com/questions/19920094/cakephp-format-findall-to-list-in-view

Nenhum comentário:

Postar um comentário