Added relationLoaded checks to optimize queries

This commit is contained in:
Hillel Coren 2016-05-05 10:15:51 +03:00
parent 7744c6c41c
commit 2a5de605dc
6 changed files with 18 additions and 12 deletions

View File

@ -9,7 +9,7 @@ class ClientRequest extends EntityRequest {
$client = parent::entity(); $client = parent::entity();
// eager load the contacts // eager load the contacts
if ($client && ! count($client->contacts)) { if ($client && ! $client->relationLoaded('contacts')) {
$client->load('contacts'); $client->load('contacts');
} }

View File

@ -8,8 +8,8 @@ class ExpenseRequest extends EntityRequest {
{ {
$expense = parent::entity(); $expense = parent::entity();
// eager load the contacts // eager load the documents
if ($expense && ! count($expense->documents)) { if ($expense && ! $expense->relationLoaded('documents')) {
$expense->load('documents'); $expense->load('documents');
} }

View File

@ -8,8 +8,8 @@ class InvoiceRequest extends EntityRequest {
{ {
$invoice = parent::entity(); $invoice = parent::entity();
// eager load the contacts // eager load the invoice items
if ($invoice && ! count($invoice->invoice_items)) { if ($invoice && ! $invoice->relationLoaded('invoice_items')) {
$invoice->load('invoice_items'); $invoice->load('invoice_items');
} }

View File

@ -9,7 +9,7 @@ class VendorRequest extends EntityRequest {
$vendor = parent::entity(); $vendor = parent::entity();
// eager load the contacts // eager load the contacts
if ($vendor && ! count($vendor->vendor_contacts)) { if ($vendor && ! $vendor->relationLoaded('vendor_contacts')) {
$vendor->load('vendor_contacts'); $vendor->load('vendor_contacts');
} }

View File

@ -212,7 +212,9 @@ class Account extends Eloquent
public function isGatewayConfigured($gatewayId = 0) public function isGatewayConfigured($gatewayId = 0)
{ {
if ( ! $this->relationLoaded('account_gateways')) {
$this->load('account_gateways'); $this->load('account_gateways');
}
if ($gatewayId) { if ($gatewayId) {
return $this->getGatewayConfig($gatewayId) != false; return $this->getGatewayConfig($gatewayId) != false;
@ -241,7 +243,7 @@ class Account extends Eloquent
return $this->name; return $this->name;
} }
$this->load('users'); //$this->load('users');
$user = $this->users()->first(); $user = $this->users()->first();
return $user->getDisplayName(); return $user->getDisplayName();

View File

@ -256,13 +256,17 @@ class Client extends EntityModel
public function getGatewayToken() public function getGatewayToken()
{ {
$this->account->load('account_gateways'); $account = $this->account;
if (!count($this->account->account_gateways)) { if ( ! $account->relationLoaded('account_gateways')) {
$account->load('account_gateways');
}
if (!count($account->account_gateways)) {
return false; return false;
} }
$accountGateway = $this->account->getGatewayConfig(GATEWAY_STRIPE); $accountGateway = $account->getGatewayConfig(GATEWAY_STRIPE);
if (!$accountGateway) { if (!$accountGateway) {
return false; return false;