本文整理匯總了PHP中Author::setBooks方法的典型用法代碼示例。如果您正苦於以下問題:PHP Author::setBooks方法的具體用法?PHP Author::setBooks怎麽用?PHP Author::setBooks使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Author
的用法示例。
在下文中一共展示了Author::setBooks方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: testSetterOneToManyReplacesOldObjectsByNewObjects
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());
}
示例2: testSetterOneToManyReplacesOldObjectsByNewObjects
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());
}
示例3: JsonResolver
<?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 =>