public function testIsNew() { $book = new Book(); $this->assertEquals($book->isNew(), true); $book->id = 123; $this->assertEquals($book->isNew(), false); }
/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param PropelPDO $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aCategory !== null) { if ($this->aCategory->isModified() || $this->aCategory->isNew()) { $affectedRows += $this->aCategory->save($con); } $this->setCategory($this->aCategory); } if ($this->aBook !== null) { if ($this->aBook->isModified() || $this->aBook->isNew()) { $affectedRows += $this->aBook->save($con); } $this->setBook($this->aBook); } if ($this->isNew()) { $this->modifiedColumns[] = ArticlePeer::ID; } // If this object has been modified, then save it to the database. if ($this->isModified()) { if ($this->isNew()) { $pk = ArticlePeer::doInsert($this, $con); $affectedRows += 1; // we are assuming that there is only 1 row per doInsert() which // should always be true here (even though technically // BasePeer::doInsert() can insert multiple rows). $this->setId($pk); //[IMV] update autoincrement primary key $this->setNew(false); } else { $affectedRows += ArticlePeer::doUpdate($this, $con); } $this->resetModified(); // [HL] After being saved an object is no longer 'modified' } if ($this->collAuthorArticles !== null) { foreach ($this->collAuthorArticles as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } if ($this->collAttachments !== null) { foreach ($this->collAttachments as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } $this->alreadyInSave = false; } return $affectedRows; }
public function testClear() { $b = new Book(); $b->setNew(false); $b->clear(); $this->assertTrue($b->isNew(), 'clear() sets the object to new'); $b = new Book(); $b->setDeleted(true); $b->clear(); $this->assertFalse($b->isDeleted(), 'clear() sets the object to not deleted'); }
public function testSaveCanInsertNonEmptyObjects() { $b = new Book(); $b->setTitle('foo'); $b->setIsbn('124'); $b->save(); $this->assertFalse($b->isNew()); $this->assertNotNull($b->getId()); }
public function testSetterCollectionWithManyToManyModifiedByReferenceWithAnExistingObject() { // Ensure no data BookQuery::create()->deleteAll(); BookClubListQuery::create()->deleteAll(); BookListRelQuery::create()->deleteAll(); $book = new Book(); $book->setTitle('foo'); $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->save(); $this->assertEquals(1, BookQuery::create()->count()); $this->assertEquals(1, BookListRelQuery::create()->count()); $this->assertEquals(1, BookClubListQuery::create()->count()); }
public function testHasKeyId() { // after setting a key ID on a new object, it shall still be considered new but shall be aware of it's key ID. $book = new Book(); $book->setKeyId(1); $this->assertTrue($book->hasKeyId(), 'New object should be aware of its key when it was explicitly set via setKeyId.'); $this->assertTrue($book->isNew(), 'New object should still be new after its key was explicitly set via setKeyId.'); // make sure it works when calling the explicit setters for the columns making up the primary key. $book = new Book(); $book->setBookId(1); $this->assertTrue($book->hasKeyId(), 'New object should be aware of its key when it was explicitly set via setters.'); $this->assertTrue($book->isNew(), 'New object should still be new after its key was explicitly set via setters.'); }
public function testCreateDelete() { $connection = $this->getT4Connection(); Book::setConnection($connection); $book = new Book(); $book->title = 'War and peace'; $book->author = 'Tolstoi'; $this->assertTrue($book->isNew()); $this->assertFalse($book->isDeleted()); $book->save(); $this->assertFalse($book->isNew()); $this->assertTrue($book->wasNew()); $this->assertFalse($book->isDeleted()); $this->assertEquals(1, $book->getPk()); $book1 = Book::findByPK(1); $this->assertEquals(1, $book1->getPk()); $this->assertEquals('War and peace', $book1->title); $this->assertEquals('Tolstoi', $book1->author); $book->delete(); $this->assertFalse($book->isNew()); $this->assertTrue($book->wasNew()); $this->assertTrue($book->isDeleted()); $count = $connection->query("SELECT COUNT(*) FROM `books`")->fetchScalar(); $this->assertEquals(0, $count); }
/** * Performs the work of inserting or updating the row in the database. * * If the object is new, it inserts it; otherwise an update is performed. * All related objects are also updated in this method. * * @param PropelPDO $con * @return int The number of rows affected by this insert/update and any referring fk objects' save() operations. * @throws PropelException * @see save() */ protected function doSave(PropelPDO $con) { $affectedRows = 0; // initialize var to track total num of affected rows if (!$this->alreadyInSave) { $this->alreadyInSave = true; // We call the save method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aBook !== null) { if ($this->aBook->isModified() || $this->aBook->isNew()) { $affectedRows += $this->aBook->save($con); } $this->setBook($this->aBook); } if ($this->aItemRelatedByPackageId !== null) { if ($this->aItemRelatedByPackageId->isModified() || $this->aItemRelatedByPackageId->isNew()) { $affectedRows += $this->aItemRelatedByPackageId->save($con); } $this->setItemRelatedByPackageId($this->aItemRelatedByPackageId); } if ($this->isNew() || $this->isModified()) { // persist changes if ($this->isNew()) { $this->doInsert($con); } else { $this->doUpdate($con); } $affectedRows += 1; $this->resetModified(); } if ($this->sectionsScheduledForDeletion !== null) { if (!$this->sectionsScheduledForDeletion->isEmpty()) { SectionHasItemQuery::create()->filterByPrimaryKeys($this->sectionsScheduledForDeletion->getPrimaryKeys(false))->delete($con); $this->sectionsScheduledForDeletion = null; } foreach ($this->getSections() as $section) { if ($section->isModified()) { $section->save($con); } } } if ($this->itemsRelatedByIdScheduledForDeletion !== null) { if (!$this->itemsRelatedByIdScheduledForDeletion->isEmpty()) { ItemQuery::create()->filterByPrimaryKeys($this->itemsRelatedByIdScheduledForDeletion->getPrimaryKeys(false))->delete($con); $this->itemsRelatedByIdScheduledForDeletion = null; } } if ($this->collItemsRelatedById !== null) { foreach ($this->collItemsRelatedById as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } if ($this->sectionHasItemsScheduledForDeletion !== null) { if (!$this->sectionHasItemsScheduledForDeletion->isEmpty()) { SectionHasItemQuery::create()->filterByPrimaryKeys($this->sectionHasItemsScheduledForDeletion->getPrimaryKeys(false))->delete($con); $this->sectionHasItemsScheduledForDeletion = null; } } if ($this->collSectionHasItems !== null) { foreach ($this->collSectionHasItems as $referrerFK) { if (!$referrerFK->isDeleted()) { $affectedRows += $referrerFK->save($con); } } } $this->alreadyInSave = false; } return $affectedRows; }