Skip to content

Commit

Permalink
Support for loading PROPERY and MANY_TO_ONE fields in EntityUtility.
Browse files Browse the repository at this point in the history
  • Loading branch information
GregaMohorko committed Jan 15, 2019
1 parent 863e160 commit 15d9a75
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# BlueDB

A PHP MySQL library with Database First model that lets you create Entities with base properties and complex relationships (*One-To-Many*, *Many-To-One*, *Many-To-Many*, *Table Inheritance*, *Associative tables*), supports queries with expressions, JSON encoding/decoding and is simple to use.
A PHP MySQL ORM library with Database First model that lets you create Entities with base properties and complex relationships (*One-To-Many*, *Many-To-One*, *Many-To-Many*, *Table Inheritance*, *Associative tables*), supports queries with expressions, JSON encoding/decoding and is simple to use.

Check the [BlueDBClient.NET](https://github.com/GregaMohorko/BlueDBClient.NET) for a .NET client library to use on the .NET client side.

Expand Down Expand Up @@ -70,6 +70,6 @@ PHP version >= 5.5

Grega Mohorko ([www.mohorko.info](https://www.mohorko.info))

Copyright (c) 2018 Grega Mohorko
Copyright (c) 2019 Grega Mohorko

[Apache License 2.0](./LICENSE)
2 changes: 1 addition & 1 deletion src/BlueDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* Bootstrap file for BlueDB library.
*
* Version 1.2.4.0
* Version 1.2.5.0
*
* @project BlueDB
* @author Grega Mohorko <[email protected]>
Expand Down
16 changes: 16 additions & 0 deletions src/DataAccess/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ public static function rollbackTransaction()
}
}

/**
* Will begin or commit when the provided values differ from each other.
*
* @param bool $beginTransaction
* @param bool $commit
*/
public static function beginOrCommitTransaction($beginTransaction,$commit)
{
if($beginTransaction===true && $commit!==true){
self::beginTransaction();
}
if($beginTransaction!==true && $commit===true){
self::commitTransaction();
}
}

/**
* Use this function when selecting multiple rows.
*
Expand Down
66 changes: 40 additions & 26 deletions src/Utility/EntityUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ public static function createDTO($entity)
}

/**
* Loads the specified field of the provided entity and returns it. This function does not modify the provided entity. Field type must either be ONE_TO_MANY or MANY_TO_MANY.
* Loads the specified field of the provided entity and returns it. This function does not modify the provided entity.
*
* @param FieldEntity $entity
* @param string $field Must either be ONE_TO_MANY or MANY_TO_MANY.
* @param array $fieldsToLoad [optional] Specifies which fields to load.
* @param array $fieldsToIgnore [optional]
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions.
* @param string $field
* @param array $fieldsToLoad [optional] Specifies which fields to load. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @param array $fieldsToIgnore [optional] Specifies which fields to ignore. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @return array
*/
public static function loadFieldOf($entity,$field,$fieldsToLoad=null,$fieldsToIgnore=null,$additionalExpressions=null)
Expand All @@ -168,13 +168,13 @@ public static function loadFieldOf($entity,$field,$fieldsToLoad=null,$fieldsToIg
}

/**
* Loads the specified field of the provided entity and returns it. Use this function when you expect to get only one result. This function does not modify the provided entity. Field type must be ONE_TO_MANY.
* Loads the specified field of the provided entity and returns it. Use this function when you expect to get only one result. This function does not modify the provided entity. Field type must not be MANY_TO_MANY.
*
* @param FieldEntity $entity
* @param string $field Must be ONE_TO_MANY.
* @param array $fieldsToLoad [optional] Specifies which fields to load.
* @param array $fieldsToIgnore [optional]
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions.
* @param string $field Must not be MANY_TO_MANY.
* @param array $fieldsToLoad [optional] Specifies which fields to load. Only for ONE_TO_MANY fields.
* @param array $fieldsToIgnore [optional] Specifies which fields to ignore. Only for ONE_TO_MANY fields.
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions. Only for ONE_TO_MANY fields.
* @return FieldEntity
*/
public static function loadFieldOfSingle($entity,$field,$fieldsToLoad=null,$fieldsToIgnore=null,$additionalExpressions=null)
Expand All @@ -185,41 +185,41 @@ public static function loadFieldOfSingle($entity,$field,$fieldsToLoad=null,$fiel
}

/**
* Loads the specified field of the provided entity. Field type must either be ONE_TO_MANY or MANY_TO_MANY.
* Loads the specified field of the provided entity.
*
* @param FieldEntity $entity
* @param string $field Must either be ONE_TO_MANY or MANY_TO_MANY.
* @param array $fieldsToLoad [optional] Specifies which fields to load.
* @param array $fieldsToIgnore [optional]
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions.
* @param string $field
* @param array $fieldsToLoad [optional] Specifies which fields to load. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @param array $fieldsToIgnore [optional] Specifies which fields to ignore. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions. Only for ONE_TO_MANY or MANY_TO_MANY fields.
*/
public static function loadField($entity,$field,$fieldsToLoad=null,$fieldsToIgnore=null,$additionalExpressions=null)
{
self::loadFieldInternal($entity, $field, $fieldsToLoad, $fieldsToIgnore, $additionalExpressions, false);
}

/**
* Loads the specified field of the provided entity. Use this function when you expect to get only one result. Field type must be ONE_TO_MANY.
* Loads the specified field of the provided entity. Use this function when you expect to get only one result. Field type must not be MANY_TO_MANY.
*
* @param FieldEntity $entity
* @param string $field Must be ONE_TO_MANY.
* @param array $fieldsToLoad [optional] Specifies which fields to load.
* @param array $fieldsToIgnore [optional]
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions.
* @param string $field Must not be MANY_TO_MANY.
* @param array $fieldsToLoad [optional] Specifies which fields to load. Only for ONE_TO_MANY fields.
* @param array $fieldsToIgnore [optional] Specifies which fields to ignore. Only for ONE_TO_MANY fields.
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions. Only for ONE_TO_MANY fields.
*/
public static function loadFieldSingle($entity,$field,$fieldsToLoad=null,$fieldsToIgnore=null,$additionalExpressions=null)
{
self::loadFieldInternal($entity, $field, $fieldsToLoad, $fieldsToIgnore, $additionalExpressions, true);
}

/**
* Loads the specified field of the provided entity. Field type must either be ONE_TO_MANY or MANY_TO_MANY.
* Loads the specified field of the provided entity.
*
* @param FieldEntity $entity
* @param string $field Must either be ONE_TO_MANY or MANY_TO_MANY.
* @param array $fieldsToLoad [optional] Specifies which fields to load.
* @param array $fieldsToIgnore [optional]
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions.
* @param string $field
* @param array $fieldsToLoad [optional] Specifies which fields to load. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @param array $fieldsToIgnore [optional] Specifies which fields to ignore. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @param array $additionalExpressions [optional] Any additional expressions to filter the loaded entities. Can be a single expression or a list of expressions. Only for ONE_TO_MANY or MANY_TO_MANY fields.
* @param bool $expectsSingle
*/
private static function loadFieldInternal($entity,$field,$fieldsToLoad,$fieldsToIgnore,$additionalExpressions,$expectsSingle)
Expand All @@ -228,6 +228,20 @@ private static function loadFieldInternal($entity,$field,$fieldsToLoad,$fieldsTo
$fieldBaseConstName="$entityClass::$field";
$fieldType=constant($fieldBaseConstName."FieldType");
switch($fieldType){
case FieldTypeEnum::PROPERTY:
case FieldTypeEnum::MANY_TO_ONE:
if($additionalExpressions!==null){
throw new Exception("Additional expressions are not allowed for PROPERTY or MANY_TO_ONE field type.");
}
if($fieldsToLoad!==null){
throw new Exception("Fields to load are not allowed for PROPERTY or MANY_TO_ONE field type.");
}
if($fieldsToIgnore!==null){
throw new Exception("Fields to ignore are not allowed for PROPERTY or MANY_TO_ONE field type.");
}
$entityWithLoadedField=$entityClass::loadByID($entity->getID(),[$field]);
$loadedField=$entityWithLoadedField->$field;
break;
case FieldTypeEnum::ONE_TO_MANY:
$fieldClass=constant($fieldBaseConstName."Class");
$fieldIdentifier=constant($fieldBaseConstName."Identifier");
Expand Down Expand Up @@ -276,7 +290,7 @@ public static function loadFields($entity,$fields)
$entityClass= get_class($entity);

/* @var $entityWithLoadedFields FieldEntity */
$entityWithLoadedFields=$entityClass::loadByID($entity->getID(),$fields);
$entityWithLoadedFields=$entityClass::loadByID($entity->getID(),$fields,null,true,true,true);

foreach($fields as $field){
$entity->$field=$entityWithLoadedFields->$field;
Expand Down

0 comments on commit 15d9a75

Please sign in to comment.