Example #1
0
 public function testRelationOneToOne()
 {
     $q = BookstoreEmployeeQuery::create()->joinBookstoreEmployeeAccount();
     $joins = $q->getJoins();
     $join = $joins['BookstoreEmployeeAccount'];
     $with = new ModelWith($join);
     $this->assertEquals($with->getRelationMethod(), 'setBookstoreEmployeeAccount', 'A ModelWith computes the relation method from the join');
     $this->assertEquals($with->getRelationName(), 'BookstoreEmployeeAccount', 'A ModelWith computes the relation name from the join');
     $this->assertFalse($with->isAdd(), 'A ModelWith computes the relation cardinality from the join');
 }
 public function testJoinAliasQuery()
 {
     $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->join('b.Author a');
     $c->where('a.FirstName = ?', 'Leo');
     $books = BookQuery::create(null, $c)->find($con);
     $expectedSQL = $this->getSql("SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book INNER JOIN author a ON (book.author_id=a.id) WHERE a.first_name = 'Leo'");
     $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in where()');
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\BookstoreEmployee', 'be');
     $c->join('be.Supervisor sup');
     $c->join('sup.Subordinate sub');
     $c->where('sub.Name = ?', 'Foo');
     $employees = BookstoreEmployeeQuery::create(null, $c)->find($con);
     $expectedSQL = $this->getSql("SELECT bookstore_employee.id, bookstore_employee.class_key, bookstore_employee.name, bookstore_employee.job_title, bookstore_employee.supervisor_id FROM bookstore_employee INNER JOIN bookstore_employee sup ON (bookstore_employee.supervisor_id=sup.id) INNER JOIN bookstore_employee sub ON (sup.id=sub.supervisor_id) WHERE sub.name = 'Foo'");
     $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in further joins()');
 }
 public function testFindPkSimpleWithSingleTableInheritanceReturnCorrectClass()
 {
     Propel::disableInstancePooling();
     $employee = new BookstoreEmployee();
     $employee->save($this->con);
     $manager = new BookstoreManager();
     $manager->save($this->con);
     $cashier1 = new BookstoreCashier();
     $cashier1->save($this->con);
     $cashier2 = new BookstoreCashier();
     $cashier2->save($this->con);
     $this->assertInstanceOf('\\Propel\\Tests\\Bookstore\\BookstoreEmployee', BookstoreEmployeeQuery::create()->findPk($employee->getId()), 'findPk() return right object : BookstoreEmployee');
     $this->assertInstanceOf('\\Propel\\Tests\\Bookstore\\BookstoreManager', BookstoreEmployeeQuery::create()->findPk($manager->getId()), 'findPk() return right object : BookstoreManager');
     $this->assertInstanceOf('\\Propel\\Tests\\Bookstore\\BookstoreCashier', BookstoreEmployeeQuery::create()->findPk($cashier1->getId()), 'findPk() return right object : BookstoreCashier');
     $this->assertInstanceOf('\\Propel\\Tests\\Bookstore\\BookstoreCashier', BookstoreEmployeeQuery::create()->findPk($cashier2->getId()), 'findPk() return right object : BookstoreCashier');
     Propel::enableInstancePooling();
 }
Example #4
0
 /**
  * Testing creating & saving new object & instance pool.
  */
 public function testObjectInstances_New()
 {
     $emp = new BookstoreEmployee();
     $emp->setName(md5(microtime()));
     $emp->save();
     $id = $emp->getId();
     $retrieved = BookstoreEmployeeQuery::create()->findPk($id);
     $this->assertSame($emp, $retrieved, "Expected same object (from instance pool)");
 }
 /**
  * Test copyInto method.
  */
 public function testCopyInto_Deep()
 {
     BookstoreDataPopulator::populate();
     // Test a "normal" object
     $book = BookQuery::create()->filterByTitle('Harry%', Criteria::LIKE)->findOne();
     $reviews = $book->getReviews();
     $b2 = $book->copy(true);
     $this->assertInstanceOf('\\Propel\\Tests\\Bookstore\\Book', $b2);
     $this->assertNull($b2->getId());
     $r2 = $b2->getReviews();
     $this->assertEquals(count($reviews), count($r2));
     // Test a one-to-one object
     $emp = BookstoreEmployeeQuery::create()->findOne();
     $e2 = $emp->copy(true);
     $this->assertInstanceOf('\\Propel\\Tests\\Bookstore\\BookstoreEmployee', $e2);
     $this->assertNull($e2->getId());
     $this->assertEquals($emp->getBookstoreEmployeeAccount()->getLogin(), $e2->getBookstoreEmployeeAccount()->getLogin());
 }
 public function testJoinAliasQuery()
 {
     $con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->join('b.Author a');
     $c->where('a.FirstName = ?', 'Leo');
     $books = BookQuery::create(null, $c)->find($con);
     $expectedSQL = $this->getSql("SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo'");
     $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in where()');
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\BookstoreEmployee', 'be');
     $c->join('be.Supervisor sup');
     $c->join('sup.Subordinate sub');
     $c->where('sub.Name = ?', 'Foo');
     $employees = BookstoreEmployeeQuery::create(null, $c)->find($con);
     $expectedSQL = $this->getSql("SELECT bookstore_employee.ID, bookstore_employee.CLASS_KEY, bookstore_employee.NAME, bookstore_employee.JOB_TITLE, bookstore_employee.SUPERVISOR_ID FROM `bookstore_employee` INNER JOIN `bookstore_employee` `sup` ON (bookstore_employee.SUPERVISOR_ID=sup.ID) INNER JOIN `bookstore_employee` `sub` ON (sup.ID=sub.SUPERVISOR_ID) WHERE sub.NAME = 'Foo'");
     $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'join() allows the use of relation alias in further joins()');
 }
 /**
  * Test inheritance features.
  */
 public function testInheritance()
 {
     $manager = new BookstoreManager();
     $manager->setName("Manager 1");
     $manager->setJobTitle("Warehouse Manager");
     $manager->save();
     $managerId = $manager->getId();
     $employee = new BookstoreEmployee();
     $employee->setName("Employee 1");
     $employee->setJobTitle("Janitor");
     $employee->setSupervisorId($managerId);
     $employee->save();
     $empId = $employee->getId();
     $cashier = new BookstoreCashier();
     $cashier->setName("Cashier 1");
     $cashier->setJobTitle("Cashier");
     $cashier->save();
     $cashierId = $cashier->getId();
     // 1) test the pooled instances'
     $c = new Criteria();
     $c->add(BookstoreEmployeeTableMap::ID, [$managerId, $empId, $cashierId], Criteria::IN);
     $c->addAscendingOrderByColumn(BookstoreEmployeeTableMap::ID);
     $objects = BookstoreEmployeeQuery::create()->doSelect($c);
     $this->assertEquals(3, count($objects), "Expected 3 objects to be returned.");
     list($o1, $o2, $o3) = $objects;
     $this->assertSame($o1, $manager);
     $this->assertSame($o2, $employee);
     $this->assertSame($o3, $cashier);
     // 2) test a forced reload from database
     BookstoreEmployeeTableMap::clearInstancePool();
     list($o1, $o2, $o3) = BookstoreEmployeeQuery::create()->doSelect($c);
     $this->assertTrue($o1 instanceof BookstoreManager, "Expected BookstoreManager object, got " . get_class($o1));
     $this->assertTrue($o2 instanceof BookstoreEmployee, "Expected BookstoreEmployee object, got " . get_class($o2));
     $this->assertTrue($o3 instanceof BookstoreCashier, "Expected BookstoreCashier object, got " . get_class($o3));
 }