On Mon, Oct 18, 2010 at 4:18 AM, Rocco Luca
<[email protected]> wrote:
> Hello everyone, I state that I am new to the world of cakephp.
> I'm rewriting a program from a php to MVC. I need
> add a function that checks mean it is booked in a
> range of time from the input fields "from" and "until".
>
> In PHP also make a query like:
>
> $query = "SELECT * FROM calendar WHERE id='$id_mean' AND
> confirm='1' AND (
> ('".$from."' <= from AND '".$until."' >= until) OR
> ('".$from."' <= from AND '".$until."'<= until AND '".$until."' >=
> from) OR
> ('".$from."' >= from AND '".$until."'<= until)OR
> ('".$from."' >= from AND '".$from."' <= until AND '".$until."'>=
> until))";
> $results = mysql_query($query) or die (mysql_error());
> $num = mysql_num_rows($results);
>
> If the result is 0, then arming otherwise get an error message flash.
>
> How can I do the same in cakephp?

It's a bit tricky in that you've got the variable on the left and the
table column on the right. For Cake, you'll need to reverse that, so
the operators will have to change (as I've done below).

Your query also doesn't make sense because you are fetching a record
with a specific id. You should either fetch by id OR by the rest of
the criteria here. But, if we leave out id it would be something like:

$data = $this->find(     // assuming query is from inside Calendar model
        'all',
        array(
                'conditions' => array(
                        'Calendar.confirm' => 1,
                        array(
                                'OR' => array(
                                        array(
                                                'Calendar.from >=' => $from,
                                                'Calendar.until <=' => $until
                                        ),
                                        array(
                                                'Calendar.from >=' => $from,
                                                'Calendar.until >=' => $until,
                                                'Calendar.from <=' => $until
                                        ),
                                        array(
                                                'Calendar.from <=' => $from,
                                                'Calendar.until >=' => $until
                                        ),
                                        array(
                                                'Calendar.from <=' => $from,
                                                'Calendar.until >=' => $from,
                                                'Calendar.until <=' => $until
                                        )
                                )
                        )
                )
        )
);


So, the record must have confirm == 1 AND at least one of the
subsequent groups must evaluate true. Each of the groups, because
they're in their own array, will use AND, so all tests within a
particular group must be true..

This could probably be tightened up a bit by using BETWEEN in places,
and, if you took care (in record creation) not to allow Calendar.from
to ever be greater than Calendar.until, you wouldn't need to test for
it in find(), which would simplify this more.

It might help to draw this out on paper. Draw a line and place an F
and a U somewhere along it. This will be C.from and C.until (for any
record. Below that, in 4 steps below each other, place a $f and $u to
represent your vars, either to the left or right of F and U.

So, given the above, you have 2 options where $from is less than
C.from: one where $until is greater than C.until, and one where it's
less.

Next, you have 2 options where $until is greater than C.until: one
where $from is less than C.from and one where it's greater.

The only things you don't want are $from > C.until and $until <
C.from, which makes sense. So the query can then be simplified to:

$data = $this->find(
        'all',
        array(
                'conditions' => array(
                        'Calendar.confirm' => 1,
                        array(
                                'Calendar.from <' => $until,
                                'Calendar.until >' => $from
                        )
                )
        )
);

> Sorry for bad English.

It's far better than my Italian. :-)

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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