public function testPreAndPostDelete() { $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book'); $books = $c->find(); $count = count($books); $book = $books->shift(); $this->con->lastAffectedRows = 0; $c = new ModelCriteriaWithPreAndPostDeleteHook('bookstore', '\\Propel\\Tests\\Bookstore\\Book', 'b'); $c->where('b.Id = ?', $book->getId()); $nbBooks = $c->delete($this->con); $this->assertEquals(12, $this->con->lastAffectedRows, 'postDelete() is called after delete() even if preDelete() returns not null'); $this->con->lastAffectedRows = 0; $c = new ModelCriteriaWithPreAndPostDeleteHook('bookstore', '\\Propel\\Tests\\Bookstore\\Book'); $nbBooks = $c->deleteAll($this->con); $this->assertEquals(12, $this->con->lastAffectedRows, 'postDelete() is called after deleteAll() even if preDelete() returns not null'); }
public static function from($queryClassAndAlias) { list($class, $alias) = ModelCriteria::getClassAndAlias($queryClassAndAlias); $queryClass = $class . 'Query'; if (!class_exists($queryClass)) { throw new PropelException('Cannot find a query class for ' . $class); } $query = new $queryClass(); if ($alias !== null) { $query->setModelAlias($alias); } return $query; }
/** * Define the hydration schema based on a query object. * Fills the Formatter's properties using a Criteria as source * * @param ModelCriteria $criteria * * @return PropelFormatter The current formatter object */ public function init(ModelCriteria $criteria) { $this->dbName = $criteria->getDbName(); $this->setClass($criteria->getModelName()); $this->setWith($criteria->getWith()); $this->asColumns = $criteria->getAsColumns(); $this->hasLimit = $criteria->getLimit() != 0; return $this; }
public function replaceNames(&$clause) { return parent::replaceNames($clause); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->setFormatter(ModelCriteria::FORMAT_ARRAY); $c->filterByTitle('The Tin Drum'); $c->join('Propel\\Tests\\Bookstore\\Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME); $book = $c->findOne($con); $this->assertEquals(array('Id', 'Title', 'ISBN', 'Price', 'PublisherId', 'AuthorId', 'Author', 'AuthorName', 'AuthorName2'), array_keys($book), 'withColumn() do not change the resulting model class'); $this->assertEquals('The Tin Drum', $book['Title']); $this->assertEquals('Gunter', $book['Author']['FirstName'], 'ArrayFormatter correctly hydrates withclass and columns'); $this->assertEquals('Gunter', $book['AuthorName'], 'ArrayFormatter adds withColumns as columns'); $this->assertEquals('Grass', $book['AuthorName2'], 'ArrayFormatter correctly hydrates all as columns'); }
public function testGetSelectReturnsArrayWhenSelectingAllColumns() { $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book'); $c->select('*'); $this->assertEquals(array('Book.Id', 'Book.Title', 'Book.ISBN', 'Book.Price', 'Book.PublisherId', 'Book.AuthorId'), $c->getSelect()); }
public function testPruneCompositeKey() { BookstoreDataPopulator::depopulate(); BookstoreDataPopulator::populate(); // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $books = $c->find(); foreach ($books as $book) { $book->save(); } BookPeer::clearInstancePool(); $nbBookListRel = BookListRelQuery::create()->prune()->count(); $this->assertEquals(2, $nbBookListRel, 'prune() does nothing when passed a null object'); $testBookListRel = BookListRelQuery::create()->findOne(); $nbBookListRel = BookListRelQuery::create()->prune($testBookListRel)->count(); $this->assertEquals(1, $nbBookListRel, 'prune() removes an object from the result'); }
public function __construct($dbName = 'bookstore', $modelName = 'Propel\\Tests\\Bookstore\\Author', $modelAlias = null) { parent::__construct($dbName, $modelName, $modelAlias); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->filterByTitle('The Tin Drum'); $c->join('Propel\\Tests\\Bookstore\\Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME); $book = $c->findOne($con); $this->assertTrue($book instanceof Book, 'withColumn() do not change the resulting model class'); $this->assertEquals('The Tin Drum', $book->getTitle()); $this->assertTrue($book->getAuthor() instanceof Author, 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getAuthor()->getFirstName(), 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getVirtualColumn('AuthorName'), 'ObjectFormatter adds withColumns as virtual columns'); $this->assertEquals('Grass', $book->getVirtualColumn('AuthorName2'), 'ObjectFormatter correctly hydrates all virtual columns'); }
public function testMagicFindByObject() { $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author'); $testAuthor = $c->findOne(); $q = BookQuery::create()->findByAuthor($testAuthor); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId(); $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXX($value) is turned into findBy(XXX, $value)'); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author'); $testAuthor = $c->findOne(); $q = BookQuery::create()->findByAuthorAndISBN($testAuthor, 1234); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId() . " AND book.ISBN=1234"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXXAndYYY($value) is turned into findBy(array(XXX, YYY), $value)'); }
public function __construct($dbName = 'bookstore', $modelName = 'Author', $modelAlias = null) { parent::__construct($dbName, $modelName, $modelAlias); }