Пример #1
0
 public function testSetterCollection()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     BookClubListQuery::create()->deleteAll();
     BookListRelQuery::create()->deleteAll();
     $books = new ObjectCollection();
     for ($i = 0; $i < 10; $i++) {
         $b = new Book();
         $b->setTitle('My Book ' . $i);
         $b->setIsbn($i);
         $books[] = $b;
     }
     $this->assertEquals(10, $books->count());
     // Basic usage
     $bookClubList1 = new BookClubList();
     $bookClubList1->setGroupLeader('BookClubList1 Leader');
     $bookClubList1->setBooks($books);
     $bookClubList1->save();
     $this->assertEquals(10, $bookClubList1->getBooks()->count());
     $this->assertEquals(1, BookClubListQuery::create()->count());
     $this->assertEquals(10, BookQuery::create()->count());
     $this->assertEquals(10, BookListRelQuery::create()->count());
     $i = 0;
     foreach ($bookClubList1->getBooks() as $book) {
         $this->assertEquals('My Book ' . $i, $book->getTitle());
         $this->assertEquals($i++, $book->getIsbn());
     }
     // Remove an element
     $books->shift();
     $this->assertEquals(9, $books->count());
     $bookClubList1->setBooks($books);
     $bookClubList1->save();
     $this->assertEquals(9, $bookClubList1->getBooks()->count());
     $this->assertEquals(1, BookClubListQuery::create()->count());
     $this->assertEquals(9, BookListRelQuery::create()->count());
     $this->assertEquals(10, BookQuery::create()->count());
     // Add a new object
     $newBook = new Book();
     $newBook->setTitle('My New Book');
     $newBook->setIsbn(1234);
     // Kind of new collection
     $books = clone $books;
     $books[] = $newBook;
     $bookClubList1->setBooks($books);
     $bookClubList1->save();
     $this->assertEquals(10, $books->count());
     $this->assertEquals(10, $bookClubList1->getBooks()->count());
     $this->assertEquals(1, BookClubListQuery::create()->count());
     $this->assertEquals(10, BookListRelQuery::create()->count());
     $this->assertEquals(11, BookQuery::create()->count());
     // Add a new object
     $newBook1 = new Book();
     $newBook1->setTitle('My New Book1');
     $newBook1->setIsbn(1256);
     // Existing collection - The fix around reference is tested here.
     // Ths `$books` collection has ever been setted to the `$bookClubList1` object.
     // Here we are adding a new object into the collection but, in this process, it
     // added the new object in the internal `collBooks` of the `$bookClubList1`
     // object too.
     // That's why the new object is not tracked and the cross object is not created,
     // in `addBook()` we consider the `collBooks` ever contains this new object. It's
     // not true but this is the "reference" process.
     // By saying "all new objects have to be added", we solve this issue. To know if
     // it's the best solution is the question.
     $books[] = $newBook1;
     $bookClubList1->setBooks($books);
     $bookClubList1->save();
     $this->assertEquals(11, $books->count());
     $this->assertEquals(11, $bookClubList1->getBooks()->count());
     $this->assertEquals(1, BookClubListQuery::create()->count());
     $this->assertEquals(11, BookListRelQuery::create()->count());
     $this->assertEquals(12, BookQuery::create()->count());
     // Add the same collection
     $books = $bookClubList1->getBooks();
     $bookClubList1->setBooks($books);
     $bookClubList1->save();
     $this->assertEquals(11, $books->count());
     $this->assertEquals(11, $bookClubList1->getBooks()->count());
     $this->assertEquals(1, BookClubListQuery::create()->count());
     $this->assertEquals(11, BookListRelQuery::create()->count());
     $this->assertEquals(12, BookQuery::create()->count());
 }
Пример #2
0
 public function testSetterOneToMany()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     AuthorQuery::create()->deleteAll();
     $coll = new ObjectCollection();
     $coll->setModel('\\Propel\\Tests\\Bookstore\\Book');
     for ($i = 0; $i < 3; $i++) {
         $b = new Book();
         $b->setTitle('Title ' . $i);
         $b->setISBN($i);
         $coll[] = $b;
     }
     $this->assertEquals(3, $coll->count());
     $a = new Author();
     $a->setFirstName('Chuck');
     $a->setLastName('Norris');
     $a->setBooks($coll);
     $a->save();
     $this->assertInstanceOf('\\Propel\\Runtime\\Collection\\ObjectCollection', $a->getBooks());
     $this->assertEquals(3, $a->getBooks()->count());
     $this->assertEquals(1, AuthorQuery::create()->count());
     $this->assertEquals(3, BookQuery::create()->count());
     $coll->shift();
     $this->assertEquals(2, $coll->count());
     $a->setBooks($coll);
     $a->save();
     $this->assertEquals(2, $a->getBooks()->count());
     $this->assertEquals(1, AuthorQuery::create()->count());
     //The book is not deleted because his fk is not required
     $this->assertEquals(3, BookQuery::create()->count());
     $newBook = new Book();
     $newBook->setTitle('My New Book');
     $newBook->setIsbn(1234);
     // Kind of new collection
     $coll = clone $coll;
     $coll[] = $newBook;
     $a->setBooks($coll);
     $a->save();
     $this->assertEquals(3, $coll->count());
     $this->assertEquals(3, $a->getBooks()->count());
     $this->assertEquals(1, AuthorQuery::create()->count());
     $this->assertEquals(4, BookQuery::create()->count());
     // Add a new object
     $newBook1 = new Book();
     $newBook1->setTitle('My New Book1');
     $newBook1->setIsbn(1256);
     // Existing collection - The fix around reference is tested here.
     $coll[] = $newBook1;
     $a->setBooks($coll);
     $a->save();
     $this->assertEquals(4, $coll->count());
     $this->assertEquals(4, $a->getBooks()->count());
     $this->assertEquals(1, AuthorQuery::create()->count());
     $this->assertEquals(5, BookQuery::create()->count());
     // Add the same collection
     $books = $a->getBooks();
     $a->setBooks($books);
     $a->save();
     $this->assertEquals(4, $books->count());
     $this->assertEquals(4, $a->getBooks()->count());
     $this->assertEquals(1, AuthorQuery::create()->count());
     $this->assertEquals(5, BookQuery::create()->count());
 }
Пример #3
0
 public function testFindOneWithLeftJoinWithOneToManyAndNullObjectsAndWithAdditionalJoins()
 {
     BookTableMap::clearInstancePool();
     AuthorTableMap::clearInstancePool();
     BookOpinionTableMap::clearInstancePool();
     BookReaderTableMap::clearInstancePool();
     $freud = new Author();
     $freud->setFirstName("Sigmund");
     $freud->setLastName("Freud");
     $freud->save($this->con);
     $publisher = new Publisher();
     $publisher->setName('Psycho Books');
     $publisher->save();
     $book = new Book();
     $book->setAuthor($freud);
     $book->setTitle('Weirdness');
     $book->setIsbn('abc123456');
     $book->setPrice('14.99');
     $book->setPublisher($publisher);
     $book->save();
     $query = BookQuery::create()->filterByTitle('Weirdness')->innerJoinAuthor()->useBookOpinionQuery(null, Criteria::LEFT_JOIN)->leftJoinBookReader()->endUse()->with('Author')->with('BookOpinion')->with('BookReader');
     $books = $query->find($this->con)->get(0);
     $this->assertEquals(0, count($books->getBookOpinions()));
 }
Пример #4
0
 public function testSetterCollectionWithCustomNamedFKs()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     BookClubListQuery::create()->deleteAll();
     BookListRelQuery::create()->deleteAll();
     BookListFavoriteQuery::create()->deleteAll();
     $books = new ObjectCollection();
     foreach (['foo', 'bar', 'test'] as $title) {
         $b = new Book();
         $b->setTitle($title);
         $b->setIsbn('FA404');
         $books[] = $b;
     }
     $bookClubList = new BookClubList();
     $bookClubList->setGroupLeader('fabpot');
     $bookClubList->setFavoriteBooks($books);
     $bookClubList->save();
     $bookClubList->reload(true);
     $this->assertCount(3, $bookClubList->getFavoriteBooks());
     $bookClubList->reload(true);
     $books->shift();
     $bookClubList->setFavoriteBooks($books);
     $bookClubList->save();
     $bookClubList->reload(true);
     $this->assertCount(2, $bookClubList->getFavoriteBooks());
 }