diff --git a/phalcon/Db/Result/PdoResult.zep b/phalcon/Db/Result/PdoResult.zep index c6e1cf76c72..ea24bf1070b 100644 --- a/phalcon/Db/Result/PdoResult.zep +++ b/phalcon/Db/Result/PdoResult.zep @@ -71,9 +71,9 @@ class PdoResult implements ResultInterface protected result; /** - * @var bool + * @var int|null */ - protected rowCount = false; + protected rowCount = null; /** * @var string|null @@ -164,6 +164,7 @@ class PdoResult implements ResultInterface */ public function execute() -> bool { + let this->rowCount = null; return this->pdoStatement->execute(); } @@ -281,7 +282,7 @@ class PdoResult implements ResultInterface let rowCount = this->rowCount; - if rowCount === false { + if rowCount === null { let connection = this->connection, type = connection->getType(); @@ -296,7 +297,7 @@ class PdoResult implements ResultInterface /** * We should get the count using a new statement :( */ - if rowCount === false { + if rowCount === null { /** * SQLite/SQLServer returns resultsets that to the client eyes * (PDO) has an arbitrary number of rows, so we need to perform diff --git a/phalcon/Mvc/Model.zep b/phalcon/Mvc/Model.zep index e5f5bf9b9ac..58c7e5cf23a 100644 --- a/phalcon/Mvc/Model.zep +++ b/phalcon/Mvc/Model.zep @@ -1421,6 +1421,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface, */ if (success) { let this->related = []; + let this->dirtyRelated = []; this->modelsManager->clearReusableObjects(); } @@ -2091,6 +2092,13 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface, // */ // let this->related[lowerAlias] = result; // } + if isset(this->dirtyRelated[lowerAlias]) { + return this->dirtyRelated[lowerAlias]; + } + if isset(this->related[lowerAlias]) { + return this->related[lowerAlias]; + } + /** * We do not need conditionals here. The models manager stores * reusable related records so we utilize that and remove complexity @@ -2758,7 +2766,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface, */ let this->dirtyRelated = []; } - + let this->related = []; this->fireEvent("afterSave"); } diff --git a/phalcon/Mvc/Model/Resultset.zep b/phalcon/Mvc/Model/Resultset.zep index b53ed2fb33b..6ad5c3bd839 100644 --- a/phalcon/Mvc/Model/Resultset.zep +++ b/phalcon/Mvc/Model/Resultset.zep @@ -285,7 +285,7 @@ abstract class Resultset if transaction === true { connection->commit(); } - + this->refresh(); return result; } @@ -697,7 +697,7 @@ abstract class Resultset if transaction === true { connection->commit(); } - + this->refresh(); return transaction; } @@ -708,4 +708,61 @@ abstract class Resultset { return this->pointer < this->count; } + + public function refresh() -> bool + { + var prefetchRecords, rowCount, rows, result, success; + + /** + * 'false' is given as result for empty result-sets + */ + if typeof this->result !== "object" { + let this->count = 0; + let this->rows = []; + + return; + } + let result = this->result; + let success = result->execute(); + if false === success { + return false; + } + let this->isFresh = true; + /** + * Update the row-count + */ + let rowCount = result->numRows(), + this->count = rowCount; + + /** + * Empty result-set + */ + if rowCount == 0 { + let this->rows = []; + return true; + } + + /** + * Small result-sets with less equals 32 rows are fetched at once + */ + let prefetchRecords = (int) globals_get("orm.resultset_prefetch_records"); + if prefetchRecords > 0 && rowCount <= prefetchRecords { + /** + * Fetch ALL rows from database + */ + let rows = result->fetchAll(); + + if typeof rows == "array" { + let this->rows = rows; + } else { + let this->rows = []; + } + } + return true; + } + + public function getResult() -> var + { + return this->result; + } } diff --git a/tests/database/Mvc/Model/DeleteCest.php b/tests/database/Mvc/Model/DeleteCest.php index 021669fc3ef..97c8d13d792 100644 --- a/tests/database/Mvc/Model/DeleteCest.php +++ b/tests/database/Mvc/Model/DeleteCest.php @@ -272,6 +272,7 @@ public function mvcModelDeleteGetRelated(DatabaseTester $I) * Check for the number of invoices */ $expected = 0; + $customer->invoices->refresh(); $actual = $customer->invoices->count(); $I->assertEquals($expected, $actual); }