Example #1
0
 public function testSetterOneToManyReplacesOldObjectsByNewObjects()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     AuthorQuery::create()->deleteAll();
     $books = new PropelObjectCollection();
     foreach (array('foo', 'bar') as $title) {
         $b = new Book();
         $b->setTitle($title);
         $books[] = $b;
     }
     $a = new Author();
     $a->setBooks($books);
     $a->save();
     $books = $a->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;
     }
     $a->setBooks($books);
     $a->save();
     $books = $a->getBooks();
     $this->assertEquals('bam', $books[0]->getTitle());
     $this->assertEquals('bom', $books[1]->getTitle());
     $this->assertEquals(1, AuthorQuery::create()->count());
     $this->assertEquals(2, BookQuery::create()->count());
 }
 public function testSetterOneToManyReplacesOldObjectsByNewObjects()
 {
     // Ensure no data
     BookQuery::create()->deleteAll();
     AuthorQuery::create()->deleteAll();
     $books = new PropelObjectCollection();
     foreach (array('foo', 'bar') as $title) {
         $b = new Book();
         $b->setTitle($title);
         $b->setIsbn('12354');
         $books[] = $b;
     }
     $a = new Author();
     $a->setFirstName('Foo');
     $a->setLastName('Bar');
     $a->setBooks($books);
     $a->save();
     $books = $a->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);
         $b->setIsbn('1235');
         $books[] = $b;
     }
     $a->setBooks($books);
     $a->save();
     $books = $a->getBooks();
     $this->assertEquals('bam', $books[0]->getTitle());
     $this->assertEquals('bom', $books[1]->getTitle());
     $this->assertEquals(1, AuthorQuery::create()->count());
     // the replaced book are still there because the PK is not required
     $this->assertEquals(4, BookQuery::create()->count());
 }
Example #3
0
<?php

use Gries\JsonObjectResolver\JsonResolver;
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/Author.php';
require_once __DIR__ . '/Book.php';
$book = new \Book();
$book->setTitle('test-title-1');
$book2 = new \Book();
$book2->setTitle('test-title-2');
$author = new \Author();
$author->setName('test-author');
$author->setBooks([$book, $book2]);
$resolver = new JsonResolver();
$json = $resolver->encode($author);
$decodedAuthor = $resolver->decode($json);
var_dump($author);
var_dump($decodedAuthor);
//class Author#4 (2) {
//  protected $name =>
//  string(11) "test-author"
//  protected $books =>
//  array(2) {
//    [0] =>
//    class Book#2 (1) {
//      protected $title =>
//      string(12) "test-title-1"
//    }
//    [1] =>
//    class Book#3 (1) {
//      protected $title =>