Monday, April 15, 2013

Re: Filling in a drop down box - comments on code please



On Monday, March 12, 2012 5:32:14 PM UTC+5:30, Graham wrote:

Dear All. I would like some advice please. I am new to CakePHP and so far I am loving it. I am keen to do things the "right way" so I am interested in any comments.

 

What I describe in the following I have working. I am interested in knowing if I have gone about it the right or wrong way.

 

I have a table with a varchar field that I want to complete with data from another table – my choices table. There is no connection between the two tables.

 

In my controller I have the following:

 

      App::import('Model', 'Choice');

      $tmpChoice = new Choice();

 

      $titles = $tmpChoice->find('list', array(

          'fields' => array('item', 'item'),

          'conditions' => array('cat' => 'title'),

          'recursive' => 0

      ));

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

 

I have this in my view:

 

     echo $this->Form->input('title', array('type'=>'select','options'=>$titles));

 

Above, I needed to instantiate the Choice model class to make it available to my code. When I used:

 

$titles = $this->MainTable->Choice->find('list',…

 

I received an error saying that I was trying to execute a function of an object that isn't a class. I resorted to my above code to resolve the error.

 

The "fields" array specifies "('item', 'item')". This I did so that I could have the value and the option the same – I didn't want to insert the id key into the database, I wanted the text. The "conditions" statement is present so that I can select the appropriate category of choices.

 

Thanks for looking at this and making any comments about the code.

 

With regards

 

Graham Jones

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
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 cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: