更新时间:2022-12-12 11:29:43
我明白了:这里的诀窍是使用 INDEX BY
词.
I got it: the trick here is use the INDEX BY
word.
导入 Query 类(并非总是可选的):
import the Query class (no always optional):
use DoctrineORMQuery;
创建查询:
$query = $this->data->em->createQuery('
SELECT s
FROM modelsSetting 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
$settings = $query->getResult(Query::HYDRATE_ARRAY);
显示值:
echo $settings['desc']['value']; // prints "bar"
使用 QueryBuilder
对象,您可以在 from
语句中设置索引:
With the QueryBuilder
object you can set the index at the from
statement:
$qb = $em->createQueryBuilder();
$qb->select('s');
$qb->from('modelsSettings', 's', 's.arg'); // here the magic
$result = $qb->getQuery()->getResult();
然后,您可以通过以下方式访问该对象:
Then, you can access the object as:
$description = $result['desc'];
$value = $description->getValue();