-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] ORM Entity Relation : unable to get defined table name from rel…
…ation definition.
- Loading branch information
Showing
5 changed files
with
106 additions
and
74 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/Core/Orm/Relations/Provider/Contract/BaseRelationInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Wepesi\Core\Orm\Relations\Provider\Contract; | ||
|
||
interface BaseRelationInterface | ||
{ | ||
|
||
} |