diff --git a/changelog.md b/changelog.md index d20e3a303..130ffda1c 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,11 @@ ## Changelog +### 4.4.1 + +- Bugfix: Removed reliance on MySQL-specific backticks in queries +- Bugfix: New validateArray method in Laravel core Validator class was messing with custom version in Administrator's Validator +- Bugfix: In relationship where clauses, there would be issues with values defined on the pivot table + ### 4.4.0 - You can now provide custom actions to a "global_actions" option in model configs. These actions are passed the current filtered query object and can be used to perform table-wide actions. - There is now a query_filter option for model configs that lets you filter a model's results query before it's constructed diff --git a/readme.md b/readme.md index d1560c038..b24076969 100644 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ Administrator is a database interface package for the Laravel PHP framework. Adm - **Author:** Jan Hartigan - **Website:** [http://frozennode.com](http://frozennode.com) -- **Version:** 4.4.0 +- **Version:** 4.4.1 [![Build Status](https://travis-ci.org/FrozenNode/Laravel-Administrator.png?branch=master)](https://travis-ci.org/FrozenNode/Laravel-Administrator) @@ -53,6 +53,12 @@ Administrator is released under the MIT License. See the LICENSE file for detail ## Changelog +### 4.4.1 + +- Bugfix: Removed reliance on MySQL-specific backticks in queries +- Bugfix: New validateArray method in Laravel core Validator class was messing with custom version in Administrator's Validator +- Bugfix: In relationship where clauses, there would be issues with values defined on the pivot table + ### 4.4.0 - You can now provide custom actions to a "global_actions" option in model configs. These actions are passed the current filtered query object and can be used to perform table-wide actions. - There is now a query_filter option for model configs that lets you filter a model's results query before it's constructed diff --git a/src/Frozennode/Administrator/DataTable/Columns/Column.php b/src/Frozennode/Administrator/DataTable/Columns/Column.php index 7077cf6a0..2f4b6ed18 100644 --- a/src/Frozennode/Administrator/DataTable/Columns/Column.php +++ b/src/Frozennode/Administrator/DataTable/Columns/Column.php @@ -188,7 +188,7 @@ public function filterQuery(&$selects) { if ($select = $this->getOption('select')) { - $selects[] = $this->db->raw($select . ' AS `' . $this->getOption('column_name') . '`'); + $selects[] = $this->db->raw($select . ' AS ' . $this->db->getQueryGrammar()->wrap($this->getOption('column_name'))); } } diff --git a/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsTo.php b/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsTo.php index cd33cd1cd..fc0dc413b 100644 --- a/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsTo.php +++ b/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsTo.php @@ -127,7 +127,7 @@ public function filterQuery(&$selects) $selects[] = $this->db->raw("(SELECT " . $this->getOption('select') . " FROM " . $from_table." AS " . $field_table . ' ' . $joins . " - WHERE " . $where . ") AS `" . $columnName . "`"); + WHERE " . $where . ") AS " . $this->db->getQueryGrammar()->wrap($columnName)); } /** diff --git a/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsToMany.php b/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsToMany.php index 22224fa90..dc15dcca6 100644 --- a/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsToMany.php +++ b/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsToMany.php @@ -42,14 +42,14 @@ public function filterQuery(&$selects) .' LEFT JOIN '.$other_table.' AS '.$other_alias.' ON '.$other_alias.'.'.$other_key.' = '.$int_alias.'.'.$column2; //grab the existing where clauses that the user may have set on the relationship - $relationshipWheres = $this->getRelationshipWheres($relationship, $other_alias); + $relationshipWheres = $this->getRelationshipWheres($relationship, $other_alias, $int_alias); $where = $this->tablePrefix . $model->getTable() . '.' . $model->getKeyName() . ' = ' . $int_alias . '.' . $column1 . ($relationshipWheres ? ' AND ' . $relationshipWheres : ''); $selects[] = $this->db->raw("(SELECT " . $this->getOption('select') . " FROM " . $from_table." AS " . $field_table . ' ' . $joins . " - WHERE " . $where . ") AS `" . $columnName . "`"); + WHERE " . $where . ") AS " . $this->db->getQueryGrammar()->wrap($columnName)); } /** diff --git a/src/Frozennode/Administrator/DataTable/Columns/Relationships/HasOneOrMany.php b/src/Frozennode/Administrator/DataTable/Columns/Relationships/HasOneOrMany.php index 29ef5c234..d5219e85f 100644 --- a/src/Frozennode/Administrator/DataTable/Columns/Relationships/HasOneOrMany.php +++ b/src/Frozennode/Administrator/DataTable/Columns/Relationships/HasOneOrMany.php @@ -30,6 +30,6 @@ public function filterQuery(&$selects) $selects[] = $this->db->raw("(SELECT " . $this->getOption('select') . " FROM " . $from_table." AS " . $field_table . ' ' . $joins . " - WHERE " . $where . ") AS `" . $columnName . "`"); + WHERE " . $where . ") AS " . $this->db->getQueryGrammar()->wrap($columnName)); } } \ No newline at end of file diff --git a/src/Frozennode/Administrator/DataTable/Columns/Relationships/Relationship.php b/src/Frozennode/Administrator/DataTable/Columns/Relationships/Relationship.php index 21281a683..1c01de2e9 100644 --- a/src/Frozennode/Administrator/DataTable/Columns/Relationships/Relationship.php +++ b/src/Frozennode/Administrator/DataTable/Columns/Relationships/Relationship.php @@ -77,10 +77,11 @@ public function getIncludedColumn() * * @param Illuminate\Database\Eloquent\Relations\Relation $relationship * @param string $tableAlias + * @param string $pivotAlias * * @return string */ - public function getRelationshipWheres($relationship, $tableAlias) + public function getRelationshipWheres($relationship, $tableAlias, $pivotAlias = null) { //get the query instance $query = $relationship->getQuery()->getQuery(); @@ -94,7 +95,17 @@ public function getRelationshipWheres($relationship, $tableAlias) //iterate over the wheres to properly alias the columns foreach ($query->wheres as &$where) { - $where['column'] = $tableAlias . '.' . $where['column']; + //split the $where column on '.' which indicates that the clause is for a pivot table + $split = explode('.', $where['column']); + + if (isset($split[1])) + { + $where['column'] = $pivotAlias . '.' . $split[1]; + } + else + { + $where['column'] = $tableAlias . '.' . $where['column']; + } } $sql = $query->toSql(); diff --git a/src/Frozennode/Administrator/Validator.php b/src/Frozennode/Administrator/Validator.php index 994e45c3a..db6e05bb7 100644 --- a/src/Frozennode/Administrator/Validator.php +++ b/src/Frozennode/Administrator/Validator.php @@ -141,7 +141,7 @@ public function validateDirectory($attribute, $value, $parameters) /** * Validates that an item is an array */ - public function validateArray($attribute, $value, $parameters) + public function validateArray($attribute, $value) { return is_array($value); } diff --git a/tests/DataTable/Columns/ColumnTest.php b/tests/DataTable/Columns/ColumnTest.php index 4c9f8bca3..951c7b1a8 100644 --- a/tests/DataTable/Columns/ColumnTest.php +++ b/tests/DataTable/Columns/ColumnTest.php @@ -87,7 +87,10 @@ public function testBuild() public function testFilterQueryAddsSelect() { $this->column->shouldReceive('getOption')->twice()->andReturn('foo'); - $this->db->shouldReceive('raw')->once()->andReturn('foo'); + $grammar = m::mock('Illuminate\Database\Query\Grammars'); + $grammar->shouldReceive('wrap')->once()->andReturn(''); + $this->db->shouldReceive('raw')->once()->andReturn('foo') + ->shouldReceive('getQueryGrammar')->once()->andReturn($grammar); $selects = array(); $this->column->filterQuery($selects); $this->assertEquals($selects, array('foo')); @@ -96,6 +99,8 @@ public function testFilterQueryAddsSelect() public function testFilterQueryDoesntAddSelect() { $this->column->shouldReceive('getOption')->once(); + $this->db->shouldReceive('raw')->never() + ->shouldReceive('getQueryGrammar')->never(); $selects = array(); $this->column->filterQuery($selects); $this->assertEquals($selects, array()); diff --git a/tests/DataTable/Columns/Relationships/HasOneOrManyTest.php b/tests/DataTable/Columns/Relationships/HasOneOrManyTest.php index b7c2d0b7c..ffe8cfdf5 100644 --- a/tests/DataTable/Columns/Relationships/HasOneOrManyTest.php +++ b/tests/DataTable/Columns/Relationships/HasOneOrManyTest.php @@ -59,10 +59,13 @@ public function testFilterQuery() { $relationship = m::mock(array('getPlainForeignKey' => '', 'getRelated' => m::mock(array('getTable' => 'table')))); $model = m::mock(array('getTable' => 'table', 'getKeyName' => '', 'method' => $relationship)); + $grammar = m::mock('Illuminate\Database\Query\Grammars'); + $grammar->shouldReceive('wrap')->once()->andReturn(''); $this->config->shouldReceive('getDataModel')->once()->andReturn($model); $this->column->shouldReceive('getOption')->times(3)->andReturn('column_name', 'method', 'select') ->shouldReceive('getRelationshipWheres')->once()->andReturn(''); - $this->db->shouldReceive('raw')->once()->andReturn('foo'); + $this->db->shouldReceive('raw')->once()->andReturn('foo') + ->shouldReceive('getQueryGrammar')->once()->andReturn($grammar); $selects = array(); $this->column->filterQuery($selects); $this->assertEquals($selects, array('foo'));