YASB – Yet Another Symfony Blog

July 23, 2007

More gotchas from the Symfony world

Filed under: General, PHP, Tips and tricks — Krof Drakula @ 5:00 pm

Well, it seems there’s a few other things that aren’t as they seem in Symfony – if they’re expected or not, they are things I’ve noticed and needed a few minutes (or in a another case) or just over two hours to figure out.

All of them have to do with Symfony’s output escaper using PHPView. The first one is concerned with custom ResultSet objects that you pass into the template using a simple $this->anything assignation within an action or a component. Say you’re got a simple SQL query within a component or action that you want returned as a ResultSet (using Creole):

// lang php
$SQL = "
  SELECT *
  FROM app_user
  WHERE insertion_time > DATE_DIFF(CURDATE, INTERVAL ? DAY)
";
$stmt = Propel::getConnection()->prepareStatement($SQL);
$this->results = $stmt->executeQuery(
  array(7), ResultSet::FETCH_ASSOC
);

You’d expect to have $results contain a ResultSet object that you can iterate over using a while($results->next()) {}, but you’re in for a surprise – it doesn’t work.

If you look closely at the variable within the template printed out using print_r(), you can see that the object is wrapped within an sfOutputEscaper (or variant of) object that intercepts certain functions. This is what seems to happen when using a ResultSet object – the wrapper escaper object overloads the methods and prevents ResultSet’s methods from being invoked.

The solution? Place $results = $results->getRawValue() somewhere before the while loop and everything works fine.

I’ll probably enter a ticket for this one, after I grab something to eat. ;)

3 Comments

  1. Hi. Could I translate some of your post and put them on my blog? Of course I`ll include link to source.

    Comment by Teamon — September 6, 2007 @ 6:51 pm

  2. Sure, as long as you give credit where credit is due. :)

    Note to self: might as well start thinking of putting up a license banner, something in the lines of CC.

    Comment by Krof Drakula — September 6, 2007 @ 8:34 pm

  3. Thank articles good.

    Comment by Bitkisel eczane bitkilerle tedavi — June 12, 2009 @ 3:42 pm

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress