Skip to content

Commit

Permalink
Merge branch 'vpg-issue_11999' into 4.0.x
Browse files Browse the repository at this point in the history
* vpg-issue_11999:
  Doc Add the ability to use FrontendInterface to serialize Model and ResultSet
  [NFR] Add FrontendInterface to serialize Model and Resultset
  • Loading branch information
niden committed Nov 29, 2018
2 parents 3eb50b3 + 8ef4730 commit a0da80b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Added `Phalcon\Tag::renderTitle()` that renders the title enclosed in `<title>` tags. [#13547](https://github.com/phalcon/cphalcon/issues/13547)
- Added `hasHeader()` method to `Phalcon\Http\Response` to provide the ability to check if a header exists. [PR-12189](https://github.com/phalcon/cphalcon/pull/12189)
- Added global setting `orm.case_insensitive_column_map` to attempt to find value in the column map case-insensitively. Can be also enabled by setting `caseInsensitiveColumnMap` key in `\Phalcon\Mvc\Model::setup()`. [#11802](https://github.com/phalcon/cphalcon/pull/11802)
- Added the ability to use FrontendInterface to serialize Model and ResultSet - Inject a `serializer` object which implements `FrontendInterface` in DI to use it. [#12808] (https://github.com/phalcon/cphalcon/pull/12888)
- Added `Phalcon\Mvc\Model\Query\BuilderInterface::offset` [#13599](https://github.com/phalcon/cphalcon/pull/13599)
- Added `Phalcon\Http\Response\Cookies::getCookies` [#13591](https://github.com/phalcon/cphalcon/pull/13591)
- Added `Phalcon\Mvc\Model::isRelationshipLoaded` to check if relationship is loaded
Expand Down
52 changes: 36 additions & 16 deletions phalcon/mvc/collection.zep
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use Phalcon\Mvc\Collection\Exception;
use Phalcon\Mvc\Collection\ManagerInterface;
use Phalcon\Messages\Message as Message;
use Phalcon\ValidationInterface;
use Phalcon\Cache\FrontendInterface;


/**
* Phalcon\Mvc\Collection
Expand Down Expand Up @@ -1594,6 +1596,21 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
*/
public function serialize() -> string
{
var dependencyInjector, serializer;

/**
* Obtain the default DI
*/
let dependencyInjector = Di::getDefault();
if typeof dependencyInjector != "object" {
throw new Exception("The dependency injector container is not valid");
}

if dependencyInjector->has("serializer") {
let serializer = <FrontendInterface> this->_dependencyInjector->getShared("serializer");
return serializer->beforeStore(this->toArray());
}

/**
* Use the standard serialize function to serialize the array data
*/
Expand All @@ -1605,24 +1622,27 @@ abstract class Collection implements EntityInterface, CollectionInterface, Injec
*/
public function unserialize(string! data)
{
var attributes, dependencyInjector, manager, key, value;

let attributes = unserialize(data);
if typeof attributes == "array" {

/**
* Obtain the default DI
*/
let dependencyInjector = Di::getDefault();
if typeof dependencyInjector != "object" {
throw new Exception("A dependency injector container is required to obtain the services related to the ODM");
}
var attributes, dependencyInjector, manager, key, value, serializer;

/**
* Update the dependency injector
*/
let this->_dependencyInjector = dependencyInjector;
/**
* Obtain the default DI
*/
let dependencyInjector = Di::getDefault();
if typeof dependencyInjector != "object" {
throw new Exception("A dependency injector container is required to obtain the services related to the ORM");
}

/**
* Update the dependency injector
*/
let this->_dependencyInjector = dependencyInjector;
if dependencyInjector->has("serializer") {
let serializer = <FrontendInterface> dependencyInjector->getShared("serializer");
let attributes = serializer->afterRetrieve(data);
} else {
let attributes = unserialize(data);
}
if typeof attributes == "array" {
/**
* Gets the default modelsManager service
*/
Expand Down
2 changes: 2 additions & 0 deletions phalcon/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ use Phalcon\Mvc\Model\TransactionInterface;
use Phalcon\Mvc\Model\ValidationFailed;
use Phalcon\Mvc\ModelInterface;
use Phalcon\ValidationInterface;
use Phalcon\Events\ManagerInterface as EventsManagerInterface;
use Phalcon\Cache\FrontendInterface;

/**
* Phalcon\Mvc\Model
Expand Down
38 changes: 29 additions & 9 deletions phalcon/mvc/model/resultset/complex.zep
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ use Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Model\Exception;
use Phalcon\Cache\BackendInterface;
use Phalcon\Mvc\Model\ResultsetInterface;
use Phalcon\DiInterface;
use Phalcon\Di;
use Phalcon\Cache\FrontendInterface;

/**
* Phalcon\Mvc\Model\Resultset\Complex
Expand Down Expand Up @@ -270,8 +273,7 @@ class Complex extends Resultset implements ResultsetInterface
*/
public function serialize() -> string
{
var records, cache, columnTypes, hydrateMode, serialized;

var records, cache, columnTypes, hydrateMode, dependencyInjector, serializer;
/**
* Obtain the records as an array
*/
Expand All @@ -280,30 +282,48 @@ class Complex extends Resultset implements ResultsetInterface
let cache = this->_cache,
columnTypes = this->_columnTypes,
hydrateMode = this->_hydrateMode;
let dependencyInjector = Di::getDefault();
if typeof dependencyInjector != "object" {
throw new Exception("The dependency injector container is not valid");
}
if dependencyInjector->has("serializer") {
let serializer = <FrontendInterface> dependencyInjector->getShared("serializer");
return serializer->beforeStore([
"cache" : cache,
"rows" : records,
"columnTypes" : columnTypes,
"hydrateMode" : hydrateMode
]);
}

let serialized = serialize([
return serialize([
"cache" : cache,
"rows" : records,
"columnTypes" : columnTypes,
"hydrateMode" : hydrateMode
]);

return serialized;
}

/**
* Unserializing a resultset will allow to only works on the rows present in the saved state
*/
public function unserialize(string! data) -> void
{
var resultset;

var resultset, dependencyInjector, serializer;
/**
* Rows are already hydrated
*/
let this->_disableHydration = true;

let resultset = unserialize(data);
let dependencyInjector = Di::getDefault();
if typeof dependencyInjector != "object" {
throw new Exception("The dependency injector container is not valid");
}
if dependencyInjector->has("serializer") {
let serializer = <FrontendInterface> dependencyInjector->getShared("serializer");
let resultset = serializer->afterRetrieve(data);
} else {
let resultset = unserialize(data);
}
if typeof resultset != "array" {
throw new Exception("Invalid serialization data");
}
Expand Down
34 changes: 31 additions & 3 deletions phalcon/mvc/model/resultset/simple.zep
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use Phalcon\Mvc\Model;
use Phalcon\Mvc\Model\Resultset;
use Phalcon\Mvc\Model\Exception;
use Phalcon\Cache\BackendInterface;
use Phalcon\DiInterface;
use Phalcon\Di;
use Phalcon\Cache\FrontendInterface;


/**
* Phalcon\Mvc\Model\Resultset\Simple
Expand Down Expand Up @@ -224,6 +228,22 @@ class Simple extends Resultset
*/
public function serialize() -> string
{
var dependencyInjector, serializer;
let dependencyInjector = Di::getDefault();
if typeof dependencyInjector != "object" {
throw new Exception("The dependency injector container is not valid");
}
if dependencyInjector->has("serializer") {
let serializer = <FrontendInterface> dependencyInjector->getShared("serializer");
return serializer->beforeStore([
"model" : this->_model,
"cache" : this->_cache,
"rows" : this->toArray(false),
"columnMap" : this->_columnMap,
"hydrateMode" : this->_hydrateMode,
"keepSnapshots" : this->_keepSnapshots
]);
}
/**
* Serialize the cache using the serialize function
*/
Expand All @@ -242,9 +262,17 @@ class Simple extends Resultset
*/
public function unserialize(string! data) -> void
{
var resultset, keepSnapshots;

let resultset = unserialize(data);
var resultset, keepSnapshots, dependencyInjector, serializer;
let dependencyInjector = Di::getDefault();
if typeof dependencyInjector != "object" {
throw new Exception("The dependency injector container is not valid");
}
if dependencyInjector->has("serializer") {
let serializer = <FrontendInterface> dependencyInjector->getShared("serializer");
let resultset = serializer->afterRetrieve(data);
} else {
let resultset = unserialize(data);
}
if typeof resultset != "array" {
throw new Exception("Invalid serialization data");
}
Expand Down

0 comments on commit a0da80b

Please sign in to comment.