Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed getRelated already fetched relations #16449

Open
wants to merge 1 commit into
base: 5.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions phalcon/Db/Result/PdoResult.zep
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ class PdoResult implements ResultInterface
protected result;

/**
* @var bool
* @var int|null
*/
protected rowCount = false;
protected rowCount = null;

/**
* @var string|null
Expand Down Expand Up @@ -164,6 +164,7 @@ class PdoResult implements ResultInterface
*/
public function execute() -> bool
{
let this->rowCount = null;
return this->pdoStatement->execute();
}

Expand Down Expand Up @@ -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();

Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion phalcon/Mvc/Model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
*/
if (success) {
let this->related = [];
let this->dirtyRelated = [];
this->modelsManager->clearReusableObjects();
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -2758,7 +2766,7 @@ abstract class Model extends AbstractInjectionAware implements EntityInterface,
*/
let this->dirtyRelated = [];
}

let this->related = [];
this->fireEvent("afterSave");
}

Expand Down
61 changes: 59 additions & 2 deletions phalcon/Mvc/Model/Resultset.zep
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ abstract class Resultset
if transaction === true {
connection->commit();
}

this->refresh();
return result;
}

Expand Down Expand Up @@ -697,7 +697,7 @@ abstract class Resultset
if transaction === true {
connection->commit();
}

this->refresh();
return transaction;
}

Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions tests/database/Mvc/Model/DeleteCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading