Merge branch 'master' of https://git.mcnd.ca/mcndave/ulmus
This commit is contained in:
commit
f14347f01d
9 files changed
+245
-131
No files matched your search
+79
-67
@@ -14,52 +14,23 @@ use Ulmus\Annotation\Property\Relation\{ Ignore as RelationIgnore };
|
||||
|
||||
trait EntityTrait {
|
||||
use EventTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
protected bool $entityStrictFieldsDeclaration = false;
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
protected array $entityDatasetUnmatchedFields = [];
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public array $entityLoadedDataset = [];
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public function __get(string $name)
|
||||
{
|
||||
$relation = new Repository\RelationBuilder($this);
|
||||
|
||||
if ( false !== $data = $relation->searchRelation($name) ) {
|
||||
return $this->$name = $data;
|
||||
}
|
||||
|
||||
throw new \Exception(sprintf("[%s] - Undefined variable: %s", static::class, $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public function __isset(string $name) : bool
|
||||
{
|
||||
#if ( null !== $relation = static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) {
|
||||
# return isset($this->{$relation->key});
|
||||
#}
|
||||
if ( $this->isLoaded() && static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return isset($this->$name);
|
||||
}
|
||||
|
||||
/**
|
||||
/**entityLoadedDataset
|
||||
* @Ignore
|
||||
*/
|
||||
public function entityFillFromDataset(iterable $dataset, bool $overwriteDataset = false) : self
|
||||
@@ -71,9 +42,7 @@ trait EntityTrait {
|
||||
foreach($dataset as $key => $value) {
|
||||
$field = $entityResolver->field(strtolower($key), EntityResolver::KEY_COLUMN_NAME, false) ?? null;
|
||||
|
||||
if ( $field === null ) {
|
||||
$field = $entityResolver->field(strtolower($key), EntityResolver::KEY_LC_ENTITY_NAME, false);
|
||||
}
|
||||
$field ??= $entityResolver->field(strtolower($key), EntityResolver::KEY_LC_ENTITY_NAME, false);
|
||||
|
||||
if ( $field === null ) {
|
||||
if ($this->entityStrictFieldsDeclaration ) {
|
||||
@@ -104,17 +73,22 @@ trait EntityTrait {
|
||||
}
|
||||
}
|
||||
elseif ( $field['type'] === 'bool' ) {
|
||||
$this->{$field['name']} = (bool) $value;
|
||||
$value = (bool) $value;
|
||||
}
|
||||
|
||||
$this->{$field['name']} = $value;
|
||||
}
|
||||
elseif ( ! $field['builtin'] ) {
|
||||
$this->{$field['name']} = Ulmus::instanciateObject($field['type'], [ $value ]);
|
||||
try {
|
||||
$this->{$field['name']} = Ulmus::instanciateObject($field['type'], [ $value ]);
|
||||
}
|
||||
catch(\Error $e) {
|
||||
throw new \Error(sprintf("%s for class '%s' on field '%s'", $e->getMessage(), get_class($this), $field['name']));
|
||||
}
|
||||
}
|
||||
|
||||
# Keeping original data to diff on UPDATE query
|
||||
if ( ! $loaded ) {
|
||||
if ( ! $loaded /* || $isLoadedDataset */ ) {
|
||||
#if ( $field !== null ) {
|
||||
# $annotation = $entityResolver->searchFieldAnnotation($field['name'], new Field() );
|
||||
# $this->entityLoadedDataset[$annotation ? $annotation->name : $field['name']] = $dataset; # <--------- THIS TO FIX !!!!!!
|
||||
@@ -143,7 +117,7 @@ trait EntityTrait {
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
@@ -151,7 +125,7 @@ trait EntityTrait {
|
||||
{
|
||||
return $this->entityFillFromDataset($dataset);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
@@ -160,53 +134,53 @@ trait EntityTrait {
|
||||
if ( $returnSource ) {
|
||||
return $this->entityLoadedDataset;
|
||||
}
|
||||
|
||||
|
||||
$dataset = [];
|
||||
|
||||
|
||||
$entityResolver = $this->resolveEntity();
|
||||
|
||||
|
||||
foreach($entityResolver->fieldList(Common\EntityResolver::KEY_ENTITY_NAME, true) as $key => $field) {
|
||||
$annotation = $entityResolver->searchFieldAnnotation($key, new Field() );
|
||||
|
||||
if ( isset($this->$key) ) {
|
||||
if ( isset($this->$key) ) {
|
||||
$realKey = $annotation->name ?? $key;
|
||||
|
||||
|
||||
switch (true) {
|
||||
case is_object($this->$key):
|
||||
$dataset[$realKey] = Ulmus::convertObject($this->$key);
|
||||
break;
|
||||
|
||||
case is_array($this->$key):
|
||||
break;
|
||||
|
||||
case is_array($this->$key):
|
||||
$dataset[$realKey] = Ulmus::encodeArray($this->$key);
|
||||
break;
|
||||
break;
|
||||
|
||||
case is_bool($this->$key):
|
||||
$dataset[$realKey] = (int) $this->$key;
|
||||
break;
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
$dataset[$realKey] = $this->$key;
|
||||
$dataset[$realKey] = $this->$key;
|
||||
}
|
||||
}
|
||||
elseif ( $field['nullable'] ) {
|
||||
$dataset[ $annotation->name ?? $key ] = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# @TODO Must fix recursive bug !
|
||||
if ($includeRelations) {
|
||||
foreach($entityResolver->properties as $name => $field){
|
||||
$relation = $entityResolver->searchFieldAnnotation($name, new Relation() );
|
||||
|
||||
|
||||
if ( $relation && isset($this->$name) && ($relation->entity ?? $relation->bridge) !== static::class ) {
|
||||
if ( null !== $value = $this->$name ?? null ) {
|
||||
if ( is_iterable($value) ) {
|
||||
$list = [];
|
||||
|
||||
|
||||
foreach($value as $entity) {
|
||||
$list[] = $entity->entityGetDataset(false);
|
||||
}
|
||||
|
||||
|
||||
$dataset[$name] = $list;
|
||||
}
|
||||
elseif ( is_object($value) ) {
|
||||
@@ -215,11 +189,11 @@ trait EntityTrait {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $dataset;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
@@ -227,7 +201,7 @@ trait EntityTrait {
|
||||
{
|
||||
return $this->entityGetDataset($includeRelations);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
@@ -235,7 +209,7 @@ trait EntityTrait {
|
||||
{
|
||||
return static::entityCollection([ $this ]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
@@ -244,16 +218,45 @@ trait EntityTrait {
|
||||
if ( null === $pkField = $this->resolveEntity()->getPrimaryKeyField($this) ) {
|
||||
throw new Exception\EntityPrimaryKeyUnknown(sprintf("Entity %s has no field containing attributes 'primary_key'", static::class));
|
||||
}
|
||||
|
||||
|
||||
$key = key($pkField);
|
||||
|
||||
|
||||
return isset($this->$key);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public function __sleep()
|
||||
public function __get(string $name)
|
||||
{
|
||||
$relation = new Repository\RelationBuilder($this);
|
||||
|
||||
if ( false !== $data = $relation->searchRelation($name) ) {
|
||||
return $this->$name = $data;
|
||||
}
|
||||
|
||||
throw new \Exception(sprintf("[%s] - Undefined variable: %s", static::class, $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public function __isset(string $name) : bool
|
||||
{
|
||||
#if ( null !== $relation = static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) {
|
||||
# return isset($this->{$relation->key});
|
||||
#}
|
||||
if ( $this->isLoaded() && static::resolveEntity()->searchFieldAnnotation($name, new Relation() ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return isset($this->$name);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public function __sleep()
|
||||
{
|
||||
return array_keys($this->resolveEntity()->fieldList());
|
||||
}
|
||||
@@ -279,9 +282,9 @@ trait EntityTrait {
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->entityGetDataset();
|
||||
return $this->entityGetDataset();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
@@ -309,6 +312,15 @@ trait EntityTrait {
|
||||
return $collection;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
public static function queryBuilder() : QueryBuilder
|
||||
{
|
||||
return Ulmus::queryBuilder(static::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user