更新时间:2022-11-25 18:45:56
这里的诀窍是使用 INDEX BY
字。
导入Query类(不总是可选):
import the Query class (no always optional):
use \Doctrine\ORM\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
$settings = $query->getResult(Query::HYDRATE_ARRAY);
显示值:
echo $settings['desc']['value']; // prints "bar"
使用 QueryBuilder
对象,您可以从语句中的设置索引:
With the QueryBuilder
object you can set the index at the from
statement:
$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();