문제

How can I get rid of the table name from the keys in the results.

$query = Doctrine_Core::getTable('Apps')
       ->createQuery('a')
       ->select("DATE_FORMAT(`created_at`,'%Y/%m') as Month,count(*) as Applications")
       ->groupBy(" year(`created_at`) , month(`created_at`)");
       $query->setHydrationMode(Doctrine::HYDRATE_NONE);

Gives me something like

    {
        "a_Month": "2012/09",
        "a_Applications": "3447"
    },
    {
        "a_Month": "2012/10",
        "a_Applications": "565"
    },
    {
        "a_Month": "2012/11",
        "a_Applications": "689"
    }...

I need to get something like

    {
        "Month": "2012/09",
        "Applications": "3447"
    },
    {
        "Month": "2012/10",
        "Applications": "565"
    },
    {
        "Month": "2012/11",
        "Applications": "689"
    }...

Without the table name prepended, is there any way to do this ?

도움이 되었습니까?

해결책

Internally Doctrine uses its own column names to build queries. The column names assigned by you are applied to the results during hydration. Because you used HYDRATE_NONE you get the Doctrine's names as no hydration is being made on the result set.

Use the HYDRATE_ARRAY mode and you will get an array with keys you assigned yourself.

다른 팁

I don't know Doctrine very well so there might be something native but you could try this ($arr is your results array):

array_map(function ($k, $v) use (&$arr){

    $ke = str_replace("a_", "", $k);
    $arr[$ke] = $v;
    unset($arr[$k]);

}, array_keys($arr), $arr);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top