Skip to content

Commit

Permalink
Fix nested SELECT bug phalcon#10705, implemented feature phalcon#10704
Browse files Browse the repository at this point in the history
…ability to disable implicit joins for query.
  • Loading branch information
David Schissler committed Aug 5, 2015
1 parent 9c9c7b4 commit 31eeb3d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 4 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@
"type": "bool",
"default": false
},
"orm.enable_implicit_joins": {
"type": "bool",
"default": true
},
"orm.cast_on_hydrate": {
"type": "bool",
"default": false
Expand Down
49 changes: 47 additions & 2 deletions phalcon/mvc/model/query.zep
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ class Query implements QueryInterface, InjectionAwareInterface

protected _bindTypes;

protected _enableImplicitJoins;

static protected _irPhqlCache;

const TYPE_SELECT = 309;
Expand All @@ -114,15 +116,23 @@ class Query implements QueryInterface, InjectionAwareInterface
* @param string phql
* @param \Phalcon\DiInterface dependencyInjector
*/
public function __construct(phql = null, <DiInterface> dependencyInjector = null)
public function __construct(phql = null, <DiInterface> dependencyInjector = null, options = null)
{
var enableImplicitJoins;

if typeof phql != "null" {
let this->_phql = phql;
}

if typeof dependencyInjector == "object" {
this->setDI(dependencyInjector);
}

if typeof options == "array" && fetch enableImplicitJoins, options["enable_implicit_joins"] {
let this->_enableImplicitJoins = (enableImplicitJoins == true);
} else {
let this->_enableImplicitJoins = globals_get("orm.enable_implicit_joins");
}
}

/**
Expand Down Expand Up @@ -1238,7 +1248,7 @@ class Query implements QueryInterface, InjectionAwareInterface
joinPrepared, manager, selectJoins, joinItem, joins, joinData, schema, source, model,
realModelName, completeSource, joinType, aliasExpr, alias, joinAliasName, joinExpr,
fromModelName, joinAlias, joinModel, joinSource, preCondition, modelNameAlias,
relation, relations, modelAlias, sqlJoin, sqlJoinItem;
relation, relations, modelAlias, sqlJoin, sqlJoinItem, selectTables, table, tables, tableItem;

let models = this->_models,
sqlAliases = this->_sqlAliases,
Expand All @@ -1257,6 +1267,13 @@ class Query implements QueryInterface, InjectionAwareInterface

let manager = this->_manager;

let tables = select["tables"];
if !isset tables[0] {
let selectTables = [tables];
} else {
let selectTables = tables;
}

let joins = select["joins"];
if !isset joins[0] {
let selectJoins = [joins];
Expand Down Expand Up @@ -1423,6 +1440,34 @@ class Query implements QueryInterface, InjectionAwareInterface
}
}

/**
* Skip all implicit joins if the option is not enabled
*/
if !this->_enableImplicitJoins {
for joinAliasName, _ in joinPrepared {

let joinType = joinTypes[joinAliasName];
let joinSource = joinSources[joinAliasName];
let preCondition = joinPreCondition[joinAliasName];
let sqlJoins[] = [
"type": joinType,
"source": joinSource,
"conditions": [preCondition]
];
}
return sqlJoins;
}

/**
* Build the list of tables used in the SELECT clause
*/
let fromModels = [];
for tableItem in selectTables {

let table = tableItem["qualifiedName"]["name"];
let fromModels[table] = true;
}

/**
* Create join relationships dynamically
*/
Expand Down

0 comments on commit 31eeb3d

Please sign in to comment.