Skip to content

Instantly share code, notes, and snippets.

@n1k0
Created September 21, 2010 08:00
Show Gist options
  • Save n1k0/589366 to your computer and use it in GitHub Desktop.
Save n1k0/589366 to your computer and use it in GitHub Desktop.
<?php
// example of a locally computed virtual property
class Book extends Doctrine_Record
{
public function getSynopsys($length = 200)
{
return substr($this->abstract.$this->cover, 0, $length);
}
public function setSynopsys($value)
{
throw new LogicException('cannot set a computed field');
}
}
$book = BookTable::getInstance()->findOneByTitle('Les Misérables');
echo $book->getSynopsys();
// example of a SQL computed virtual property
class BookTable extends Doctrine_Table
{
public function getWithAuthorsCount()
{
return $this->createQuery('b')
->addSelect('COUNT(a.id) as nb_authors')
->leftJoin('b.Authors a')
->groupBy('b.id, a.id')
->execute()
;
}
}
for (BookTable::getInstance()->getWithAuthorsCount() as $book)
{
echo sprintf('%s (%d author(s))', $book->title, $book->nb_authors);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment