Skip to content

Commit

Permalink
Error message when you try to add a user that already exists.
Browse files Browse the repository at this point in the history
  • Loading branch information
rorteg committed Feb 12, 2019
1 parent 39696d5 commit c75e1dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 29 deletions.
50 changes: 26 additions & 24 deletions app/code/Framework/Model/ModelAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(ConnectionInterface $connection)
* @param null|string $column
* @return $this
*/
public function load($idOrColumnValue, $column = null) : ModelInterface
public function load($idOrColumnValue, $column = null): ModelInterface
{
if (is_null($column)) {
$column = 'id';
Expand All @@ -64,7 +64,7 @@ public function load($idOrColumnValue, $column = null) : ModelInterface

$this->setData($result);

if (! isset($this->data['id'])) {
if (!isset($this->data['id'])) {
$this->data = [];
}
} catch (\Exception $e) {
Expand All @@ -80,11 +80,16 @@ public function load($idOrColumnValue, $column = null) : ModelInterface
* @param array $data
* @return $this
*/
public function setData($dataOrKey, $data = null) : ModelInterface
public function setData($dataOrKey, $data = null): ModelInterface
{
if (! is_null($data)) {
if (!is_null($data)) {
$this->data[$dataOrKey] = $data;
} else {
if (!is_array($dataOrKey)) {
throw new \InvalidArgumentException(
'If you do not pass a key to the method, you must pass an array.'
);
}
$this->data = array_merge($this->data, $dataOrKey);
}

Expand All @@ -107,41 +112,38 @@ public function getData($key = false)
/**
* @return $this
*/
public function save() : ModelInterface
public function save(): ModelInterface
{
try {
if (isset($this->data['id'])) {
// Update

if (count($this->getData())) {
$id = $this->connection->update(
$this->getTableName(),
$this->getData()
);

$this->setData('id', $id);
}
} else {
// New Records
$id = $this->connection->insert(

if (isset($this->data['id'])) {
// Update

if (count($this->getData())) {
$id = $this->connection->update(
$this->getTableName(),
$this->getData()
);

$this->setData('id', $id);
}
} else {
// New Records
$id = $this->connection->insert(
$this->getTableName(),
$this->getData()
);

return $this;
} catch (\Exception $e) {
echo $e->getMessage();
$this->setData('id', $id);
}

return $this;
}

/**
* Delete entity
* @return bool
*/
public function delete() : bool
public function delete(): bool
{
try {
$this->connection->delete(
Expand Down
19 changes: 14 additions & 5 deletions app/code/User/Controllers/Admin/UserNewOrUpdateAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,21 @@ private function addNewUser($postParams) : void

$user = $this->user;
$user->setData($postParams);
$user->save();

FlashMessage::addNotificationMessage(
FlashMessage::TYPE_SUCCESS,
'User added successfully!'
);
try {
$user->save();

FlashMessage::addNotificationMessage(
FlashMessage::TYPE_SUCCESS,
'User added successfully!'
);

} catch (\Exception $e) {
FlashMessage::addNotificationMessage(
FlashMessage::TYPE_DANGER,
'There was a problem trying to save the user.'
);
}

$this->redirect('/admin/user');
}
Expand Down

0 comments on commit c75e1dd

Please sign in to comment.