Example #1
0
 public function testSetterCollectionWithManyToManyModifiedByReferenceWithAnExistingObject()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     BookClubListQuery::create()->deleteAll();
     BookListRelQuery::create()->deleteAll();
     $book = new Book();
     $book->setTitle('foo');
     $book->save();
     // The object isn't "new"
     $this->assertFalse($book->isNew());
     $bookClubList = new BookClubList();
     $books = $bookClubList->getBooks();
     // Add the object by reference
     $books[] = $book;
     $bookClubList->setBooks($books);
     $bookClubList->save();
     $this->assertEquals(1, BookQuery::create()->count());
     $this->assertEquals(1, BookListRelQuery::create()->count());
     $this->assertEquals(1, BookClubListQuery::create()->count());
 }
 public function testManyToManyAdd()
 {
     $list = new BookClubList();
     $list->setGroupLeader('Archimedes Q. Porter');
     $book = new Book();
     $book->setTitle("Jungle Expedition Handbook");
     $book->setISBN('TEST');
     $list->addBook($book);
     $this->assertEquals(1, $list->countBooks(), 'addCrossFk() sets the internal collection properly');
     $this->assertEquals(1, $list->countBookListRels(), 'addCrossFk() sets the internal cross reference collection properly');
     $list->save();
     $this->assertFalse($book->isNew(), 'related object is saved if added');
     $rels = $list->getBookListRels();
     $rel = $rels[0];
     $this->assertFalse($rel->isNew(), 'cross object is saved if added');
     $list->clearBookListRels();
     $list->clearBooks();
     $books = $list->getBooks();
     $expected = new PropelObjectCollection(array($book));
     $expected->setModel('Book');
     $this->assertEquals($expected, $books, 'addCrossFk() adds the object properly');
     $this->assertEquals(1, $list->countBookListRels());
 }
 public function testSetterCollectionReplacesOldObjectsByNewObjects()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     BookClubListQuery::create()->deleteAll();
     BookListRelQuery::create()->deleteAll();
     $books = new PropelObjectCollection();
     foreach (array('foo', 'bar') as $title) {
         $b = new Book();
         $b->setTitle($title);
         $books[] = $b;
     }
     $bookClubList = new BookClubList();
     $bookClubList->setBooks($books);
     $bookClubList->save();
     $books = $bookClubList->getBooks();
     $this->assertEquals('foo', $books[0]->getTitle());
     $this->assertEquals('bar', $books[1]->getTitle());
     $books = new PropelObjectCollection();
     foreach (array('bam', 'bom') as $title) {
         $b = new Book();
         $b->setTitle($title);
         $books[] = $b;
     }
     $bookClubList->setBooks($books);
     $bookClubList->save();
     $books = $bookClubList->getBooks();
     $this->assertEquals('bam', $books[0]->getTitle());
     $this->assertEquals('bom', $books[1]->getTitle());
     $this->assertEquals(1, BookClubListQuery::create()->count());
     $this->assertEquals(2, BookListRelQuery::create()->count());
     $this->assertEquals(4, BookQuery::create()->count());
 }