Snippet: Symfony & Propel–Queries with SQL functions

This is a super short snippet that I ALWAYS forget how to do! Often I want to build more complex where clauses with Propel Criteria which use SQL functions such as UCASE, LCASE, LEN and the date functions DAY, MONTH and YEAR. This is possible using Propel & Criteria, but how to do it is not immediately obvious.

The snippet below shows how to select objects from the database which were created in a specific month and year. Using Criteria::CUSTOM, it’s possible to specify a column and a comparison to do with that column. This is quite useful for doing things like building archive lists.

$c->add(MyObjPeer::CREATED_AT, 'MONTH('.MyObjPeer::CREATED_AT.')='. $month, Criteria::CUSTOM);
$c->addAnd(MyObjPeer::CREATED_AT, 'YEAR('.MyObjPeer::CREATED_AT.')='. $year, Criteria::CUSTOM);

A Note on Snippets: When using frameworks such as Symfony it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know.

About deepak

Name:Deepak Kumar Country:India Education:B.Tech(Mechanical Engg.), MBA(Information Technology) Expertise:ISO, CMMI, ITIL, SQA, SQC, Web Development, Business & System Analysis, Business Process Analysis & Design, E-Business(B2B,B2C), Information Management & Strategy. Technologies:PHP, CSS, JAVA,AJAX, MySQL, Oracle etc. Interested In:Quality Assurance, Freelance Web Development, System Analysis & Friends
This entry was posted in Symfony and tagged , , , . Bookmark the permalink.
  • Anonymous

    Nice Post.

blog comments powered by Disqus