Musings of ErisDS
beta
ErisDS

Archive > Tag > snippet

I’m still pretty new to Magento, and am learning more about it every day. I’ve been trying to learn all I can about internals such as managing collections & recently stumbled on trying to filter a collection by two attributes. I wanted to build a list of all products that had one OR another attribute set to certain values.

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.

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

Perusing my Google Analytics data shows up some interesting results. For starters the top search query which sends people to my blog is “symfony accessing object in a form”. This has been consistent for several months, but there is no article on my blog which answers that question. I imagine this must be very frustrating for people so it’s about time I added the answer here as a snippet.

Recently I worked on a Wordpress site where I wanted to be able to customise the dashboard for different user roles. Wordpress by default has administrators, editors, authors, contributors and subscribers. It’s not too much to assume you might not want to show your editors or authors all the details of your blog posts etc and this short snippet will allow you to do just that.

When working with large and complex datasets in Symfony, there comes a point where the ORM layer (Propel or Doctrine) causes more problems than it solves. Sometimes it’s necessary to debug large SQL queries built using the ORM layer, and at other times it’s appropriate to bypass the ORM layer entirely.  The following snippets come from projects using Propel, I realise a lot of folks will have moved on to Doctrine but I hope there may be one or two useful things for that here too.

These functions and hooks let you customise the output of the_excerpt() template tag. Just copy and paste into the functions.php file in your theme. This works for both posts and pages.