Skip to content

Commit

Permalink
[FIX] ORM Entity Relation : unable to get defined table name from rel…
Browse files Browse the repository at this point in the history
…ation definition.

[FIX] ORM Entity Relation : unable to get defined table name from relation definition.
  • Loading branch information
bim-g authored Sep 29, 2023
2 parents 9d4ac51 + 6773526 commit cf2690a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 74 deletions.
64 changes: 0 additions & 64 deletions src/Core/Orm/EntityModel/EntityReflexion.php

This file was deleted.

76 changes: 76 additions & 0 deletions src/Core/Orm/EntityModel/EntityReflexionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Wepesi\Core\Orm\EntityModel;

use Exception;
use ReflectionClass;
use ReflectionProperty;
use Wepesi\Core\Orm\EntityModel\Provider\Contract\EntityInterface;
use Wepesi\Core\Orm\Relations\Provider\Contract\BaseRelationInterface;

/**
*
*/
trait EntityReflexionTrait
{
/**
* @param $entity EntityInterface|BaseRelationInterface
* @param bool $entity_relation default false, use true in case we want to get more information about entity relation
* @param bool $get_only_entity_name default false, use True in cas ww want to get only entity name or object.
* @return array|object
*/
private function getClassDefinition($entity, bool $entity_relation = false, bool $get_only_entity_name = false)
{
try {
$reflexion = new ReflectionClass($entity);
$classEntityName = $reflexion->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()];
}
}
}
4 changes: 2 additions & 2 deletions src/Core/Orm/EntityModel/Provider/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,7 +26,7 @@ abstract class Entity implements EntityInterface
* @var array|mixed
*/
private array $param;
use EntityReflexion;
use EntityReflexionTrait;

/**
*
Expand Down
28 changes: 20 additions & 8 deletions src/Core/Orm/Relations/BaseRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,29 +24,32 @@ abstract class BaseRelation
* @var array
*/
protected array $table_relations;
use EntityReflexion;
use EntityReflexionTrait;

/**
* @param EntityInterface $entity_parent
* @param EntityInterface $entity_child
*/
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
{
$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
];
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Wepesi\Core\Orm\Relations\Provider\Contract;

interface BaseRelationInterface
{

}

0 comments on commit cf2690a

Please sign in to comment.