php 在学说中使用列值作为数组索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12096792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using column value as array index in doctrine
提问by manix
I am using doctrine 2.1 in order to create a model for settingstable:
我正在使用学说 2.1 来创建settings表格模型:
id | arg | value | category
1 | name | foo | general_settings
2 | desc | bar | general_settings
Suppose that I have a lot of setting for different categories. In order to get all the setting for a specific category I do something like this:
假设我有很多不同类别的设置。为了获得特定类别的所有设置,我执行以下操作:
$q = Doctrine_Query::create()
->from('Setting p')
->where('p.category = ?', $category_name);
Everything works fine at this point. Well.. the question of $64,000 is: Do exist a data access alternative that allow me to read the result as below?
此时一切正常。嗯.. 64,000 美元的问题是:是否存在允许我读取结果的数据访问替代方案,如下所示?
$resultSet = $q->execute();
//the magic here could be use the -arg- column as index
$requested_setting = $resulSet['name']
//print the setting value
echo $requested_setting['value']; //should prints "foo"
//another way
echo $resulSet['desc']['value']; //should prints "bar"
回答by manix
I got it: the trick here is use the INDEX BYword.
我明白了:这里的诀窍是使用这个INDEX BY词。
Query class
查询类
import the Query class (no always optional):
导入 Query 类(不总是可选的):
use \Doctrine\ORM\Query;
create the query:
创建查询:
$query = $this->data->em->createQuery('
SELECT s
FROM models\Setting s
INDEX BY s.arg //to set array custom key
WHERE s.category = :category');
$query->setParameter('category', 'general');
set the hidration mode in order to work with read-only arrays
设置 hidration 模式以使用只读数组
$settings = $query->getResult(Query::HYDRATE_ARRAY);
Display the value:
显示值:
echo $settings['desc']['value']; // prints "bar"
QueryBuilder
查询生成器
With the QueryBuilderobject you can set the index at the fromstatement:
使用该QueryBuilder对象,您可以在from语句中设置索引:
$qb = $em->createQueryBuilder();
$qb->select('s');
$qb->from('models\Settings', 's', 's.arg'); // here the magic
$result = $qb->getQuery()->getResult();
Then, you can access the object as:
然后,您可以通过以下方式访问该对象:
$description = $result['desc'];
$value = $description->getValue();
回答by SMASHED
FYI when using createQueryBuilder in your EntityRepository, you can directly specify the INDEX BY along with the alias:
仅供参考,在您的 EntityRepository 中使用 createQueryBuilder 时,您可以直接指定 INDEX BY 和别名:
$this->createQueryBuilder('p', 'p.id')
This avoids handling manually the from which is automatically handled in EntityRepositories.
这避免了手动处理 EntityRepositories 中自动处理的来源。
回答by symi khan
Doctrine IndexBy function is used, to display column value as array index
使用 Doctrine IndexBy 函数,将列值显示为数组索引
$this
// database table alias
->createQueryBuilder( 'app_settings' )
// first parameter should be alias and second parameter will be column name, which you want to show as array index
->indexBy('app_settings','app_settings.name')
// get query
->getQuery()
// get doctrine result in array format
->getArrayResult();
The result of mentioned query will be in this format: Result of mentioned query
提及查询的结果将采用以下格式: 提及查询的结果

