Wednesday, November 30, 2011

Re: cakephp select option stored as 0 in database instead of selected value

I figured it out... I had to manually give me select 'name' =>
'data[Student][period_id]', and 'id' => 'StudentPeriodId'. I'm sure
I've strayed from cake's naming conventions a bit, thats why it didn't
hook up automatically.

On Nov 30, 3:53 pm, GG <jairusk...@gmail.com> wrote:
> When I change it to period_id the entire select box disappears.
>
> See how in the source code, it populates the correct information, but
> when it submits, the "option value" doesn't get sent to the correct
> field. It actually doesn't get sent anywhere. After submit, my table
> has a new entry that looks like:
>
> period_id:0
> user:_id:14 (the correct user)
>
> On Nov 30, 3:39 pm, euromark <dereurom...@googlemail.com> wrote:
>
>
>
>
>
>
>
> > I guess you made a mistake in your form
> > why is it not echo $this->Form->input('period_id');
> > (note the _id)?
>
> > tip:
> > let cake bake your code. this way less mistakes can be made and such
> > as above easily be avoided
>
> > On 1 Dez., 00:09, GG <jairusk...@gmail.com> wrote:
>
> > > The form is successfully adding a new entry to the table, the user_id
> > > is correct, however, the period_id is being stored as 0. They are both
> > > int(11).
>
> > > Problem:::: I need to tell cake to: store the value of the option
> > > being sent into the period_id column.
> > > Solution::::?????? Thanks for your help!
>
> > > Here is my database
>
> > > name:students
> > > field1:period_id
> > > field2:user_id
>
> > > Here is my StudentsController.php
>
> > >  public function add() {
> > >     if ($this->request->is('post')) {
> > >         $this->request->data['Student']['user_id'] = $this->Auth->user('id');
>
> > >         if ($this->Student->save($this->request->data)) {
> > >             $this->Session->setFlash('Class successfully added.');
> > >             $this->redirect(array('action' => 'index'));
> > >         }
> > >     }
>
> > >     $periods = $this->Period->find('list');
> > >     $this->set(compact('periods'));
>
> > > }
>
> > > Here is my add.ctp View
>
> > > echo $this->Form->create('Student');
> > > echo $this->Form->input('period');
> > > echo $this->Form->end('Add Class');
>
> > > Here is the source code from Students/add
>
> > > <div class="input select"><label for="StudentPeriod">Period</label>
> > > <select name="data[Student][period]" id="StudentPeriod">
> > > <option value="57">Class 1</option>
> > > <option value="56">Class 2</option>
> > > <option value="52">Class 3</option>
> > > <option value="51">Class 4</option>
> > > <option value="59">Class 5</option>
> > > </select>
> > > </div>

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: