Monday, January 10, 2011

Symfony Doctrine Execute Raw SQL

Converting to object oriented can be sometimes difficult as people are not used to it... same is the case with ORM, when you use it there may be some problems for the new comers.

Doctrine an ORM tool, can execute Raw SQL queries beside fully object and partial queries, executing raw sql queries is very simple following PHP code demonstrate it, here we use Doctrine with symfony.
public function executeShowUserInfo(sfWebRequest $request){

     $query = Doctrine_Query::create()
      ->query("select * from user");

     $result = $query->toArray();

     $count = count($result);

     for($i=0;$i<$count;$i++){

         echo $result [$i]["first_name"]
         ." => "
         .$result [$i]["email"]
         .",";

     }
}
here $query->toArray() convert returned response in to array.
Note : it is not recommended to echo from a controller, i do just for demonstration purpose.

1 comment:

  1. Thanks a lot. Was trying using the symfony example provided in the manual but it seems to be outdated stuff.

    Also, maybe it's me but the Doctrine manual is very hard to understand and follow.

    ReplyDelete