- WIP on Ulmus -- new use case showed up, modification were needed.
This commit is contained in:
parent
1b051f923e
commit
b589ba3c2b
|
@ -132,7 +132,6 @@ trait ArrayObjectTrait {
|
|||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
if ( !is_null($this->arrayobject_pointer) ) {
|
||||
|
|
|
@ -20,6 +20,8 @@ class EntityResolver {
|
|||
public array $properties;
|
||||
|
||||
public array $methods;
|
||||
|
||||
protected array $fieldList = [];
|
||||
|
||||
public function __construct(string $entityClass)
|
||||
{
|
||||
|
@ -28,18 +30,22 @@ class EntityResolver {
|
|||
list($this->uses, $this->class, $this->methods, $this->properties) = array_values(
|
||||
ObjectReflection::fromClass($entityClass)->read()
|
||||
);
|
||||
|
||||
|
||||
$this->resolveAnnotations();
|
||||
}
|
||||
|
||||
public function field($name, $fieldKey = self::KEY_ENTITY_NAME) : ? array
|
||||
public function field($name, $fieldKey = self::KEY_ENTITY_NAME, $throwException = true) : ? array
|
||||
{
|
||||
try{
|
||||
return $this->fieldList($fieldKey)[$name];
|
||||
}
|
||||
catch(\Throwable $e) {
|
||||
throw new \InvalidArgumentException("Can't find entity field's column named `$name` from entity {$this->entityClass}");
|
||||
if ( $throwException) {
|
||||
throw new \InvalidArgumentException("Can't find entity field's column named `$name` from entity {$this->entityClass}");
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function fieldList($fieldKey = self::KEY_ENTITY_NAME) : array
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Ulmus;
|
||||
|
||||
class EntityCollection extends \ArrayObject
|
||||
{
|
||||
use Common\ArrayObjectTrait;
|
||||
#class EntityCollection implements \Countable, \ArrayAccess
|
||||
class EntityCollection extends \ArrayObject {
|
||||
# use Common\ArrayObjectTrait;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,10 @@ use Ulmus\Annotation\Property\{ Field, Relation, OrderBy, Where, };
|
|||
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, };
|
||||
|
||||
trait EntityTrait {
|
||||
|
||||
protected bool $strictEntityFieldsDeclaration = true;
|
||||
|
||||
protected array $unmatchedEntityDatasetFields = [];
|
||||
|
||||
/**
|
||||
* @Ignore
|
||||
|
@ -20,11 +24,17 @@ trait EntityTrait {
|
|||
$fields = Ulmus::resolveEntity(static::class);
|
||||
|
||||
foreach($dataset as $key => $value) {
|
||||
if ( null === $field = $fields->field($key, EntityResolver::KEY_COLUMN_NAME) ?? null ) {
|
||||
throw new \Exception("Field `$key` can not be found within your entity ".static::class);
|
||||
$key = strtolower($key);
|
||||
|
||||
if ( null === $field = $fields->field($key, EntityResolver::KEY_COLUMN_NAME, false) ?? null ) {
|
||||
if ($this->strictEntityFieldsDeclaration ) {
|
||||
throw new \Exception("Field `$key` can not be found within your entity ".static::class);
|
||||
}
|
||||
else {
|
||||
$this->unmatchedEntityDatasetFields[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_null($value) ) {
|
||||
elseif ( is_null($value) ) {
|
||||
$this->{$field['name']} = null;
|
||||
}
|
||||
elseif ( $field['type'] === 'array' ) {
|
||||
|
|
|
@ -36,9 +36,9 @@ class Repository
|
|||
return $this->collectionFromQuery();
|
||||
}
|
||||
|
||||
public function loadFromPk($value) : EntityCollection
|
||||
public function loadFromPk($value, $primaryKey = "id") : EntityCollection
|
||||
{
|
||||
return $this->where('id', $value)->loadOne();
|
||||
return $this->where($primaryKey, $value)->loadOne();
|
||||
}
|
||||
|
||||
public function loadFromField($field, $value) : EntityCollection
|
||||
|
|
Loading…
Reference in New Issue