diff --git a/src/Core/Orm/EntityModel/EntityReflexion.php b/src/Core/Orm/EntityModel/EntityReflexion.php deleted file mode 100644 index 24d04ef..0000000 --- a/src/Core/Orm/EntityModel/EntityReflexion.php +++ /dev/null @@ -1,64 +0,0 @@ -getShortName(); - $table_name = lcfirst($classEntity); - $table_field = []; - $entity_object = null; - $method = null; - foreach ($reflexion->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) { - $propertyName = $property->getName(); - $table_field[] = $propertyName; - } - if($entity_relation){ - $object = $reflexion->newInstance(); - $class_name = lcfirst($classEntity); - $method = $reflexion->getMethod('getTableName'); - $method->setAccessible(true); - $get_table_name = $method->invoke($object); - // - $this_reflexion = new \ReflectionClass($this); - $object = $this_reflexion->newInstance(); - if ($this_reflexion->hasMethod($get_table_name)){ - $method = $this_reflexion->getMethod($get_table_name); - $method->setAccessible(true); - } else if ($this_reflexion->hasMethod(($class_name))){ - $method = $this_reflexion->getMethod($class_name); - $method->setAccessible(true); - }else{ - throw new \Exception('You should implement a relation method call ' . $class_name .' from class '. $classEntity); - } - $entity_object = $method->invoke($object); - }elseif ($entity_name){ - $object = $reflexion->newInstance(); - $method = $reflexion->getMethod('getTableName'); - $method->setAccessible(true); - $table_name = $method->invoke($object); - } - return (object)[ - 'table' => $table_name, - 'fields' => $table_field, - 'entity_object' => $entity_object, - 'class' => $classEntity, - ]; - } catch (\Exception $ex) { - return ['exception' => $ex->getMessage()]; - } - } -} diff --git a/src/Core/Orm/EntityModel/EntityReflexionTrait.php b/src/Core/Orm/EntityModel/EntityReflexionTrait.php new file mode 100644 index 0000000..e581c5e --- /dev/null +++ b/src/Core/Orm/EntityModel/EntityReflexionTrait.php @@ -0,0 +1,76 @@ +getShortName(); + $table_name = strtolower($classEntityName); + $table_field = []; + $entity_object = null; + // get entity table field defined as public properties + foreach ($reflexion->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { + $table_field[] = $property->getName(); + } + + /** + * check in case we want to get more information about the entity relation. + */ + if ($entity_relation) { + $class_name = lcfirst($classEntityName); + //get table name + $entity_instance_object = $reflexion->newInstance(); + $getTableNameMethod = $reflexion->getMethod('getTableName'); + $getTableNameMethod->setAccessible(true); + $table_name = $getTableNameMethod->invoke($entity_instance_object); + // + $parentEntityReflexion = new ReflectionClass($this); + $parent_entity_instance_object = $parentEntityReflexion->newInstance(); + // check from parent entity if the child entity name as method as been defined. + if ($parentEntityReflexion->hasMethod($table_name)) { + $method = $parent_entity_instance_object->getMethod($table_name); + $method->setAccessible(true); + $entity_object = $method->invoke($parent_entity_instance_object); + } else if ($parentEntityReflexion->hasMethod($class_name)) { + $method = $parentEntityReflexion->getMethod($class_name); + $method->setAccessible(true); + $entity_object = $method->invoke($parent_entity_instance_object); + } else { + throw new Exception('You should implement a relation method call ' . $class_name . ' from class ' . $classEntityName); + } + } elseif ($get_only_entity_name) { + $instance_object = $reflexion->newInstance(); + $get_table_name_method = $reflexion->getMethod('getTableName'); + $get_table_name_method->setAccessible(true); + $table_name = $get_table_name_method->invoke($instance_object); + } + return (object)[ + 'table' => $table_name, + 'fields' => $table_field, + 'entity_object' => $entity_object, + 'class' => $classEntityName, + ]; + } catch (Exception $ex) { + return ['exception' => $ex->getMessage()]; + } + } +} diff --git a/src/Core/Orm/EntityModel/Provider/Entity.php b/src/Core/Orm/EntityModel/Provider/Entity.php index 3fc44f5..4ba1745 100644 --- a/src/Core/Orm/EntityModel/Provider/Entity.php +++ b/src/Core/Orm/EntityModel/Provider/Entity.php @@ -2,7 +2,7 @@ namespace Wepesi\Core\Orm\EntityModel\Provider; -use Wepesi\Core\Orm\EntityModel\EntityReflexion; +use Wepesi\Core\Orm\EntityModel\EntityReflexionTrait; use Wepesi\Core\Orm\EntityModel\Provider\Contract\EntityInterface; use Wepesi\Core\Orm\DB; use Wepesi\Core\Orm\Relations\HasMany; @@ -26,7 +26,7 @@ abstract class Entity implements EntityInterface * @var array|mixed */ private array $param; - use EntityReflexion; + use EntityReflexionTrait; /** * diff --git a/src/Core/Orm/Relations/BaseRelation.php b/src/Core/Orm/Relations/BaseRelation.php index 7b628d0..a089ed0 100644 --- a/src/Core/Orm/Relations/BaseRelation.php +++ b/src/Core/Orm/Relations/BaseRelation.php @@ -2,12 +2,15 @@ namespace Wepesi\Core\Orm\Relations; -use Wepesi\Core\Orm\EntityModel\EntityReflexion; +use ReflectionClass; +use Wepesi\Core\Orm\EntityModel\EntityReflexionTrait; use Wepesi\Core\Orm\EntityModel\Provider\Contract\EntityInterface; +use Wepesi\Core\Orm\Relations\Provider\Contract\BaseRelationInterface; + /** * */ -abstract class BaseRelation +abstract class BaseRelation implements BaseRelationInterface { /** * @var string|mixed @@ -21,7 +24,7 @@ abstract class BaseRelation * @var array */ protected array $table_relations; - use EntityReflexion; + use EntityReflexionTrait; /** * @param EntityInterface $entity_parent @@ -29,13 +32,16 @@ abstract class BaseRelation */ public function __construct(EntityInterface $entity_parent, EntityInterface $entity_child) { - $this->parent_table = $this->getClassDefinition($entity_parent,false,true)->table; - $this->child_table = $this->getClassDefinition($entity_child,false,true)->table; + $this->parent_table = $this->getClassDefinition($entity_parent, false, true)->table; + $this->child_table = $this->getClassDefinition($entity_child, false, true)->table; $this->table_relations = []; } + /** - * @param string $reference_key - * @param string $foreignKey + * create link relation between to two entity. + * + * @param string $reference_key The parent entity relation id + * @param string $foreignKey the child entity relation id * @return BaseRelation */ public function linkOn(string $reference_key, string $foreignKey): BaseRelation @@ -43,7 +49,7 @@ public function linkOn(string $reference_key, string $foreignKey): BaseRelation $this->table_relations = [ 'parent' => $this->parent_table, 'child' => $this->child_table, - 'type' => $this->getClassDefinition($this)->class, + 'type' => (new ReflectionClass($this))->getShortName(), 'primary_key' => $reference_key, 'foreign_key' => $foreignKey ]; @@ -63,6 +69,12 @@ public function __call($method, $params) } /** + * Get relation type information about two entities + * parent the parent entity table, + * child the child entity table, + * type the entity relation type (hasOne, hasMany,...), + * primary_key entity primary key references not for table, + * foreign_key entity primary key references * @return object */ protected function getRelation(): object diff --git a/src/Core/Orm/Relations/Provider/Contract/BaseRelationInterface.php b/src/Core/Orm/Relations/Provider/Contract/BaseRelationInterface.php new file mode 100644 index 0000000..df18a2a --- /dev/null +++ b/src/Core/Orm/Relations/Provider/Contract/BaseRelationInterface.php @@ -0,0 +1,8 @@ +