protected function create() { if (isset($_POST["name"])) { $group = GroupQuery::create()->filterByOwner($this->data["loggedUser"])->filterByName($_POST["name"])->findOne(); if ($group) { $this->sendFlashMessage("You already have a group with this name.", "error"); $this->redirect($this->data["referersURI"]); } $group = new Group(); $group->setName($_POST["name"]); $group->setOwner($this->data["loggedUser"]); $group->setDescription(isset($_POST["description"]) ? $_POST["description"] : null); if (!$group->save()) { $failures = $group->getValidationFailures(); if (count($failures) > 0) { foreach ($failures as $failure) { $this->sendFlashMessage("Group has not been created. " . $failure->getMessage(), "error"); } } $this->redirect("/group/new"); } if (isset($_POST["users"])) { $users = array_map("trim", explode(",", $_POST["users"])); foreach ($users as $username) { $u = UserQuery::create()->findOneByUsername($username); if ($u) { if ($u == $this->data["loggedUser"]) { $this->sendFlashMessage("You can not add yourself to group.", "error"); continue; } $userGroup = UserGroupQuery::create()->filterByUser($u)->filterByGroup($group)->findOne(); if ($userGroup) { $this->sendFlashMessage("User " . $username . " is already in this group.", "error"); continue; } $group->addUser($u); } else { $this->sendFlashMessage("User " . $username . " does not exist.", "error"); } } $group->save(); } $this->sendFlashMessage("Group has been successfully created.", "success"); $this->redirect("/group/" . $group->getId()); } 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->aGroup !== null) { if ($this->aGroup->isModified() || $this->aGroup->isNew()) { $affectedRows += $this->aGroup->save($con); } $this->setGroup($this->aGroup); } 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; }