protected function create() { if (isset($_POST["name"])) { $pack = PackQuery::create()->filterByOwner($this->data["loggedUser"])->filterByName($_POST["name"])->findOne(); if ($pack) { $this->sendFlashMessage("Your pack has not been created. You already have pack with this name.", "error"); $this->redirect("/pack/new"); } $pack = new Pack(); $pack->setName($_POST["name"]); $pack->setOwner($this->data["loggedUser"]); $pack->setPrivate(isset($_POST["private"])); $pack->setDescription(isset($_POST["description"]) ? $_POST["description"] : null); if ($pack->save() <= 0) { $failures = $pack->getValidationFailures(); if (count($failures) > 0) { foreach ($failures as $failure) { $this->sendFlashMessage("Your pack has not been created. " . $failure->getMessage(), "error"); } $this->redirect($this->data["referersURI"]); } } $this->sendFlashMessage("You have successfuly created new pack.", "success"); $this->redirect("/pack/" . $pack->getId()); } else { setHTTPStatusCode("400"); } }
/** * 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 ConnectionInterface $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(ConnectionInterface $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 corresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aUser !== null) { if ($this->aUser->isModified() || $this->aUser->isNew()) { $affectedRows += $this->aUser->save($con); } $this->setUser($this->aUser); } if ($this->aPack !== null) { if ($this->aPack->isModified() || $this->aPack->isNew()) { $affectedRows += $this->aPack->save($con); } $this->setPack($this->aPack); } if ($this->isNew() || $this->isModified()) { // persist changes if ($this->isNew()) { $this->doInsert($con); $affectedRows += 1; } else { $affectedRows += $this->doUpdate($con); } $this->resetModified(); } $this->alreadyInSave = false; } return $affectedRows; }