Skip to content

Commit

Permalink
fix bug in with() and ambiguous columns
Browse files Browse the repository at this point in the history
  • Loading branch information
zombor committed Feb 28, 2011
1 parent cd3976e commit 65f56b5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion classes/automodeler/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function __construct($id = NULL)

if ($id !== NULL)
{
$this->load(db::select_array(array_keys($this->_data))->where('id', '=', $id));
$this->load(db::select_array(array_keys($this->_data))->where($this->_table_name.'.id', '=', $id));
}
}

Expand Down
20 changes: 16 additions & 4 deletions classes/automodeler/orm/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,27 @@ public function load(Database_Query_Builder_Select $query = NULL, $limit = 1)

if ($this->_load_with !== NULL)
{
if (is_array($this->_load_with))
{
$model = current(array_keys($this->_load_with));
$alias = current(array_values($this->_load_with));
}
else
{
$model = $this->_load_with;
$alias = $this->_load_with;
}

$fields = array();
foreach (array_merge($this->fields(), AutoModeler_ORM::factory($this->_load_with)->fields()) as $field)
foreach (array_merge($this->fields(), AutoModeler_ORM::factory($model)->fields()) as $field)
{
$fields[] = array($field, str_replace('.', ':', $field));
}

$query->select_array($fields);
$join_table = inflector::plural($this->_load_with);
$query->join($join_table)->on($join_table.'.id', '=', $this->_table_name.'.'.$this->_load_with.'_id');
$query->select_array($fields, TRUE);
$join_model = Model::factory($model);
$join_table = $join_model->get_table_name();
$query->join($join_table)->on($join_table.'.id', '=', $this->_table_name.'.'.$alias.'_id');
}

return parent::load($query, $limit);
Expand Down
51 changes: 51 additions & 0 deletions classes/database/query/builder/select.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php defined('SYSPATH') or die('No direct script access.');
/**
* Database query builder for SELECT statements. See [Query Builder](/database/query/builder) for usage and examples.
*
* @package Kohana/Database
* @category Query
* @author Kohana Team
* @copyright (c) 2008-2009 Kohana Team
* @license http://kohanaphp.com/license
*/
class Database_Query_Builder_Select extends Kohana_Database_Query_Builder_Select
{
/**
* Choose the columns to select from.
*
* @param mixed column name or array($column, $alias) or object
* @param ...
* @return $this
*/
public function select($columns = NULL, $reset = FALSE)
{
if ($reset)
{
$this->_select = array();
}

$columns = func_get_args();

$this->_select = array_merge($this->_select, $columns);

return $this;
}

/**
* Choose the columns to select from, using an array.
*
* @param array list of column names or aliases
* @return $this
*/
public function select_array(array $columns = NULL, $reset = FALSE)
{
if ($reset)
{
$this->_select = array();
}

$this->_select = array_merge($this->_select, $columns);

return $this;
}
}

0 comments on commit 65f56b5

Please sign in to comment.