Backbonejs只在PHP JOIN上获取一条记录

时间:2012-08-31 15:47:24

标签: php join backbone.js fetch

好的,所以我有一个响应GET HTTP请求的php;这个php脚本调用数据库,并检索一组行。

$results = $db->pdoquery("SELECT * FROM posts JOIN users ON posts.user_id=users.id ORDER BY posts.cre DESC LIMIT 4");
    $json = json_encode($results);
    echo $json;

现在当我使用backbonejs'.fetch'命令时,它只返回第一条记录的1,而不是4条。

如果我改变了PHP脚本,我会得到所有四个,但我必须失去我的JOIN

$results = $db->pdoquery("SELECT * FROM posts ORDER BY posts.cre DESC LIMIT 4");
    $json = json_encode($results);
    echo $json;

有谁能解释我做错了什么?

更新: php页面打印的JSON

[{"id":"7","post_name":"title","pretty_url":"link1","preface":"a preface 7","body_text":"a body","feat":"0","img":"\/img\/logo.png","cre":"2012-07-22","modi":"2012-07-18","user_id":"2"},{"id":"6","post_name":"title","pretty_url":"link","preface":"a preface 6","body_text":"a body1","feat":"0","img":"\/img\/logo.png","cre":"2012-07-21","modi":"2012-07-18","user_id":"2"},{"id":"5","post_name":"TestTitle 1","pretty_url":"testlink4","preface":"a preface 5","body_text":"a body 2","feat":"0","img":"\/img\/logo.png","cre":"2012-07-20","modi":"2012-07-16","user_id":"2"},{"id":"4","post_name":"TestTitle 1","pretty_url":"testlink3","preface":"a preface 4","body_text":"a body 3","feat":"0","img":"\/img\/logo.png","cre":"2012-07-19","modi":"2012-07-16","user_id":"2"}]

1 个答案:

答案 0 :(得分:0)

我发现了问题。

帖子和用户的列名相同,主要是“id”,由于这四个帖子记录具有相同的user_id,因此“id”列的设置值相同。

Backbone必须内置一些内容,跳过它认为相同的记录或类似的东西。简单地描述我需要更详细的内容解决了这个问题。

无论修复它的代码是否在

之下
        $results = $db->pdoquery("SELECT 
        posts.post_name, posts.pretty_url, posts.preface, posts.img, posts.cre, posts.modi, users.username
        FROM posts 
            JOIN users 
                ON posts.user_id=users.id 
                ORDER BY posts.cre 
                DESC LIMIT 4");
    $json = json_encode($results);
    echo $json;

感谢所有帮助过的人。