Sunday, February 20, 2011

Symfony Doctrine Native SQL

in my earlier post, Symfony Doctrine Execute Raw SQL i execute Raw or Native SQL quries, however method describe previously can only perform SELECT operations, in this post we will execute all type of SQL quires i.e. SELECT, INSERT, UPDATE, DELETE
public function myDBWrapper($query,$type=0){
      $connection=Doctrine_Manager::getInstance()
                  ->getCurrentConnection()->getDbh();
      // Get Connection of Database

      $statement=$connection->prepare($query);
      // Make Statement

      $statement->execute();
      // Execute Query

      if($type==0){
          // Check if it is SELECT query
          
          $results = $statement->fetchAll();
          return $results;
      }

  }

Usage

public function executeMyDBWrapperTest(sfWebRequest $request){

       $insertQuery="INSERT INTO users (username,email)
                       VALUES ('Originative','originative@live.com')";
       $selectQuery="Select * from users";

       $this->myDBWrapper($insertQuery);
       // INSERT data in table
       
       $this->results=$this->myDBWrapper($selectQuery, 1);
       // Fetch data from table save output 
       // which will be displayed in
       // MyDBWrapperTestSuccess.php

   }

0 comments:

Post a Comment

 

Blog Info

A Pakistani Website by Originative Systems

Total Pageviews

Tutorial Jinni Copyright © 2015 WoodMag is Modified by Originative Systems