How do I make Propel retain associative format with singular selects?

纵然是瞬间 提交于 2019-12-12 06:30:35

问题


I have the following Criteria in Propel:

$q = \UserQuery::create();
$q->select(array('id', 'name'));
$q->orderBy('name');
$q->setOffset($offset);
$q->setLimit($limit);
return $q->find();

This works fine, and gives me a two-dimensional array with id and name keys in the inner array, i.e.

array( array("id" => 1, "name" => 'A'), array("id" => 2, "name" => 'B') )

However, if I run the following:

$q = \UserQuery::create();
$q->select(array('id'));
$q->orderBy('id');
$q->setOffset($offset);
$q->setLimit($limit);
return $q->find();

It gives me a one-dimensional array with no column keys, only indexes, i.e.

array(1, 2)

If I use the PropelArrayFormatter:

$q = \UserQuery::create();
$q->select(array('id'));
$q->orderBy('id');
$q->setOffset($offset);
$q->setLimit($limit);
$q->setFormatter('PropelArrayFormatter');
return $q->find();

It hydrates the entire object with null values:

array( array("id" => 1, "name" => null, "hash" => null, "last_login" => null), array("id" => 2, "name" => null, "hash" => null, "last_login" => null) )

If I add:

$q->setFormatter('PropelArrayFormatter');

Then I get:

PHP Notice:  Undefined offset: 2 in BaseUser.php on line 1518
PHP Notice:  Undefined offset: 3 in BaseUser.php on line 1519
PHP Notice:  Undefined offset: 4 in BaseUser.php on line 1520
PHP Notice:  Undefined offset: 5 in BaseUser.php on line 1521
PHP Notice:  Undefined offset: 6 in BaseUser.php on line 1522
PHP Notice:  Undefined offset: 7 in BaseUser.php on line 1523
PHP Notice:  Undefined offset: 8 in BaseUser.php on line 1524
PHP Notice:  Undefined offset: 9 in BaseUser.php on line 1525
PHP Notice:  Undefined offset: 10 in BaseUser.php on line 1526
PHP Notice:  Undefined offset: 11 in BaseUser.php on line 1527
PHP Notice:  Undefined offset: 12 in BaseUser.php on line 1528
PHP Notice:  Undefined offset: 13 in BaseUser.php on line 1529
PHP Notice:  Undefined offset: 14 in BaseUser.php on line 1530
PHP Notice:  Undefined offset: 15 in BaseUser.php on line 1531
PHP Notice:  Undefined offset: 16 in BaseUser.php on line 1532
PHP Notice:  Undefined offset: 17 in BaseUser.php on line 1533
PHP Notice:  Undefined offset: 18 in BaseUser.php on line 1534
PHP Notice:  Undefined offset: 19 in BaseUser.php on line 1535
PHP Notice:  Undefined offset: 20 in BaseUser.php on line 1536

And nothing is returned. I have also tried PropelSimpleArrayFormatter, and it never returns an associative array:

$q->setFormatter('PropelSimpleArrayFormatter');

The result:

PropelArrayCollection::__set_state(array(
   0 => '123',
   1 => '126',
   2 => '133',
   3 => '242',
   4 => '256',
   5 => '272',
   6 => '283',
   7 => '284',
   8 => '474',
   9 => '617',
))

回答1:


Propel uses per default SimpleArrayFormatter if you limit the selected columns, which does some handy stuff as you've seen.

If you want to stick with the normal behavior, set the normal ArrayFormatter as Formatter:

$q->setFormatter('\Propel\Runtime\Formatter\ArrayFormatter');


来源:https://stackoverflow.com/questions/20849227/how-do-i-make-propel-retain-associative-format-with-singular-selects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!