Skip to content
Snippets Groups Projects
Commit 1a5cb613 authored by Jared Hancock's avatar Jared Hancock
Browse files

orm: Support constant constraints

For instance
join table2 on (field = 'T' and table1.field_id = table2.id)
parent 44103e4a
No related branches found
No related tags found
No related merge requests found
......@@ -799,7 +799,11 @@ class SqlCompiler {
foreach ($parts as $p) {
$path[] = $p;
$tip = implode('__', $path);
$info = $model::$meta['joins'][$p];
if (!($info = $model::$meta['joins'][$p])) {
throw new OrmException(sprintf(
'Model `%s` does not have a relation called `%s`',
$model, $p));
}
$alias = $this->pushJoin($crumb, $tip, $model, $info);
// Roll to foreign model
foreach ($info['constraint'] as $local => $foreign) {
......@@ -993,11 +997,20 @@ class MySqlCompiler extends SqlCompiler {
$table = $this->quote($model::$meta['table']);
foreach ($info['constraint'] as $local => $foreign) {
list($rmodel, $right) = explode('.', $foreign);
// TODO: Support a constant constraint
$constraints[] = sprintf("%s.%s = %s.%s",
$table, $this->quote($local), $alias,
$this->quote($right)
);
// Support a constant constraint with
// "'constant'" => "Model.field_name"
if ($local[0] == "'") {
$constraints[] = sprintf("%s.%s = %s",
$alias, $this->quote($right),
$this->input(trim($local, '\'"'))
);
}
else {
$constraints[] = sprintf("%s.%s = %s.%s",
$table, $this->quote($local), $alias,
$this->quote($right)
);
}
}
return $join.$this->quote($rmodel::$meta['table'])
.' '.$alias.' ON ('.implode(' AND ', $constraints).')';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment