Tuesday, July 22, 2014

Booking a date

Hello everyone.

I'm struggling with this since a week ago and it's driving me crazy.

I'm doing a website for doing surveys in a forest.

There is many places, and each place has different points. An user can choose one of those points and book a day for a survey, but there can be only one survey in a point each day.

The models are: Place, Point, User, BookedSurvey

The controllers are: PlacesController, PointsController, UsersController, BookedSurveysControllers

The tables (omitting some that are not necessaries for this question) are places, points, users, booked_surveys; and inside of booked_surveys you can find the fields id (INT AUTO INCREMENT), survey_date (datetime), point_id (int) and user_id (int).

The views are: add, edit, remove, index for each one

When viewing a point, an user can select a date and press "Book survey". This is part of the view:

<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>

Then, the controler must search in booked_survey if the point has any survey for the same date. As each field should have only one survey per day, an error will be shown if there is any other survey in the same point for the same day. And this is the problem: it never finds any coincidence. I Googled, tried all the different options, and still is not working. This is my seach in the PointsController (it's simplified, at the moment I'm trying to search any survey on the same date):

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

To be honest, I don't know what is failing, but please, if somebody can help...

--
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: