/** * Tests the Base[Object]::toArray() method */ public function testToArray() { $types = array(BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM); $book = new Book(); $book->fromArray(array('Title' => 'Harry Potter and the Order of the Phoenix', 'ISBN' => '043935806X')); $expecteds = array(BasePeer::TYPE_PHPNAME => array('Title' => 'Harry Potter and the Order of the Phoenix', 'ISBN' => '043935806X'), BasePeer::TYPE_STUDLYPHPNAME => array('title' => 'Harry Potter and the Order of the Phoenix', 'iSBN' => '043935806X'), BasePeer::TYPE_COLNAME => array('book.TITLE' => 'Harry Potter and the Order of the Phoenix', 'book.ISBN' => '043935806X'), BasePeer::TYPE_FIELDNAME => array('title' => 'Harry Potter and the Order of the Phoenix', 'isbn' => '043935806X'), BasePeer::TYPE_NUM => array('1' => 'Harry Potter and the Order of the Phoenix', '2' => '043935806X')); foreach ($types as $type) { $expected = $expecteds[$type]; $result = $book->toArray($type); // remove ID since its autoincremented at each test iteration $result = array_slice($result, 1, 2, true); $this->assertEquals($expected, $result, 'expected was: ' . print_r($expected, 1) . 'but toArray() returned ' . print_r($result, 1)); } }
/** * @test */ public function it_fills_model_with_given_fields_having_a_validation_rule() { $bookFields = ['title' => 'Gone with the wind', 'author' => 'Margaret Mitchell']; $book = new Book($bookFields); $this->assertSame($bookFields, $book->toArray()); }
public function testToArrayKeyType() { $b = new Book(); $b->setTitle('Don Juan'); $arr1 = $b->toArray(BasePeer::TYPE_COLNAME); $expectedKeys = array(BookPeer::ID, BookPeer::TITLE, BookPeer::ISBN, BookPeer::PRICE, BookPeer::PUBLISHER_ID, BookPeer::AUTHOR_ID); $this->assertEquals($expectedKeys, array_keys($arr1), 'toArray() accepts a $keyType parameter to change the result keys'); $this->assertEquals('Don Juan', $arr1[BookPeer::TITLE], 'toArray() returns an associative array representation of the object'); }
public function testToArrayDeep() { $author = new Author(); $author->setId(5678); $author->setFirstName('George'); $author->setLastName('Byron'); $book = new Book(); $book->setId(9012); $book->setTitle('Don Juan'); $book->setISBN('0140422161'); $book->setPrice(12.99); $book->setAuthor($author); $coll = new PropelArrayCollection(); $coll->setModel('Book'); $coll[] = $book->toArray(BasePeer::TYPE_PHPNAME, true, array(), true); $expected = array(array('Id' => 9012, 'Title' => 'Don Juan', 'ISBN' => '0140422161', 'Price' => 12.99, 'PublisherId' => null, 'AuthorId' => 5678, 'Author' => array('Id' => 5678, 'FirstName' => 'George', 'LastName' => 'Byron', 'Email' => null, 'Age' => null, 'Books' => array('Book_0' => '*RECURSION*')))); $this->assertEquals($expected, $coll->toArray()); }
/** * Exports the object as an array. * * You can specify the key type of the array by passing one of the class * type constants. * * @param string $keyType (optional) One of the class type constants BasePeer::TYPE_PHPNAME, BasePeer::TYPE_STUDLYPHPNAME, * BasePeer::TYPE_COLNAME, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_NUM. * Defaults to BasePeer::TYPE_PHPNAME. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy loaded columns. Defaults to TRUE. * @param array $alreadyDumpedObjects List of objects to skip to avoid recursion * @param boolean $includeForeignObjects (optional) Whether to include hydrated related objects. Default to FALSE. * * @return array an associative array containing the field names (as keys) and field values */ public function toArray($keyType = BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns = true, $alreadyDumpedObjects = array(), $includeForeignObjects = false) { if (isset($alreadyDumpedObjects['Item'][$this->getPrimaryKey()])) { return '*RECURSION*'; } $alreadyDumpedObjects['Item'][$this->getPrimaryKey()] = true; $keys = ItemPeer::getFieldNames($keyType); $result = array($keys[0] => $this->getId(), $keys[1] => $this->getIsbn(), $keys[2] => $this->getPackageId(), $keys[3] => $this->getIsPackage(), $keys[4] => $this->getTitle(), $keys[5] => $this->getAuthor(), $keys[6] => $this->getEdition(), $keys[7] => $this->getPublisher(), $keys[8] => $this->getBNew(), $keys[9] => $this->getBUsed(), $keys[10] => $this->getBEbook(), $keys[11] => $this->getImageUrl(), $keys[12] => $this->getProductId(), $keys[13] => $this->getPartNumber(), $keys[14] => $this->getSpideredAt(), $keys[15] => $this->getShallowSpideredAt(), $keys[16] => $this->getTouched(), $keys[17] => $this->getBId(), $keys[18] => $this->getBookstoreType(), $keys[19] => $this->getCreatedAt(), $keys[20] => $this->getUpdatedAt()); if ($includeForeignObjects) { if (null !== $this->aBook) { $result['Book'] = $this->aBook->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } if (null !== $this->aItemRelatedByPackageId) { $result['ItemRelatedByPackageId'] = $this->aItemRelatedByPackageId->toArray($keyType, $includeLazyLoadColumns, $alreadyDumpedObjects, true); } if (null !== $this->collItemsRelatedById) { $result['ItemsRelatedById'] = $this->collItemsRelatedById->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects); } if (null !== $this->collSectionHasItems) { $result['SectionHasItems'] = $this->collSectionHasItems->toArray(null, true, $keyType, $includeLazyLoadColumns, $alreadyDumpedObjects); } } return $result; }
public function updateBook(Book &$dbBook) { $html = PtHttpCk101::getBookContentPage($dbBook->id, 1); $book = PtParserCk101::parseBookFromThread($html); if ($book == null) { $dbBook->update_time = time(); $this->db->editBook(Book::toArray($dbBook)); $this->d("Error! The Book not exists. {$dbBook->id} {$dbBook->name}"); return; } $book->current_pages = max($dbBook->current_pages, 1); if ($book->posts == 0) { $this->d("Error! Can not parse pages! {$book->id}, {$book->current_pages}/{$book->pages}, {$book->name}"); return; } do { $this->d("Start to update book {$book->id}, {$book->current_pages}/{$book->pages}, {$book->name}"); if ($book->current_pages != 1) { // page 1 is already downloaded $html = PtHttpCk101::getBookContentPage($dbBook->id, $book->current_pages); } if ($html == null) { $this->d("Get content text error! Ignore this book"); break; } $text = PtParserCk101::parseContentFromThread($html); PtFile::saveBook($book->id, $book->current_pages, $text); $this->db->editBook(Book::toArray($book)); sleep(self::QUERY_SLEEP); } while (++$book->current_pages <= $book->pages); }