示例#1
0
 public function testSetterCollectionReplacesOldObjectsByNewObjects()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     BookClubListQuery::create()->deleteAll();
     BookListRelQuery::create()->deleteAll();
     $books = new ObjectCollection();
     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 ObjectCollection();
     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());
 }
 public function testSetterCollectionWithManyToManyModifiedByReferenceWithAnExistingObject()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     BookClubListQuery::create()->deleteAll();
     BookListRelQuery::create()->deleteAll();
     $book = new Book();
     $book->setTitle('foo');
     $book->setISBN('01234');
     $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->setGroupLeader('TestLeader');
     $bookClubList->save();
     $this->assertEquals(1, BookQuery::create()->count());
     $this->assertEquals(1, BookListRelQuery::create()->count());
     $this->assertEquals(1, BookClubListQuery::create()->count());
 }