Wednesday, November 24, 2010

Re: noob - Problems with default selected option

Hi,

As 'venues' table has 'city_id', the form field name should be 'city_id' and not 'cities'.

Amit Badkas

PHP Applications for E-Biz: http://www.sanisoft.com



On Wed, Nov 24, 2010 at 5:48 AM, lash <louisholbrook@gmail.com> wrote:
I have a view doing:

echo $form->input('cities');

and a controller doing:

 function edit($id = null) {
   $this->set('cities', $this->Venue->City->find('list'));
   $this->Venue->id = $id;
   if (empty($this->data)) {
     $this->data = $this->Venue->read();
   } else {
     $this->data['Venue']['city_id'] = $this->data['Venue']['cities']
['id'];
     $venue = $this->Venue->save($this->data);
     if (!empty($venue)) {
       $this->Session->setFlash('Your venue has been saved.');
       $this->redirect(array('action' => 'index'));
     }
   }
 }

DB table names are cities and venues (id columns in both tables are
named id)
venue belongsTo city.
foreignKey is city_id in the venue table.

The "cities" select box doesn't select the current value, only the top
value of the option list.
As far as I can see I'm following naming conventions, and this should
be working out of the box. The data is stored correctly in the db, so
I know the relation works.

It works if I add to the controller:

   $this->set('venue', $this->Venue->read());

and edit the input function in the view to:

  echo $form->input('cities', array('default' => $venue['City']
['id']));

...but that's clumsy compared to the "proper" solution.

What am I missing to get this to work?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.
 
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: