mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Expand models.
This commit is contained in:
parent
ac765b7e4c
commit
6ff618e388
@ -1,22 +1,21 @@
|
|||||||
<?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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Account
|
* Account
|
||||||
*
|
*
|
||||||
* @property integer $id
|
* @property integer $id
|
||||||
* @property \Carbon\Carbon $created_at
|
* @property \Carbon\Carbon $created_at
|
||||||
* @property \Carbon\Carbon $updated_at
|
* @property \Carbon\Carbon $updated_at
|
||||||
* @property integer $user_id
|
* @property integer $user_id
|
||||||
* @property integer $account_type_id
|
* @property integer $account_type_id
|
||||||
* @property string $name
|
* @property string $name
|
||||||
* @property boolean $active
|
* @property boolean $active
|
||||||
* @property-read \AccountType $accountType
|
* @property-read \AccountType $accountType
|
||||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Transaction[] $transactions
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Transaction[] $transactions
|
||||||
* @property-read \Illuminate\Database\Eloquent\Collection|\Piggybank[] $piggybanks
|
* @property-read \Illuminate\Database\Eloquent\Collection|\Piggybank[] $piggybanks
|
||||||
* @property-read \User $user
|
* @property-read \User $user
|
||||||
* @method static \Illuminate\Database\Query\Builder|\Account whereId($value)
|
* @method static \Illuminate\Database\Query\Builder|\Account whereId($value)
|
||||||
* @method static \Illuminate\Database\Query\Builder|\Account whereCreatedAt($value)
|
* @method static \Illuminate\Database\Query\Builder|\Account whereCreatedAt($value)
|
||||||
* @method static \Illuminate\Database\Query\Builder|\Account whereUpdatedAt($value)
|
* @method static \Illuminate\Database\Query\Builder|\Account whereUpdatedAt($value)
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user