如何让我的Propel保留联想格式的奇异选择? [英] How do I make Propel retain associative format with singular selects?

查看:136
本文介绍了如何让我的Propel保留联想格式的奇异选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下标准的波轮:

  $ Q = \\ UserQuery ::创建();
$ Q->选择(阵列('身份证','名'));
$ Q->的OrderBy('名');
$ Q-> setOffset($偏移);
$ Q-> setLimit($限制);
返回$ Q->找到();

这工作得很好,并给我内部阵列,即与ID和名称密钥的二维数组。

 阵列(阵列(ID=> 1,名=>'A'),阵列(ID=> 2,名=&GT ;B))

但是,如果我运行以下命令:

  $ Q = \\ UserQuery ::创建();
$ Q->选择(阵列('身份证'));
$ Q->的OrderBy(ID);
$ Q-> setOffset($偏移);
$ Q-> setLimit($限制);
返回$ Q->找到();

它给了我一个一维数组没有列键,只有指标,即

 阵列(1,2)

如果我用PropelArrayFormatter:

  $ Q = \\ UserQuery ::创建();
$ Q->选择(阵列('身份证'));
$ Q->的OrderBy(ID);
$ Q-> setOffset($偏移);
$ Q-> setLimit($限制);
$ Q-> setFormatter('PropelArrayFormatter');
返回$ Q->找到();

据滋润整个对象空值:

 阵列(阵列(ID=> 1,名=>空,散列=>空,LAST_LOGIN=>空),数组(ID=> 2,名= GT;空,散列=>空,LAST_LOGIN=>空))

如果我补充一下:

  $ Q-> setFormatter('PropelArrayFormatter');

然后我得到:

 的通知PHP:未定义抵消:2 BaseUser.php线1518
PHP公告:未定义抵消:3 BaseUser.php在线1519
PHP公告:未定义抵消:4 BaseUser.php在线1520
PHP公告:未定义抵消:5 BaseUser.php线1521
PHP公告:未定义抵消:6 BaseUser.php线1522
PHP公告:未定义抵消:7 BaseUser.php线1523
PHP公告:未定义抵消:8 BaseUser.php线1524
PHP公告:未定义抵消:9 BaseUser.php线1525
PHP公告:未定义抵消:10 BaseUser.php线1526
PHP公告:未定义抵消:11 BaseUser.php线1527
PHP公告:未定义抵消:12 BaseUser.php线1528
PHP公告:未定义抵消:13 BaseUser.php线1529
PHP公告:未定义抵消:14 BaseUser.php线1530
PHP公告:未定义抵消:15 BaseUser.php线1531
PHP公告:未定义抵消:16 BaseUser.php线1532
PHP公告:未定义抵消:17 BaseUser.php线1533
PHP公告:未定义抵消:18 BaseUser.php线1534
PHP公告:未定义抵消:19 BaseUser.php线1535
PHP公告:未定义抵消:20 BaseUser.php线1536

和返回什么。我也曾尝试PropelSimpleArrayFormatter,它永远不会返回一个关联数组:

  $ Q-> setFormatter('PropelSimpleArrayFormatter');

结果:

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


解决方案

行走每默认使用 SimpleArrayFormatter 如果您限制选定列,其中做了一些方便的东西,因为你见过。

如果你想坚持的正常行为,设置正常 ArrayFormatter 作为格式化:

  $ Q-> setFormatter('\\行走\\运行\\格式化\\ ArrayFormatter');

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',
))

解决方案

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');

这篇关于如何让我的Propel保留联想格式的奇异选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆