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

Reply via email to