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();
// eager load the contacts
if ($client && ! count($client->contacts)) {
if ($client && ! $client->relationLoaded('contacts')) {
$client->load('contacts');
}

View File

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

View File

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

View File

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

View File

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

View File

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