Created
September 21, 2010 08:00
-
-
Save n1k0/589366 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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