The trouble is that there are 2 requests to the add action. The first,
which displays the form, has the date in the URL. But, when the form
is submitted, there is no date param. It points to
www.site.com/events/add. What you should do is create a hidden field
so that the date is already in $data.

function add($event_date = null)
{       
        if (!empty($this->data))
        {       
                if ($this->Event->save($this->data))
                {
                        ...
                }
                else
                {
                        /* set this in case form needs to be re-displayed
                         */
                        $this->set('event_date', 
$this->data['Event']['event_date']);
                }
        }
        else
        {
                /* set for initial display of form
                 */
                $this->set('event_date', $event_date);
        }
}

view:
$form->hidden('Event.event_date', array('value' => $event_date));

You might consider putting a check on the date before the initial
set() for the view. If null, or if date_parse() returns false, you
could set it to today:

$this->set('event_date', date('Y-m-d'));

There's no need for $this->Event->event_date = $event_date;

Also, although you don't need it as well, the substr() & mktime()
stuff could have been avoided by doing:

$v = date('Y-m-d', strtotime($event_date));

However, even that's superfluous because you might as well just do:

$v = $event_date;

On Thu, Aug 13, 2009 at 4:02 AM, Schwamm<[email protected]> wrote:
>
> I need urgent help!  I'm a novice at Cake and I'm having trouble
> getting a date saved in my SQL database.  I am trying to have the date
> save automatically depending on which day a user clicks on on a
> calendar to create a new event.  To do that, I added a parameter to my
> "add" function called $event_date, which was passed in the URL in the
> form YYYY-MM-DD, so my URL looks like www.site.com/events/add/2009-08-27
> .  This is my function currently:
>
> function add($event_date = null) {
>            $this->Event->event_date = $event_date;
>            $this->set('event_date',$event_date); //for use again in
> view
>            if (!empty($this->data)) {
>               $year = substr($event_date,0,4); //I broke my parameter
> down to use it in a unix time stamp.
>               $month = substr($event_date,5,2);
>               $day = substr($event_date,8,2);
>               $str = mktime(0,0,0,$month,$day,$year);
>               $v = date("Y-m-d",$str);
>
>                $this->data['Event']['event_date'] = $v; //here is
> where I think I have problems.
>                        if ($this->Event->save($this->data)) {
>                                $this->Session->setFlash('Your event has been 
> saved.');
>                                
> $this->redirect(array('controller'=>'events','action' =>
> 'calendar'));
>                                }
>            }
>        }
>
> I know this code is probably horrible and passing the whole date in
> one parameter is also not the best way to do things, but if anyone
> could enlighten me as to what I'm doing wrong it'd be very much
> appreciated!
>
> >
>

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

Reply via email to