dreamy6301 2014-03-19 12:23
浏览 49
已采纳

Backbone JS + Codeigniter Restful API和MYSQL子串函数无法正常工作

I have this BackboneJS Application where I use Codeigniter as Backend. I use the RESTful API to return data from my MySQL Database to the Frontend. As part of my application, I want to display Music artists Albums and the relevant tracks and the tracks duration. Right now, I get the track duration like 00:03:26 but I want it to display 03:26, so I made this MySQL-query where i use the SUBSTRING-method:

public function artist_tracks_get($artist_id)  
{ 
    $this->load->database();
    $sql = "SELECT products.title AS album_title, products.genre AS album_genre, products.ccws_pro_id AS product_upc, track_title, SUBSTRING(track_duration, 4) AS track_duration FROM products
                INNER JOIN track_albumn_information ON products.ccws_pro_upc = track_albumn_information.product_upc 
                AND track_albumn_information.artist_id =  '".$artist_id."'  LIMIT 0 , 30";
    $query = $this->db->query($sql);
    $data = $query->result();

    if($data) {
        $this->response($data, 200); 
    } else {
        $this->response(array('error' => 'Couldn\'t find any artist albums!'), 404);
    }
}

And inside my Backbone View I have:

serialize: function() {
var grouped = _.groupBy(this.collection.toJSON(), function(item) {
    return item.album_title;
}), spanned = [];
_.each(grouped, function(item) {
    var firstItem = item[0],
        spannedItem = {
            'album_genre': firstItem.album_genre,
            'album_id': firstItem.album_id,
            'album_title': firstItem.album_title,
            'cover_image': firstItem.cover_image,
            'digitalReleaseDate': firstItem.digitalReleaseDate,
            'physicalReleaseDate': firstItem.physicalReleaseDate,
            'pro_id': firstItem.pro_id,
            'product_upc': firstItem.product_upc,
            'tracks': []
        };
    _.each(item, function(albumItem) {
        spannedItem.tracks.push({
            'track_duration': albumItem.track_duration,
            'track_title': albumItem.track_title
        })
    })
    spanned.push(spannedItem);
});

return spanned;
}

When I run this query on my local phpmyadmin, I get the right result like 03:26 but when I test it online on my webserver, I get 00:03:26... What is the issue here? Is it the SQL version? Is BackboneJs unable to handle the SUBSTRING-method?

Please help! Thanks in advance...

  • 写回答

1条回答 默认 最新

  • douxuan0698 2014-03-19 13:34
    关注

    Backbone has no idea what you are doing in MySQL so it's not Backbone having a problem with MySQL SUBSTRING().

    If you wanted to avoid dealing with MYSQL, you could simply do

    albumItem.track_duration.substr(3);
    

    in the view to display it as "03:26".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊