views:

198

answers:

2

Hi,

EDIT: The reason why my Doctrine queries (see below) did not work/produced those errors was because I left out

ci_doctrine.

when creating this database:

CREATE TABLE `ci_doctrine`.`user` (
    `id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `username` VARCHAR( 255 ) NOT NULL ,
    `password` VARCHAR( 255 ) NOT NULL ,
    `first_name` VARCHAR( 255 ) NOT NULL ,
    `last_name` VARCHAR( 255 ) NOT NULL ,
    UNIQUE (`username`
)
)

END OF EDIT

How do I write plane SQL queries using Doctrine connection object and display the results? For example, how do I perform:

SELECT * FROM table_name WHERE column_name LIKE '%anything_similar_to_this%';

using Doctrine something like this (this example does not work)

 $search_key = 'search_for_this';

 $conn = Doctrine_Manager::connection();

 $conn->execute('SELECT * FROM table_name WHERE column_name LIKE ?)',  $search_key);

 echo $conn;  

I've also tried:

$search_key = 2;
$q = new Doctrine_RawSql();
$result = $q->select('column_name')
  ->from('table_name')
  ->where('id = ?', $search_key)
  ->execute();

echo $result;
+1  A: 

Doctrine uses It's own query language called DQL. And it uses a query class to handle the queries called Doctrine_Query. Through this class you can build a query using the so called query builder.

You can look at the documentation at http://doctrine-project.com to find out how it works exactly.

Ikke
+1  A: 

You have an unnecessary parenthesis which is probably causing an error. Try changing:

SELECT * FROM table_name WHERE column_name LIKE ?)

to:

SELECT * FROM table_name WHERE column_name LIKE ?

If it still doesn't work, try posting the error message.

Mark Byers
+1. I changed: $conn->execute('SELECT * FROM table_name WHERE column_name LIKE ?)', $search_key); to $conn->execute('SELECT * FROM table_name WHERE column_name LIKE ?', $search_key); and got the following error: Severity: 4096 Message: Argument 2 passed to Doctrine_Connection::execute() must be an array, string given, call in... Severity: Warning Message: Invalid argument supplied for foreach()... Fatal error: Uncaught exception 'Doctrine_Connection_Mysql_Exception' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound in...
01010011