Custom SQL execution in symfony

I am trying to execute some custom SQL to retrieve some model objects in a Symfony application. I found a tutorial on the Internet that says something like this will allow me to fulfill the request, although not populate the models (populating the model is not a serious problem, it's just for read-only data).

$pdo = Doctrine_Manager::getInstance()->connection()->getDbh();
$pdo->prepare("SELECT * from something complicated");
$pdo->execute();
$this->sensorReadings = $pdo->fetchAll();

But I get an error message:

Fatal error: Call to undefined method PDO::execute()
in sfproject/apps/frontend/modules/site/actions/actions.class.php 
+5
source share
3 answers
$query = "SELECT * from something complicated";
$rs = Doctrine_Manager::getInstance()->getCurrentConnection()->fetchAssoc($query);

A result set is an array.

+9
source
// get Doctrine_Connection object
$con = Doctrine_Manager::getInstance()->connection();
// execute SQL query, receive Doctrine_Connection_Statement
$st = $con->execute("SELECT User.* FROM ....");
// fetch query result
$result = $st->fetchAll();

// convert array to objects
foreach ($result as $userArray) {
    $user = new User();
    $user->fromArray($userArray);
    ...
}

This code is not very short, but allows you to execute a custom request through an existing Doctrine connection and get your object at the end.

+2

( ):

$databaseManager = new sfDatabaseManager ( $this->configuration );
Doctrine_Manager::connection ()->setAttribute ( Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true );

$dbOptions = Doctrine_Manager::connection ()->getManager ()->getConnection ( 'doctrine' )->getOptions ();
$dbDsn = $dbOptions['dsn'];
$dbDsnArr = explode ( ';', $dbDsn );
$dbHost = str_replace ( 'mysql:host=', '', $dbDsnArr[0] );
$dbName = str_replace ( 'dbname=', '', $dbDsnArr[1] );
$dbUn = $dbOptions['username'];
$dbPw = $dbOptions['password'];

, DSN : dsn: 'mysql: host = somedomain; dbname = dbname'

This code allows you to get information about connecting to the database of any connection, so it will work for several connections.

Hope this helps ...

0
source

All Articles