diff --git a/app/Models/Tag.php b/app/Models/Tag.php index 86e03e0ccf..314ca4fd19 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -96,6 +96,32 @@ class Tag extends Model return ['created_at', 'updated_at', 'date']; } + /** + * Save the model to the database. + * + * @param array $options + * + * @return bool + */ + public function save(array $options = []) + { + foreach ($this->transactionjournals()->get() as $journal) { + $count = $journal->tags()->count(); + $journal->tag_count = $count; + $journal->save(); + } + parent::save($options); + } + + /** + * @codeCoverageIgnore + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function transactionjournals() + { + return $this->belongsToMany('FireflyIII\Models\TransactionJournal'); + } + /** * @codeCoverageIgnore * @@ -140,15 +166,6 @@ class Tag extends Model $this->attributes['tag'] = Crypt::encrypt($value); } - /** - * @codeCoverageIgnore - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany - */ - public function transactionjournals() - { - return $this->belongsToMany('FireflyIII\Models\TransactionJournal'); - } - /** * @codeCoverageIgnore * @return \Illuminate\Database\Eloquent\Relations\BelongsTo diff --git a/app/Models/TransactionJournal.php b/app/Models/TransactionJournal.php index 80cc33794d..b5d8298396 100644 --- a/app/Models/TransactionJournal.php +++ b/app/Models/TransactionJournal.php @@ -72,15 +72,19 @@ use Watson\Validating\ValidatingTrait; * @method static \FireflyIII\Models\TransactionJournal orderBy * @method static \FireflyIII\Models\TransactionJournal|null first * @property-read mixed $source_account + * @property integer $tag_count + * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\TransactionJournal whereTagCount($value) */ class TransactionJournal extends Model { use SoftDeletes, ValidatingTrait; - protected $fillable = ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted']; - protected $hidden = ['encrypted']; + + protected $fillable + = ['user_id', 'transaction_type_id', 'bill_id', 'transaction_currency_id', 'description', 'completed', 'date', 'encrypted', 'tag_count']; + protected $hidden = ['encrypted']; protected $rules - = [ + = [ 'user_id' => 'required|exists:users,id', 'transaction_type_id' => 'required|exists:transaction_types,id', 'bill_id' => 'exists:bills,id', @@ -154,7 +158,7 @@ class TransactionJournal extends Model $amount = $t->amount; } } - $count = $this->tags->count(); + $count = $this->tags()->count(); if ($count === 1) { // get amount for single tag: @@ -211,15 +215,6 @@ class TransactionJournal extends Model } - /** - * @codeCoverageIgnore - * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany - */ - public function tags() - { - return $this->belongsToMany('FireflyIII\Models\Tag'); - } - /** * @param string $amount * @@ -274,6 +269,29 @@ class TransactionJournal extends Model return ['created_at', 'updated_at', 'date', 'deleted_at']; } + /** + * Save the model to the database. + * + * @param array $options + * + * @return bool + */ + public function save(array $options = []) + { + $count = $this->tags()->count(); + $this->tag_count = $count; + parent::save($options); + } + + /** + * @codeCoverageIgnore + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany + */ + public function tags() + { + return $this->belongsToMany('FireflyIII\Models\Tag'); + } + /** * @codeCoverageIgnore * @@ -418,7 +436,7 @@ class TransactionJournal extends Model $query->with( ['transactions' => function (HasMany $q) { $q->orderBy('amount', 'ASC'); - }, 'transactiontype', 'transactioncurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill', 'budgets', 'categories'] + }, 'transactionType', 'transactionCurrency', 'budgets', 'categories', 'transactions.account.accounttype', 'bill'] ); } diff --git a/database/migrations/2015_06_14_093841_changes_for_v345.php b/database/migrations/2015_06_14_093841_changes_for_v345.php new file mode 100644 index 0000000000..43a1df7223 --- /dev/null +++ b/database/migrations/2015_06_14_093841_changes_for_v345.php @@ -0,0 +1,39 @@ +dropColumn('tag_count'); + } + ); + } + + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + // + Schema::table( + 'transaction_journals', function (Blueprint $table) { + $table->smallInteger('tag_count', false, true)->default(0); + } + ); + } +}