mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
All kinds of new stuff: finished most of the importing routines, extended the database (single table inheritance) and expanded some other stuff.
This commit is contained in:
78
app/lib/Firefly/Database/SingleTableInheritanceEntity.php
Normal file
78
app/lib/Firefly/Database/SingleTableInheritanceEntity.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Firefly\Database;
|
||||
|
||||
|
||||
abstract class SingleTableInheritanceEntity extends \Elegant
|
||||
{
|
||||
/**
|
||||
* The field that stores the subclass
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $subclassField = null;
|
||||
/**
|
||||
* must be overridden and set to true in subclasses
|
||||
* @var bool
|
||||
*/
|
||||
protected $isSubclass = false;
|
||||
|
||||
public function newFromBuilder($attributes = array())
|
||||
{
|
||||
$instance = $this->mapData((array)$attributes)->newInstance(array(), true);
|
||||
$instance->setRawAttributes((array)$attributes, true);
|
||||
return $instance;
|
||||
}
|
||||
|
||||
// if no subclass is defined, function as normal
|
||||
|
||||
public function mapData(array $attributes)
|
||||
{
|
||||
if (!$this->subclassField) {
|
||||
return $this->newInstance();
|
||||
}
|
||||
|
||||
return new $attributes[$this->subclassField];
|
||||
}
|
||||
|
||||
// instead of using $this->newInstance(), call
|
||||
// newInstance() on the object from mapData
|
||||
|
||||
public function newQuery($excludeDeleted = true)
|
||||
{
|
||||
// If using Laravel 4.0.x then use the following commented version of this command
|
||||
// $builder = new Builder($this->newBaseQueryBuilder());
|
||||
// newEloquentBuilder() was added in 4.1
|
||||
$builder = $this->newEloquentBuilder($this->newBaseQueryBuilder());
|
||||
|
||||
// Once we have the query builders, we will set the model instances so the
|
||||
// builder can easily access any information it may need from the model
|
||||
// while it is constructing and executing various queries against it.
|
||||
$builder->setModel($this)->with($this->with);
|
||||
|
||||
if ($excludeDeleted && $this->softDelete) {
|
||||
$builder->whereNull($this->getQualifiedDeletedAtColumn());
|
||||
}
|
||||
|
||||
if ($this->subclassField && $this->isSubclass()) {
|
||||
$builder->where($this->subclassField, '=', get_class($this));
|
||||
}
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public function isSubclass()
|
||||
{
|
||||
return $this->isSubclass;
|
||||
}
|
||||
|
||||
// ensure that the subclass field is assigned on save
|
||||
|
||||
public function save(array $options = array())
|
||||
{
|
||||
if ($this->subclassField) {
|
||||
$this->attributes[$this->subclassField] = get_class($this);
|
||||
}
|
||||
return parent::save($options);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user