Wednesday, July 23, 2014

Re: Finding if a event exists for a date

Check out http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::format

You can pass your date and specify which date format you want (see http://www.php.net/date), so something like

$date = $this->request->data['BookedSurvey']['survey_date'];
$formattedDate = CakeTime::format("{$date['year']}-{$date['month']}-{$date['day']}", "%M %D, %Y);

I imagine there's a much better way to do this but I'm strapped for time right now to think any harder, sorry! :)


On 23 July 2014 12:11, Jumy Elerossë <jumyoriginal@gmail.com> wrote:
Thank you very much Stephen. I only have a noob question... How can I tell to my Form to send the date in that format? 

This is the code of the button that I have in my view:

echo $this->Form->input('BookedSurvey.survey_date', array(
                        'type'=>'date',
                        'label' => '',
                        'dateFormat' => 'DMY',
                        'minYear' => date('Y'),
                        'minMonth' => date('M'),
                        'minDay' => date('D'),
                        'div' => 'col col-md-9',
                        'style' => 'margin: 15px 5px 5px 0px'
                            ));

And when I do a
print_r($this->request->data['BookedSurvey']['survey_date']);

it send the date as 
Array ( [day] => 22 [month] => 01 [year] => 2014 )


It's the only thing that I can't make to work...


On Wednesday, 23 July 2014 11:35:42 UTC+1, Stephen S wrote:
Add the following above your controller class:

App::uses('CakeTime', 'Utility');

This will give you access to the CakeTime utility within your controller ($this->Time references the helper which is for use in views) Something like this should work I think, syntax may be a little off maybe, I haven't had time to test.

$isBooked = $this->Point->BookedSurvey->find('count', array(
    'conditions' => CakeTime::dayAsSql('Jul 23, 2014', 'survey_date') // replace the date as you see fit, using Jul 23, 2014 as an example
));


On 23 July 2014 11:21, Jumy Elerossë <jumyor...@gmail.com> wrote:
Thank very much for your help.

Yes, survey_date is using datetime, so the code should be something like this?

if ($this->request->is('post')) {
            
        
            $isBooked = $this->Point->BookedSurvey->find('count', array(
                'conditions' => array(
                    $this->Time->dayAsSql('BookedSurvey.survey_date') => date('Y-m-d') // Maybe pass an actual specific date rather than just today
                )
            ));
            
            echo "status";

            if($isBooked > 0) {
                echo "Already booked";
            }

             
        }


Because it returns this error:

Fatal Error

Error: Call to a member function dayAsSql() on a non-object 


On Wednesday, 23 July 2014 08:41:00 UTC+1, Stephen S wrote:
You could try something like the following

$isBooked = $this->Point->BookedSurvey->find('count', array(
    'conditions' => array(
        'BookedSurvey.survey_date' => date('Y-m-d') // Maybe pass an actual specific date rather than just today
    )
));

if($isBooked > 0) {
    echo "Already booked";
}

If your survey_date field is using datetime, you can check between 00:00:00 and 23:59:59 using the TimeHelper::dayAsSql method here http://book.cakephp.org/2.0/en/core-libraries/helpers/time.html#TimeHelper::dayAsSql

I'd recommend using the time helper where applicable really, you'd need to use CakeTime:: if you plan to do this in a controller or model.

Hope this helps


On 23 July 2014 01:44, Jumy Elerossë <jumyor...@gmail.com> wrote:
Hello everyone.

I'm struggling with a problem since a week ago, and this is driving me crazy.

I'm doing a website for doing surveys in the forest. This is the schema:

1 place have several points, and for each point, an user can book a day for doing a survey. If that point has booked already a survey for that date, an error will be shown.

I have the following tables: places, points, users and booked_surveys.

The table booked_surveys has the following fields: id, survey_date, point_id and user_id

Models: Place, Point, User, BookedSurvey

Controllers: PlacesController, PointController, UserController, BookedSurveysController

Views: add, edit, index and view for each one.

When the user is viewing a point, there's a date selector in the view for booking:

<h2>Book a date:</h2>
        
            <?php echo $this->Form->create('BoostCake', array(
                'inputDefaults' => array(
                    'div' => 'form-group',
                    'label' => array(
                        'class' => 'col col-md-1 control-label'
                    ),
                    'wrapInput' => 'col col-md-9',
                    'class' => 'form-control'
                ),
                'class' => 'form-horizontal'
            )); ?>
                
                <?php echo $this->Form->create('Point');
                    

                    echo $this->Form->input('BookedSurvey.survey_date', array(
                        'type'=>'date',
                        'label' => '',
                        'dateFormat' => 'YMD',
                        'minYear' => date('Y'),
                        'minMonth' => date('M'),
                        'minDay' => date('D'),
                        'div' => 'col col-md-9',
                        'style' => 'margin: 15px 5px 5px 0px'
                            ));

                    echo $this->Form->hidden('User.id', array(
                                'value' => $user_id)
                            );

                ?>



                <div class="form-group">
                    <?php echo $this->Form->submit('Book survey', array(
                        'div' => 'col col-md-9',
                        'class' => 'btn btn-success btn-lg',
                        'style' => 'margin: 10px 5px 5px 10px'
                    )); ?>
                </div>

And then, the PointsControllers looks if there is someone that already booked for that day. And this is what is driving me crazy, because I'm not able to doing it to work. What I tried is this:

if ($this->request->is('post')) {
            
        
            
            // Begin of comprobation
        
             $booked_condition = $this->Point->BookedSurvey->find('first', 
                   array('conditions'=>array(
                                             'DATE(BookedSurvey.survey_date)'=>'date()')));
           
            
            
            if ($booked_condition){
                
                echo "Already booked"; 
            }
            

If anyone can PLEASE give some light to this... Please....

--
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+u...@googlegroups.com.
To post to this group, send email to cake...@googlegroups.com.

Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards
 Stephen Speakman

--
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+u...@googlegroups.com.
To post to this group, send email to cake...@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards
 Stephen Speakman

--
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.
For more options, visit https://groups.google.com/d/optout.



--
Kind Regards
 Stephen Speakman

--
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.
For more options, visit https://groups.google.com/d/optout.

No comments: