你好,我有一個PostgreSQL的查詢,我想使用CakePHP格式SQL查詢CakePHP的格式(無效的JSON結果)
SELECT
id,
title,
author,
postdate,
postcontent,
userID
FROM posts
WHERE
userID = 12
AND id IN (SELECT articleID FROM favourites WHERE articlesUserID = 12)
ORDER BY postdate DESC;
這是我的查詢有現在在CakePHP的格式來寫:
$favouritearticles = $this->Favourite->query('SELECT id, title, author, postdate, postdatecreation, posteditdate, postcontent, "userID" FROM posts WHERE "userID" = '.$userID.'AND id IN (SELECT lngblogarticleid FROM favourites WHERE lngloginuserid = '.$userID.') ORDER BY postdate DESC');
它的工作,但如果回聲json_encode這樣的結果:
echo json_encode($favouritearticles);
我得到一個無效的JSON喜歡的格式如下:(帶JSONLint選中)
[
[
{
"id": 2,
"title": "Prison Or Treatment For the Mentally ill ",
"author": "mike123",
"postdate": "March 12, 2013 at 6:46 pm",
"postdatecreation": "2013-03-12",
"posteditdate": null,
"postcontent": "<p><span>The public revulsion over repeated mass shootings has placed mental health in the spotlight. This is both good and bad.<\/span><\/p>",
"userID": 34
}
]
][
]
所以我想,也許我應該使用CakePHP格式「使用find方法」重寫我的查詢是這樣的:
$favouritearticles = $this->Favourite->find('all',array('conditions'=>array(".........
但查詢是相當複雜,我不明白如何去做。 謝謝你的幫助。
您試過了什麼?請顯示你已經嘗試過的內容,並解釋你卡在哪裏。 – 2013-03-24 04:45:59
我對我的問題做了一些修改,謝謝。 – OussamaLord 2013-03-24 10:45:05