10
\$\begingroup\$

According to the Doctrine docs, you should use Array hydration rather than record hydration when retrieving data for read-only purposes.

Unfortunately, this means I have to use array syntax (as opposed to object syntax) to reference the data.

$q = Doctrine_Query::create()
    ->from('Post p')
    ->leftJoin('p.PostComment pc')
    ->leftJoin('pc.User u')
    ->where('p.post_id = ?', $id);

$p = $q->fetchOne(array(), Doctrine_Core::HYDRATE_ARRAY);

...

foreach ($p['PostComment'] as $comment) {
    $this->Controls->Add(new CommentPanel($comment['text'], 
                         $comment['User']['nickname'], 
                         $comment['last_updated_ts']));
}

Maybe it's just me, but all of those string literals as array indexes are kinda scary. Does anyone have some ideas for cleaning this up?

\$\endgroup\$
  • \$\begingroup\$ from what i understand if you would fetch an object you would to $comment->text; or would you do $comment->getText() ? (Not to familiar with the "old/current" doctrine ;) ) \$\endgroup\$ – edorian Jan 26 '11 at 10:03
  • \$\begingroup\$ @edorian: It would be $comment->text; \$\endgroup\$ – BenV Jan 26 '11 at 14:16
  • \$\begingroup\$ I believe you can use all three. \$\endgroup\$ – lotsoffreetime Jan 26 '11 at 22:38
  • \$\begingroup\$ I was trying to write a longer answer but it really bowls down to "cast to stdClass (and thats already said now) if you don't like it" but maybe i don't get your reasoning behind that looking "scary". The difference between -> and [''] shoudn't matter so much ? \$\endgroup\$ – edorian Jan 27 '11 at 8:40
5
\$\begingroup\$

Scary? In what way? I don't really get that.

It's just syntax. If you really care, just cast the arrays as stdClass objects

foreach ( $p['PostComment'] as $comment )
{
  $comment = (object) $comment;
  $this->Controls->Add( new CommentPanel(
      $comment->text
    , $comment->User->nickname
    , $comment->last_updated_ts
  ));
}
\$\endgroup\$
  • \$\begingroup\$ He'd need to cast User to an object too, else it would be $comment->User["nickname"]; But yeah, casting it was also in my mind \$\endgroup\$ – edorian Jan 27 '11 at 8:38
  • \$\begingroup\$ I guess I'm just used to other languages where a typo in a literal would not be caught until run time but a typo in a property name would be caught at compile time. But with a scripting language like PHP both will not be noticed until run time. \$\endgroup\$ – BenV Jan 27 '11 at 14:47
  • 2
    \$\begingroup\$ If that's the concern, you could use define()s or constants to reference your fields, rather than string literals. But that's probably only making things worse. Object hydration may take a little more time and memory but unless you have problems with either, I'd still use it for the very reason you outline here. \$\endgroup\$ – lotsoffreetime Jan 27 '11 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.