Category Archive:

How to select distinct with Doctrine_Query

0

There is a bug with doctrine and many tickets have been opened for it from what I saw. Doing the following in a query will return no result

$q = Doctrine_Query::create()
	->select('distinct(datetime_scraped)')
	->from('Rate')
	->orderBy('datetime_scraped desc');

The solution is to alias the distinct column like below

$q = Doctrine_Query::create()
	->select('distinct(datetime_scraped) as datetime_scraped')
	->from('Rate')
	->orderBy('datetime_scraped desc');
Posted in: Doctrine ORM

Continue Reading

How To Truncate A Table With Doctrine

1

Doctrine ORM does not provide a built in method to truncate a table, so we must run a sql query manually. Use the below code and alter the SQL as needed:

$doctrine = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$doctrine->query('TRUNCATE TABLE tableName');
unset($doctrine);
Posted in: Doctrine ORM
Tags:

Continue Reading

Doctrine ORM: Fatal error: Call to a member function getOid() on a non-object

3

Came across this today and was unable to find a solution on Google, so hopefully this will help somebody out.  I had the following code using Doctrine ORM:

 
//load profile object
$clientProfile = new Profile();
$profileObject = $clientProfile->getProfileByPid($pid);
$profileObject->lead_type_reason = $lead_type_reason;
$profileObject->save();

The getProfileByPid() method is as follows:

 
public function getProfileByPid($pid) {
$q = Doctrine_Query::create()
->from('Profile')
->where('id = ?', $pid);
 
try {
return $q->execute();
} catch (Doctrine_Connection_Exception $e) {
 
}
}

The problem is that the query returns multiple records and places them into a collection, rather than returning a single object as you might have expected.  Change the “return $q->execute()” to “return $q->fetchOne()” to solve the problem.  Better yet though, figure out why the query returns more than one record if you’re passing in a unique ID like I was.  Hope this helps!


Continue Reading