Ejemplo n.º 1
0
 protected function createBooks($nb = 15, $con = null)
 {
     BookQuery::create()->deleteAll($con);
     $books = new PropelObjectCollection();
     $books->setModel('\\Propel\\Tests\\Bookstore\\Book');
     for ($i = 0; $i < $nb; $i++) {
         $b = new Book();
         $b->setTitle('Book' . $i);
         $books[] = $b;
     }
     $books->save($con);
 }
 public function testFromArray()
 {
     $author = new Author();
     $author->setFirstName('Jane');
     $author->setLastName('Austen');
     $author->save();
     $books = array(array('Title' => 'Mansfield Park', 'AuthorId' => $author->getId()), array('Title' => 'Pride And PRejudice', 'AuthorId' => $author->getId()));
     $col = new PropelObjectCollection();
     $col->setModel('\\Propel\\Tests\\Bookstore\\Book');
     $col->fromArray($books);
     $col->save();
     $nbBooks = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book')->count();
     $this->assertEquals(6, $nbBooks);
     $booksByJane = PropelQuery::from('Book b')->join('b.Author a')->where('a.LastName = ?', 'Austen')->count();
     $this->assertEquals(2, $booksByJane);
 }
 protected function populateCreatedAt()
 {
     Table2Query::create()->deleteAll();
     $ts = new PropelObjectCollection();
     $ts->setModel('\\Propel\\Tests\\Bookstore\\Behavior\\Table2');
     for ($i = 0; $i < 10; $i++) {
         $t = new Table2();
         $t->setTitle('CreatedAt' . $i);
         $t->setCreatedAt(time() - $i * 24 * 60 * 60 - 30);
         $ts[] = $t;
     }
     $ts->save();
 }
 /**
  * Makes an additional query to populate the objects related to the collection objects
  * by a certain relation
  *
  * @param     string     $relation  Relation name (e.g. 'Book')
  * @param     Criteria   $criteria  Optional Criteria object to filter the related object collection
  * @param     PropelPDO  $con       Optional connection object
  *
  * @return    PropelObjectCollection  The list of related objects
  */
 public function populateRelation($relation, $criteria = null, $con = null)
 {
     if (!Propel::isInstancePoolingEnabled()) {
         throw new PropelException('populateRelation() needs instance pooling to be enabled prior to populating the collection');
     }
     $relationMap = $this->getFormatter()->getTableMap()->getRelation($relation);
     if ($this->isEmpty()) {
         // save a useless query and return an empty collection
         $coll = new PropelObjectCollection();
         $coll->setModel($relationMap->getRightTable()->getClassname());
         return $coll;
     }
     $symRelationMap = $relationMap->getSymmetricalRelation();
     $query = PropelQuery::from($relationMap->getRightTable()->getClassname());
     if (null !== $criteria) {
         $query->mergeWith($criteria);
     }
     // query the db for the related objects
     $filterMethod = 'filterBy' . $symRelationMap->getName();
     $relatedObjects = $query->{$filterMethod}($this)->find($con);
     if ($relationMap->getType() == RelationMap::ONE_TO_MANY) {
         // initialize the embedded collections of the main objects
         $relationName = $relationMap->getName();
         foreach ($this as $mainObj) {
             $mainObj->initRelation($relationName);
         }
         // associate the related objects to the main objects
         $getMethod = 'get' . $symRelationMap->getName();
         $addMethod = 'add' . $relationName;
         foreach ($relatedObjects as $object) {
             $mainObj = $object->{$getMethod}();
             // instance pool is used here to avoid a query
             $mainObj->{$addMethod}($object);
         }
     } elseif ($relationMap->getType() == RelationMap::MANY_TO_ONE) {
         // nothing to do; the instance pool will catch all calls to getRelatedObject()
         // and return the object in memory
     } else {
         throw new PropelException('populateRelation() does not support this relation type');
     }
     return $relatedObjects;
 }
    public function testToStringUsesCustomStringFormat()
    {
        $coll = new PropelObjectCollection();
        $coll->setModel('\\Propel\\Tests\\Bookstore\\Publisher');
        $publisher = new Publisher();
        $publisher->setId(12345);
        $publisher->setName('Penguinoo');
        $coll[] = $publisher;
        $expected = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<data>
  <PropelTestsBookstorePublisher>
    <Id>12345</Id>
    <Name><![CDATA[Penguinoo]]></Name>
  </PropelTestsBookstorePublisher>
</data>

EOF;
        $this->assertEquals($expected, (string) $coll);
    }
 public function testPopulateRelationOneToManyWithEmptyCollection()
 {
     $author = new Author();
     $author->setLastName('I who never wrote');
     $author->save($this->con);
     AuthorPeer::clearInstancePool();
     BookPeer::clearInstancePool();
     $coll = new PropelObjectCollection();
     $coll->setFormatter(new PropelObjectFormatter(new ModelCriteria(null, 'Author')));
     $coll[] = $author;
     $books = $coll->populateRelation('Book', null, $this->con);
     $this->assertEquals(0, $books->count());
     $count = $this->con->getQueryCount();
     $this->assertEquals(0, $author->countBooks());
     $this->assertEquals($count, $this->con->getQueryCount());
 }