- WIP on insert/update statement
This commit is contained in:
1 parent
bad5123447
commit
828daffec8
13 files changed
+311
-36
No files matched your search
+38
-5
@@ -6,7 +6,7 @@ use Ulmus\Repository,
|
||||
Ulmus\Common\EntityResolver,
|
||||
Ulmus\Common\EntityField;
|
||||
|
||||
use Ulmus\Annotation\Classes\{ Method, Table, Collation as Test, };
|
||||
use Ulmus\Annotation\Classes\{ Method, Table, Collation, };
|
||||
use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, };
|
||||
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
|
||||
|
||||
@@ -53,7 +53,7 @@ trait EntityTrait {
|
||||
|
||||
switch($relation->type) {
|
||||
case 'oneToMany':
|
||||
$repository->where( $baseEntity->field($relation->foreignKey), 2166); # <<<<<<<<< CHANGE $THIS->ID WITH PROPER NOMENCLATURE
|
||||
$repository->where( $baseEntity->field($relation->foreignKey), $this->$field); # <<<<<<<<< CHANGE $THIS->ID WITH PROPER NOMENCLATURE
|
||||
|
||||
return $this->$name = $repository->loadAll();
|
||||
|
||||
@@ -71,19 +71,25 @@ trait EntityTrait {
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw new \Exception(sprintf("[%s] - Undefined variable: %s", static::class, $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public function entityFillFromDataset($dataset) : self
|
||||
public function entityFillFromDataset(iterable $dataset) : self
|
||||
{
|
||||
$fields = $this->resolveEntity();
|
||||
|
||||
foreach($dataset as $key => $value) {
|
||||
$key = strtolower($key);
|
||||
$field = $fields->field(strtolower($key), EntityResolver::KEY_COLUMN_NAME, false) ?? null;
|
||||
|
||||
if ( null === $field = $fields->field($key, EntityResolver::KEY_COLUMN_NAME, false) ?? null ) {
|
||||
if ( $field === null ) {
|
||||
$field = $fields->field($key, EntityResolver::KEY_ENTITY_NAME, false);
|
||||
}
|
||||
|
||||
if ( $field === null ) {
|
||||
if ($this->strictEntityFieldsDeclaration ) {
|
||||
throw new \Exception("Field `$key` can not be found within your entity ".static::class);
|
||||
}
|
||||
@@ -108,6 +114,11 @@ trait EntityTrait {
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fromArray(iterable $dataset) : self
|
||||
{
|
||||
return $this->entityFillFromDataset($dataset);
|
||||
}
|
||||
|
||||
public function entityGetDataset() : array
|
||||
{
|
||||
$fields = array_keys($this->resolveEntity()->fieldList());
|
||||
@@ -117,6 +128,28 @@ trait EntityTrait {
|
||||
}, $fields));
|
||||
}
|
||||
|
||||
public function toArray() : array
|
||||
{
|
||||
return $this->entityGetDataset();
|
||||
}
|
||||
|
||||
public function toCollection() : array
|
||||
{
|
||||
return new EntityCollection($this->toArray());
|
||||
}
|
||||
|
||||
public function isLoaded() : bool
|
||||
{
|
||||
if ( null === $pkField = $this->resolveEntity()->getPrimaryKeyField($this) ) {
|
||||
throw new Exception\EntityPrimaryKeyUnknown("Entity has no field containing attributes 'primary_key'");
|
||||
}
|
||||
|
||||
$key = key($pkField);
|
||||
|
||||
return isset($this->$key);
|
||||
}
|
||||
|
||||
|
||||
public function __sleep()
|
||||
{
|
||||
return array_keys($this->resolveEntity()->fieldList());
|
||||
|
||||
Reference in new issue
Block a user