mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Let createNew in EntityModel determine whether public_id is needed
This commit is contained in:
parent
a5580cc650
commit
76af2f3e13
@ -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
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user