mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-13 07:53:16 +00:00
Expand models.
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
<?php
|
<?php
|
||||||
use Carbon\Carbon;
|
|
||||||
use LaravelBook\Ardent\Ardent as Ardent;
|
use LaravelBook\Ardent\Ardent as Ardent;
|
||||||
use LaravelBook\Ardent\Builder;
|
use LaravelBook\Ardent\Builder;
|
||||||
|
|
||||||
@@ -56,13 +55,20 @@ class Account extends Ardent
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transactions.
|
* @param $fieldName
|
||||||
*
|
*
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function transactions()
|
public function getMeta($fieldName)
|
||||||
{
|
{
|
||||||
return $this->hasMany('Transaction');
|
foreach ($this->accountMeta as $meta) {
|
||||||
|
if ($meta->name == $fieldName) {
|
||||||
|
return $meta->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -87,12 +93,57 @@ class Account extends Ardent
|
|||||||
$query->whereIn('account_types.type', $types);
|
$query->whereIn('account_types.type', $types);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param Builder $query
|
||||||
|
*/
|
||||||
|
public function scopeWithMeta(Builder $query)
|
||||||
|
{
|
||||||
|
$query->with(['accountmeta']);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
|
||||||
*/
|
*/
|
||||||
public function transactionjournals()
|
public function transactionjournals()
|
||||||
{
|
{
|
||||||
return $this->hasManyThrough('TransactionJournal', 'Transaction','transaction_journal_id','id');
|
return $this->hasManyThrough('TransactionJournal', 'Transaction', 'transaction_journal_id', 'id');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transactions.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
||||||
|
*/
|
||||||
|
public function transactions()
|
||||||
|
{
|
||||||
|
return $this->hasMany('Transaction');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function updateMeta($fieldName, $fieldValue)
|
||||||
|
{
|
||||||
|
$meta = $this->accountMeta()->get();
|
||||||
|
/** @var AccountMeta $entry */
|
||||||
|
foreach ($meta as $entry) {
|
||||||
|
if ($entry->name == $fieldName) {
|
||||||
|
$entry->data = $fieldValue;
|
||||||
|
$entry->save();
|
||||||
|
|
||||||
|
return $entry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$meta = new AccountMeta;
|
||||||
|
$meta->account()->associate($this);
|
||||||
|
$meta->name = $fieldName;
|
||||||
|
$meta->data = $fieldValue;
|
||||||
|
$meta->save();
|
||||||
|
|
||||||
|
return $meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function accountMeta()
|
||||||
|
{
|
||||||
|
return $this->hasMany('AccountMeta');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -16,13 +16,13 @@ class AccountMeta extends Ardent
|
|||||||
= [
|
= [
|
||||||
'account_id' => 'numeric|required|exists:accounts,id',
|
'account_id' => 'numeric|required|exists:accounts,id',
|
||||||
'name' => 'required|between:1,250',
|
'name' => 'required|between:1,250',
|
||||||
'data' => 'required'];
|
'data' => 'required'
|
||||||
|
];
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $fillable = ['account_id', 'name', 'date'];
|
protected $fillable = ['account_id', 'name', 'date'];
|
||||||
|
protected $table = 'account_meta';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
||||||
@@ -32,4 +32,22 @@ class AccountMeta extends Ardent
|
|||||||
return $this->belongsTo('Account');
|
return $this->belongsTo('Account');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $value
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getDataAttribute($value)
|
||||||
|
{
|
||||||
|
return json_decode($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $value
|
||||||
|
*/
|
||||||
|
public function setDataAttribute($value)
|
||||||
|
{
|
||||||
|
$this->attributes['data'] = json_encode($value);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user