我有一个 BackboneJS 应用程序,我使用 Codeigniter 作为后端。我使用 RESTful API 将数据从 MySQL 数据库返回到前端。作为我的应用程序的一部分,我想显示音乐艺术家专辑和相关轨道以及轨道持续时间。现在,我得到像 00:03:26
这样的轨道持续时间,但我希望它显示 03:26
,所以我做了这个 MySQL 查询,其中我使用 SUBSTRING
-方法:
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);
}
}
在我的主干 View 中,我有:
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;
}
当我在本地 phpmyadmin 上运行此查询时,我得到了正确的结果,例如 03:26
但当我在网络服务器上在线测试它时,我得到 00:03:26
...这里有什么问题?是SQL版本吗? BackboneJs 无法处理 SUBSTRING
方法吗?
请帮忙!提前致谢...
最佳答案
Backbone 不知道您在 MySQL 中做什么,因此 Backbone 没有 MySQL SUBSTRING()
问题。
如果你想避免处理 MYSQL,你可以简单地这样做
albumItem.track_duration.substr(3);
在 View 中将其显示为“03:26”。
关于php - Backbone JS + Codeigniter Restful API 和 MYSQL 子字符串功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22505733/