Skip to content

Commit

Permalink
Reusable records issue phalcon#13531
Browse files Browse the repository at this point in the history
  • Loading branch information
Németh Balázs committed May 2, 2019
1 parent 8d3ad17 commit 6e75dcf
Showing 1 changed file with 51 additions and 31 deletions.
82 changes: 51 additions & 31 deletions phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1798,20 +1798,29 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
}

/**
* Call the 'getRelationRecords' in the models manager
* If the related records are already in cache and the relation is reusable,
* we return the cached records.
*/
let result = manager->getRelationRecords(relation, null, this, arguments);

if relation->isReusable() && this->isRelationshipLoaded(lowerAlias) {
let result = this->related[lowerAlias];
} else {
/**
* Call the 'getRelationRecords' in the models manager
*
* The manager also checks and stores reusable records.
*/
let result = manager->getRelationRecords(relation, null, this, arguments);

/**
* We store relationship objects in the related bag
*/
let this->related[lowerAlias] = result;
/**
* We store relationship objects in the related bag
*/
let this->related[lowerAlias] = result;

/**
* We assign the result to the instance avoiding future queries
*/
let this->{lowerAlias} = result;
/**
* We assign the result to the instance avoiding future queries
*/
let this->{lowerAlias} = result;
}

return result;
}
Expand Down Expand Up @@ -2269,7 +2278,7 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
*/
public function save() -> bool
{
var metaData, schema, writeConnection, readConnection, source,
var metaData, schema, writeConnection, readConnection, source, lowerProperty,
table, identityField, exists, success, relatedUnsaved, hasRelatedUnsaved;

let metaData = this->getModelsMetaData();
Expand Down Expand Up @@ -2412,12 +2421,21 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
if success === false {
this->_cancelOperation();
} else {
/**
* Update and clear related caches
*/
let this->related = this->relatedSaved,
this->relatedUnsaved = [],
this->relatedSaved = [];
if(hasRelatedUnsaved) {
/**
* Update and clear related caches
*/
let this->related = this->relatedSaved,
this->relatedUnsaved = [],
this->relatedSaved = [];

/**
* Update the properties of the object
*/
for lowerProperty, records in this->related {
let this->{lowerProperty} = records;
}
}

this->fireEvent("afterSave");
}
Expand Down Expand Up @@ -4076,28 +4094,30 @@ abstract class Model implements EntityInterface, ModelInterface, ResultInterface
/**
* Calling count if the method starts with "count"
*/
elseif starts_with(method, "count") {
if starts_with(method, "count") {
let queryMethod = "count";

let relation = <RelationInterface> manager->getRelationByAlias(
modelName,
substr(method, 5)
);
}

/**
* If the relation was found perform the query via the models manager
*/
if typeof relation != "object" {
return null;
/**
* If the relation was found perform the query via the models manager
*/
if typeof relation != "object" {
return null;
}

return manager->getRelationRecords(
relation,
queryMethod,
this,
extraArgs
);
}

return manager->getRelationRecords(
relation,
queryMethod,
this,
extraArgs
);
return null;
}

/**
Expand Down

0 comments on commit 6e75dcf

Please sign in to comment.