diff --git a/app/Models/AccountGatewaySettings.php b/app/Models/AccountGatewaySettings.php index 955be73995d5..37f12e90c753 100644 --- a/app/Models/AccountGatewaySettings.php +++ b/app/Models/AccountGatewaySettings.php @@ -20,41 +20,6 @@ class AccountGatewaySettings extends EntityModel return $this->belongsTo('App\Models\GatewayType'); } - /** - * @param null $context - * @return mixed - */ - public static function createNew($context = null) - { - $className = get_called_class(); - $entity = new $className(); - - if ($context) { - $user = $context instanceof User ? $context : $context->user; - $account = $context->account; - } elseif (Auth::check()) { - $user = Auth::user(); - $account = Auth::user()->account; - } else { - Utils::fatalError(); - } - - $entity->user_id = $user->id; - $entity->account_id = $account->id; - - // store references to the original user/account to prevent needing to reload them - $entity->setRelation('user', $user); - $entity->setRelation('account', $account); - - if (method_exists($className, 'trashed')){ - $lastEntity = $className::whereAccountId($entity->account_id)->withTrashed(); - } else { - $lastEntity = $className::whereAccountId($entity->account_id); - } - - return $entity; - } - public function setCreatedAtAttribute($value) { // to Disable created_at diff --git a/app/Models/EntityModel.php b/app/Models/EntityModel.php index 4c0e88d9ac73..b64f40ad549f 100644 --- a/app/Models/EntityModel.php +++ b/app/Models/EntityModel.php @@ -2,6 +2,7 @@ use Auth; use Eloquent; +use Illuminate\Database\QueryException; use Utils; use Validator; @@ -56,13 +57,23 @@ class EntityModel extends Eloquent $lastEntity = $className::whereAccountId($entity->account_id); } - $lastEntity = $lastEntity->orderBy('public_id', 'DESC') - ->first(); - if ($lastEntity) { - $entity->public_id = $lastEntity->public_id + 1; - } else { - $entity->public_id = 1; + try { + $lastEntity = $lastEntity->orderBy('public_id', 'DESC') + ->first(); + + if ($lastEntity) { + $entity->public_id = $lastEntity->public_id + 1; + } else { + $entity->public_id = 1; + } + } catch + (QueryException $ex) { + // Code 42S22 is for an unknown column. + // If we get that code, we'll just swallow the error, since apparently this entity doesn't support public_ids. + if ($ex->getCode() !== '42S22') { + throw $ex; + } } return $entity;