Skip to content

Commit

Permalink
Remove Table::query(); Add Model::query()
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Mar 17, 2018
1 parent 3877765 commit dc856b5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
32 changes: 20 additions & 12 deletions lib/ActiveRecord/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public function __call($method, $arguments)
|| strpos($method, 'filter_by_') === 0
|| method_exists($this, 'scope_' . $method))
{
return $this->new_query()->$method(...$arguments);
return $this->query()->$method(...$arguments);
}

if (is_callable([ RelationCollection::class, $method ]))
Expand All @@ -318,7 +318,7 @@ public function __get($property)

if (method_exists($this, $method))
{
return $this->$method($this->new_query());
return $this->$method($this->query());
}

return parent::__get($property);
Expand Down Expand Up @@ -472,6 +472,24 @@ private function find_many(array $keys)
return $records;
}

/**
* @param mixed ...$conditions_and_args
*
* @return Query
*/
public function query(...$conditions_and_args)
{
/* @var Query $query */
$class = $this->query_class;
$query = new $class($this);

if ($conditions_and_args) {
$query->where(...$conditions_and_args);
}

return $query;
}

/**
* Because records are cached, we need to remove the record from the cache when it is saved,
* so that loading the record again returns the updated record, not the one in the cache.
Expand Down Expand Up @@ -637,14 +655,4 @@ protected function new_record(array $properties = [])

return $properties ? $class::from($properties, [ $this ]) : new $class($this);
}

/**
* @return Query
*/
protected function new_query()
{
$class = $this->query_class;

return new $class($this);
}
}
6 changes: 3 additions & 3 deletions lib/ActiveRecord/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1260,14 +1260,14 @@ private function compute($method, $column)
}

$query .= ' AS count ' . $this->render_from() . $this->render_main();
$query = $this->model->query($query, $this->args);
$statement = $this->model->__invoke($query);

if ($method == 'COUNT' && $column)
{
return $query->fetchAll(\PDO::FETCH_KEY_PAIR);
return $statement->pairs;
}

return (int) $query->rc;
return (int) $statement->rc;
}

/**
Expand Down
27 changes: 6 additions & 21 deletions lib/ActiveRecord/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,9 @@ public function __construct(array $attributes)
}

/**
* Alias to {@link query()}.
* Interface to the connection's query() method.
*
* The statement is resolved using the resolve_statement() method and prepared.
*
* @param string $query
* @param array $args
Expand All @@ -314,7 +316,9 @@ public function __construct(array $attributes)
*/
public function __invoke($query, array $args = [], array $options = [])
{
return $this->query($query, $args, $options);
$statement = $this->prepare($query, $options);

return $statement($args);
}

/**
Expand Down Expand Up @@ -594,25 +598,6 @@ public function execute($query, array $args = [], array $options = [])
return $statement($args);
}

/**
* Interface to the connection's query() method.
*
* The statement is resolved using the resolve_statement() method and prepared.
*
* @param string $query
* @param array $args
* @param array $options
*
* @return Statement
*/
public function query($query, array $args = [], array $options = [])
{
$statement = $this->prepare($this->resolve_statement($query), $options);
$statement($args);

return $statement;
}

/**
* Filters mass assignment values.
*
Expand Down
17 changes: 16 additions & 1 deletion tests/ActiveRecord/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,21 @@ public function test_custom_query()

]);

$this->assertInstanceOf(CustomQuery::class, $model->where('1 = 1'));
$this->assertInstanceOf(CustomQuery::class, $query1 = $model->where('1 = 1'));
$this->assertInstanceOf(CustomQuery::class, $query2 = $model->query());
$this->assertNotSame($query1, $query2);
}

public function test_query()
{
$model = $this->model;

$this->assertInstanceOf(Query::class, $query1 = $model->query());
$this->assertInstanceOf(Query::class, $query2 = $model->query("1 = 1"));
$this->assertSame(
'SELECT * FROM `myprefix_contents` `content`'.
' INNER JOIN `myprefix_nodes` `node` USING(`nid`) WHERE (1 = 1)',
(string) $query2
);
}
}

0 comments on commit dc856b5

Please sign in to comment.