Let createNew in EntityModel determine whether public_id is needed

This commit is contained in:
Joshua Dwire 2016-09-14 10:27:10 -04:00
parent a5580cc650
commit 76af2f3e13
2 changed files with 17 additions and 41 deletions

View File

@ -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

View File

@ -2,6 +2,7 @@
use Auth;
use Eloquent;
use Illuminate\Database\QueryException;
use Utils;
use Validator;
@ -56,6 +57,8 @@ class EntityModel extends Eloquent
$lastEntity = $className::whereAccountId($entity->account_id);
}
try {
$lastEntity = $lastEntity->orderBy('public_id', 'DESC')
->first();
@ -64,6 +67,14 @@ class EntityModel extends Eloquent
} 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;
}