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 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 testSelectArrayWithColumn() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book'); $c->join('Book.Author'); $c->withColumn('LOWER(Book.Title)', 'LowercaseTitle'); $c->select(array('LowercaseTitle', 'Book.Title')); $c->orderBy('Book.Title'); $rows = $c->find($this->con); $expectedSQL = 'SELECT LOWER(book.TITLE) AS LowercaseTitle, book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC'; $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()'); $expectedRows = array(array('LowercaseTitle' => 'don juan', 'Book.Title' => 'Don Juan'), array('LowercaseTitle' => 'harry potter and the order of the phoenix', 'Book.Title' => 'Harry Potter and the Order of the Phoenix'), array('LowercaseTitle' => 'quicksilver', 'Book.Title' => 'Quicksilver'), array('LowercaseTitle' => 'the tin drum', 'Book.Title' => 'The Tin Drum')); $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()'); }
public function testWithColumnAndSelectColumns() { $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->withColumn('UPPER(Propel\\Tests\\Bookstore\\Book.Title)', 'foo'); $sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, UPPER(book.TITLE) AS foo FROM `book`'; $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() adds the object columns if the criteria has no select columns'); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->addSelectColumn('book.ID'); $c->withColumn('UPPER(Propel\\Tests\\Bookstore\\Book.Title)', 'foo'); $sql = 'SELECT book.ID, UPPER(book.TITLE) AS foo FROM `book`'; $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() does not add the object columns if the criteria already has select columns'); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->addSelectColumn('book.ID'); $c->withColumn('UPPER(Propel\\Tests\\Bookstore\\Book.Title)', 'foo'); $c->addSelectColumn('book.TITLE'); $sql = 'SELECT book.ID, book.TITLE, UPPER(book.TITLE) AS foo FROM `book`'; $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() does adds as column after the select columns even though the withColumn() method was called first'); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->addSelectColumn('book.ID'); $c->withColumn('UPPER(Propel\\Tests\\Bookstore\\Book.Title)', 'foo'); $c->withColumn('UPPER(Propel\\Tests\\Bookstore\\Book.ISBN)', 'isbn'); $sql = 'SELECT book.ID, UPPER(book.TITLE) AS foo, UPPER(book.ISBN) AS isbn FROM `book`'; $params = array(); $this->assertCriteriaTranslation($c, $sql, $params, 'withColumn() called repeatedly adds several as colums'); }