Expand models.

This commit is contained in:
James Cole 2014-12-05 21:39:34 +01:00
parent ac765b7e4c
commit 6ff618e388
2 changed files with 88 additions and 19 deletions

View File

@ -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');
} }
/** /**

View File

@ -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);
}
} }