久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在學(xué)說(shuō)中使用列值作為數(shù)組索引

Using column value as array index in doctrine(在學(xué)說(shuō)中使用列值作為數(shù)組索引)
本文介紹了在學(xué)說(shuō)中使用列值作為數(shù)組索引的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在使用doctrine 2.1來(lái)為settings表創(chuàng)建一個(gè)模型:

I am using doctrine 2.1 in order to create a model for settings table:

id |  arg  |  value  |  category
1  |  name |  foo    |  general_settings 
2  |  desc |  bar    |  general_settings 

假設(shè)我有很多不同類別的設(shè)置.為了獲得特定類別的所有設(shè)置,我執(zhí)行以下操作:

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

此時(shí)一切正常.嗯.. 64,000 美元的問(wèn)題是:是否存在允許我讀取如下結(jié)果的數(shù)據(jù)訪問(wèn)替代方案?

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?

$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"

推薦答案

我明白了:這里的訣竅是使用 INDEX BY 詞.

I got it: the trick here is use the INDEX BY word.

導(dǎo)入 Query 類(并非總是可選的):

import the Query class (no always optional):

use DoctrineORMQuery;

創(chuàng)建查詢:

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

設(shè)置隱藏模式以使用只讀數(shù)組

set the hidration mode in order to work with read-only arrays

$settings = $query->getResult(Query::HYDRATE_ARRAY); 

顯示值:

echo $settings['desc']['value'];  // prints "bar"

查詢構(gòu)建器

使用 QueryBuilder 對(duì)象,您可以在 from 語(yǔ)句中設(shè)置索引:

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

然后,您可以通過(guò)以下方式訪問(wèn)該對(duì)象:

Then, you can access the object as:

$description = $result['desc'];
$value = $description->getValue();

這篇關(guān)于在學(xué)說(shuō)中使用列值作為數(shù)組索引的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個(gè)表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設(shè)置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數(shù)組自動(dòng)填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產(chǎn)生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 日韩欧美操 | 色综合美女 | 亚洲精品乱码久久久久久按摩 | 国产中文视频 | av一区二区三区四区 | 中文字幕一区在线观看视频 | 好姑娘高清在线观看电影 | 亚洲 中文 欧美 日韩 在线观看 | av手机在线 | 欧美视频第三页 | 欧洲一区在线观看 | 成人九色 | 亚洲精品电影网在线观看 | 欧美激情精品久久久久久 | 国产一在线观看 | 国产精品久久久久久久久免费桃花 | 欧美精品久久久久久久久老牛影院 | 97av视频在线| 精品国产乱码一区二区三区a | 精品国产亚洲一区二区三区大结局 | 日韩在线欧美 | 久久毛片 | 在线观看免费av网 | 亚洲天堂精品一区 | 日本亚洲一区 | 欧美不卡 | 亚洲在线一区 | 国产精品视频在线播放 | 久久久久久国产精品 | 又爽又黄axxx片免费观看 | 殴美成人在线视频 | 国产情侣久久 | 国产精品一区二区三区四区五区 | 国产欧美日韩视频 | 久久久久久国产精品 | 午夜精品久久久 | 成人精品视频99在线观看免费 | 亚洲精品免费视频 | 综合九九| 亚洲超碰在线观看 | 国产在线观 |