Skip to content

Commit

Permalink
Merge pull request #1208 from Cinderella-Man/1.3.0
Browse files Browse the repository at this point in the history
Support for array(limit, offset) as a 'limit' constructor key(Query Builder)
  • Loading branch information
Phalcon committed Sep 8, 2013
2 parents 6fabb3f + 9b9f25a commit 5ee9d11
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ext/mvc/model/query/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ PHALCON_INIT_CLASS(Phalcon_Mvc_Model_Query_Builder){
* 'order' => array('name', 'id'),
* 'limit' => 20,
* 'offset' => 20,
* // or 'limit' => array(20, 20),
*);
*$queryBuilder = new Phalcon\Mvc\Model\Query\Builder($params);
*</code>
Expand All @@ -116,6 +117,7 @@ PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, __construct){
zval *models, *columns, *group_clause;
zval *having_clause, *order_clause, *limit_clause;
zval *offset_clause, *for_update, *shared_lock;
zval *limit, *offset;

phalcon_fetch_params(0, 0, 2, &params, &dependency_injector);

Expand Down Expand Up @@ -169,7 +171,17 @@ PHP_METHOD(Phalcon_Mvc_Model_Query_Builder, __construct){
* Assign LIMIT clause
*/
if (phalcon_array_isset_string_fetch(&limit_clause, params, SS("limit"))) {
phalcon_update_property_this(this_ptr, SL("_limit"), limit_clause TSRMLS_CC);
if (Z_TYPE_P(limit_clause) == IS_ARRAY
&& phalcon_array_isset_long_fetch(&limit, limit_clause, 0)
&& phalcon_array_isset_long_fetch(&offset, limit_clause, 1)
&& phalcon_is_numeric(limit)
&& phalcon_is_numeric(offset)
) {
phalcon_update_property_this(this_ptr, SL("_limit"), limit TSRMLS_CC);
phalcon_update_property_this(this_ptr, SL("_offset"), offset TSRMLS_CC);
} else {
phalcon_update_property_this(this_ptr, SL("_limit"), limit_clause TSRMLS_CC);
}
}

/**
Expand Down
76 changes: 76 additions & 0 deletions unit-tests/ModelsQueryBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -345,4 +345,80 @@ public function testSelectDistinctAll()
->getPhql();
$this->assertEquals($phql, 'SELECT Robots.name FROM [Robots]');
}

/**
* Test checks passing query params and dependency injector into
* constructor
*/
public function testConstructor()
{
require 'unit-tests/config.db.php';
if (empty($configMysql)) {
$this->markTestSkipped("Test skipped");
return;
}

$di = $this->_getDI();

$params = array(
'models' => 'Robots',
'columns' => array('id', 'name', 'status'),
'conditions' => "a > 5",
'group' => array('type', 'source'),
'having' => "b < 5",
'order' => array('name', 'created'),
'limit' => 10,
'offset' => 15,
);

$builder = new Builder($params, $di);

$expectedPhql = "SELECT id, name, status FROM [Robots] "
. "WHERE a > 5 GROUP BY [type], [source] "
. "HAVING b < 5 ORDER BY [name], [created] "
. "LIMIT 10 OFFSET 15";

$this->assertEquals($expectedPhql, $builder->getPhql());
$this->assertEquals($di, $builder->getDI());
}

/**
* Test checks passing 'limit'/'offset' query param into constructor.
* limit key can take:
* - signle numeric value
* - array of 2 values (limit, offset)
*/
public function testConstructorLimit()
{
require 'unit-tests/config.db.php';
if (empty($configMysql)) {
$this->markTestSkipped("Test skipped");
return;
}

// separate limit and offset

$params = array(
'models' => 'Robots',
'limit' => 10,
'offset' => 15,
);

$builderLimitAndOffset = new Builder($params);

// separate limit with offset

$params = array(
'models' => 'Robots',
'limit' => array(10, 15),
);

$builderLimitWithOffset = new Builder($params);

$expectedPhql = "SELECT [Robots].* FROM [Robots] "
. "LIMIT 10 OFFSET 15";

$this->assertEquals($expectedPhql, $builderLimitAndOffset->getPhql());
$this->assertEquals($expectedPhql, $builderLimitWithOffset->getPhql());
}
}

0 comments on commit 5ee9d11

Please sign in to comment.