This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use the new forum.
Home » legacy support » symfony 1.1 » custom sql query
custom sql query [message #66371] Sun, 30 November 2008 05:08 Go to next message
yshaf13  is currently offline yshaf13
Messages: 22
Registered: November 2008
Junior Member
Hi, what would be the propel equivalent of this query?
(sf1.1)
select e.* from `event` e, `lecture` l where date(l.lecture_date) = date(e.event_date); 
Re: custom sql query [message #66398 is a reply to message #66371 ] Sun, 30 November 2008 21:40 Go to previous messageGo to next message
masterix  is currently offline masterix
Messages: 41
Registered: September 2008
Member
$cr = new Criteria();
   $cr->addJoin(EventPeer::EVENT_DATE, LecturePeer::LECTURE_DATE);
   $cr->clearSelectColumns()->addSelectColumn(implode(',', array_values(EventPeer::getFieldNames(BasePeer::TYPE_COLNAME))));


Try this. I just wrote it down so it isn't checked.
Re: custom sql query [message #66400 is a reply to message #66398 ] Sun, 30 November 2008 21:46 Go to previous messageGo to next message
yshaf13  is currently offline yshaf13
Messages: 22
Registered: November 2008
Junior Member
The reason I didn't simply join the tables is because I need to use the mysql DATE function - the timestamps don't have to match exactly, just the day..
Re: custom sql query [message #66401 is a reply to message #66400 ] Sun, 30 November 2008 21:50 Go to previous messageGo to next message
masterix  is currently offline masterix
Messages: 41
Registered: September 2008
Member
So you can use aliases. As an alias you define MySQL function with its argument. For details just see propel documentation of criteria class.

Of course it is not recommended to use database-dependant function. The reason is obvious.

Cheers,
masterix
Re: custom sql query [message #66489 is a reply to message #66401 ] Tue, 02 December 2008 01:05 Go to previous messageGo to next message
yshaf13  is currently offline yshaf13
Messages: 22
Registered: November 2008
Junior Member
hmm having trouble finding documentation... care to write an example?
Re: custom sql query [message #66506 is a reply to message #66489 ] Tue, 02 December 2008 10:20 Go to previous messageGo to next message
masterix  is currently offline masterix
Messages: 41
Registered: September 2008
Member
Try here:
http://propel.phpdb.org/docs/user_guide/

Cheers,
masterix
Re: custom sql query [message #66652 is a reply to message #66398 ] Wed, 03 December 2008 16:14 Go to previous messageGo to next message
GaryFx
Messages: 377
Registered: May 2008
Location: Masschusetts
Faithful Member
masterix wrote on Sun, 30 November 2008 15:40

$cr = new Criteria();
   $cr->addJoin(EventPeer::EVENT_DATE, LecturePeer::LECTURE_DATE);
   $cr->clearSelectColumns()->addSelectColumn(implode(',', array_values(EventPeer::getFieldNames(BasePeer::TYPE_COLNAME))));


There's no need to do the select columns.

If you don't select any columns, you will get all of the columns from the class used for the doSelect. So assuming this is evaluated with EventPeer::doSelect, you'll get the desired result.

To add the requested WHERE clause, use
$cr->add('DATE('.EventPeer::EVENT_DATE.')',
         'DATE('.LecturePeer::LECTURE_DATE.')');


Aside: The use of $c for criteria is such a common convention in symfony that I'd really discourage alternatives such as $cr. A longer meaningful name can make sense (e.g. $lectureEventDatesCriteria), but $cr adds so little to $c that it may make a future maintainer hunting for a non-existent distinction.
Re: custom sql query [message #66704 is a reply to message #66652 ] Wed, 03 December 2008 23:31 Go to previous message
yshaf13  is currently offline yshaf13
Messages: 22
Registered: November 2008
Junior Member
Thanks for you're help.
Quote:

$cr->add('DATE('.EventPeer::EVENT_DATE.')',
'DATE('.LecturePeer::LECTURE_DATE.')');


when i do that (and a doSelect) i get:
[wrapped: Cannot fetch TableMap for undefined table: DATE(event]

However
$cr->add(EventPeer::EVENT_DATE, 'date('.EventPeer::EVENT_DATE.')=  date('.LecturePeer::LECTURE_DATE.')', Criteria::CUSTOM);

will work, problem is, I have to somehow select the Lecture table since I'm doing EventPeer::doSelect() (or else it will say undefined column Lecture_date), and if I do addSelectColumn(LectureDate::ID) it won't select anything else...
Previous Topic:Change root folder Symfony
Next Topic:How to write forms which envolves more than one models
Goto Forum:
  

powered by FUDforum - copyright ©2001-2004 FUD Forum Bulletin Board Software