I got it: the trick here is use the INDEX BY
word.
Query class
import the Query class (no always optional):
use DoctrineORMQuery;
create the query:
$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);
Display the value:
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('modelsSettings', 's', 's.arg'); // here the magic
$result = $qb->getQuery()->getResult();
Then, you can access the object as:
$description = $result['desc'];
$value = $description->getValue();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…