mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Fix is / is not already joined.
This commit is contained in:
parent
6b277c5e67
commit
ca32ae4561
39
app/Models/BaseModel.php
Normal file
39
app/Models/BaseModel.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
/**
|
||||
* BaseModel.php
|
||||
* Copyright (C) 2016 Sander Dorigo
|
||||
*
|
||||
* This software may be modified and distributed under the terms
|
||||
* of the MIT license. See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace FireflyIII\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
/**
|
||||
* Class BaseModel
|
||||
*
|
||||
* @package FireflyIII\Models
|
||||
*/
|
||||
class BaseModel extends Model
|
||||
{
|
||||
/**
|
||||
* @param $query
|
||||
* @param $table
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function isJoined($query, $table)
|
||||
{
|
||||
$joins = $query->getQuery()->joins;
|
||||
if($joins == null) {
|
||||
return false;
|
||||
}
|
||||
foreach ($joins as $join) {
|
||||
if ($join->table == $table) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
@ -4,7 +4,6 @@ use Auth;
|
||||
use Carbon\Carbon;
|
||||
use Crypt;
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Query\JoinClause;
|
||||
@ -46,7 +45,7 @@ use Watson\Validating\ValidatingTrait;
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal before($date)
|
||||
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal transactionTypes($types)
|
||||
*/
|
||||
class TransactionJournal extends Model
|
||||
class TransactionJournal extends BaseModel
|
||||
{
|
||||
use SoftDeletes, ValidatingTrait;
|
||||
|
||||
@ -92,8 +91,6 @@ class TransactionJournal extends Model
|
||||
'date' => 'required|date',
|
||||
'encrypted' => 'required|boolean',
|
||||
];
|
||||
/** @var bool */
|
||||
private $joinedTypes = false;
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
@ -312,8 +309,9 @@ class TransactionJournal extends Model
|
||||
public function scopeExpanded(EloquentBuilder $query)
|
||||
{
|
||||
// left join transaction type:
|
||||
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
|
||||
$this->joinedTypes = true;
|
||||
if (!self::isJoined($query, 'transaction_types')) {
|
||||
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
|
||||
}
|
||||
|
||||
// left join transaction currency:
|
||||
$query->leftJoin('transaction_currencies', 'transaction_currencies.id', '=', 'transaction_journals.transaction_currency_id');
|
||||
@ -356,9 +354,8 @@ class TransactionJournal extends Model
|
||||
public function scopeTransactionTypes(EloquentBuilder $query, array $types)
|
||||
{
|
||||
|
||||
if (!$this->joinedTypes) {
|
||||
if (!self::isJoined($query, 'transaction_types')) {
|
||||
$query->leftJoin('transaction_types', 'transaction_types.id', '=', 'transaction_journals.transaction_type_id');
|
||||
$this->joinedTypes = true;
|
||||
}
|
||||
$query->whereIn('transaction_types.type', $types);
|
||||
}
|
||||
|
@ -115,13 +115,11 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
||||
{
|
||||
$offset = $page > 0 ? $page * 50 : 0;
|
||||
|
||||
return $category->transactionjournals()->withRelevantData()->take(50)->offset($offset)
|
||||
return $category->transactionjournals()->expanded()->take(50)->offset($offset)
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.order', 'ASC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->get(
|
||||
['transaction_journals.*']
|
||||
);
|
||||
->get(TransactionJournal::QUERYFIELDS);
|
||||
|
||||
}
|
||||
|
||||
@ -140,13 +138,13 @@ class SingleCategoryRepository extends ComponentRepository implements SingleCate
|
||||
return $category->transactionjournals()
|
||||
->after($start)
|
||||
->before($end)
|
||||
->withRelevantData()->take(50)->offset($offset)
|
||||
->expanded()
|
||||
->take(50)
|
||||
->offset($offset)
|
||||
->orderBy('transaction_journals.date', 'DESC')
|
||||
->orderBy('transaction_journals.order', 'ASC')
|
||||
->orderBy('transaction_journals.id', 'DESC')
|
||||
->get(
|
||||
['transaction_journals.*']
|
||||
);
|
||||
->get(TransactionJournal::QUERYFIELDS);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user