I think you're missing the point on the find() method. Doing a find
('all', $conditions) would do exactly the same thing that you are
doing with your query approach, and allow you to take advantage of the
Cake magic.

In your blog, you create a findByDate function that builds a SQL
statement. First of all, I would point out that concatenating a string
into SQL always makes me shudder since it exposes you to SQL
injection.

But ignoring that for a moment, you can simply rewrite that function
as:

 function findByDate($date) {

   // TODO: validate $date

   $periodsExist  = '(EXISTS select '1' from menu_periods MenuPeriod
where MenuPeriod.period_start <= ? AND MenuPeriod.period_end > ? AND
MenuPeriod.menu_id=Menu.menu_id)';
   $postingsExist = '(EXISTS select '2' from menu_postings where
MenuPosting.post_start <= NOW() AND MenuPosting.post_end > NOW() and
MenuPosting.menu_id = Menu.menu_id)';
   $conditions = array('AND' => array( $periodsExist => array($date,
$date),
                                       $postingsExist)
                      );


   $menus = $this->Menu->find('all', array('conditions' =>
$conditions));
   return $menus;
  }

And somebody smarter than me could probably make this even simpler,
but this seems cleaner and safer to me than using query.

On Dec 22, 7:15 pm, erturne <[email protected]> wrote:
> So there's the hard part... understanding how find() works! I think
> I'm pretty close to understanding it, but for what I was doing I
> simply ran out of time and could not justify struggling with find()
> anymore. I read so many articles on Google, read and re-read the
> sections of the cookbook, etc. Maybe I'm dense but I spent hours
> trying to get find() to return the data I wanted. Check out my blog
> post 
> athttp://erturne-cakephp.blogspot.com/2008/12/retrieving-model-data-fin...
> for a description of the problem and a few of the things I tried.
>
> On Dec 19, 6:56 pm, teknoid <[email protected]> wrote:
>
> > query() is not a recommended practice...
> > besides a few points you've mentioned (and don't think that
> > portability is not important, you'd be surprised what happens when
> > oracle invests $1M into your company, but now you gotta switch
> > DB's ;)), i've outlined a few more here:
>
> >http://teknoid.wordpress.com/2008/05/09/cakephp-and-custom-sql/
>
> > besides, once you understand how find() works, it's becomes a lot
> > easier to manage/maintain than writing custom SQL
>
> > On Dec 19, 6:26 pm,erturne<[email protected]> wrote:
>
> > > I have to ask... if you have to take extra measures to specify your
> > > join conditions, why not just provide a method in a model that uses
> > > query() with a simple-to-understand SQL statement? All these find()
> > > contortions, while interesting, may not be the easiest to maintain
> > > later on when you have to decipher what you were trying to do. I
> > > suppose you have to worry about SQL statement portability, but really
> > > how often do you have to move your database from one server type to
> > > another? And if you keep all your queries in your models at least it's
> > > easy to find what needs to change should you move to a different
> > > database.
>
> > > On Dec 19, 12:51 pm, Webweave <[email protected]> wrote:
>
> > > > OK, it depends on what you are really trying to fetch. Are you trying
> > > > to restrict the Advert results to just those that have data in the
> > > > hasMany tables, or are you trying to restrict the child table values ?
>
> > > > If it's the former, I would just use a condition that includes a
> > > > select for that condition specifically. For instance, in my
> > > > VolunteerCake application, I have a structure where I have a Job that
> > > > has signup Slots associated with it. I want to list jobs with
> > > > available slots (meaning there are not more Slot rows than a max field
> > > > in the jobs table), so I use a condition like:
>
> > > >         $whereArray =  array(
> > > >                             'AND' => array(
> > > >                                 'Slot.max_signups > (select count(1)
> > > > from user_slots s2 where s2.slot_id = Slot.slot_id)',
> > > >                                 'Slot.job_id = '.$id
> > > >             )
> > > >         );
>
> > > > Note that the "select count" is enclosed in parens, and compared to
> > > > the Slot.max_signups. This correlated sub-query causes the database to
> > > > do an implicit join, without Cake having to understand the
> > > > relationship.
>
> > > > If it's the latter, you can add your condition to the belongsTo or do
> > > > a separate find for the related data.
>
> > > > On Dec 19, 1:53 am, dev <[email protected]> wrote:
>
> > > > > i posted code here:http://bin.cakephp.org/saved/40741
>
> > > > > How to join Image and Option tables if i want to set these find
> > > > > conditions in Advert?
>
> > > > > I tried Containable (test2 function), but it selects all Adverts, and
> > > > > if i set conditions in first paginate array - it generates the same
> > > > > error, bicause tables don't join.
>
> > > > > How to solve this problem?
>
>
--~--~---------~--~----~------------~-------~--~----~
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