我试图在使用Laravel 3的字典应用中修改查询。 这部分代码可以在模型文件夹中找到。
public static function getItems( $orderBy, $limit = null, $term_id = null )
{
$query = 'SELECT definitions.*, terms.term, COUNT(DISTINCT(likes.id)) AS likes, COUNT(DISTINCT(dislikes.id)) AS dislikes,
CASE WHEN COUNT(voted.id) > 0 THEN 1 ELSE 0 END AS voted,
GROUP_CONCAT(DISTINCT(tags.name) SEPARATOR ",") AS tags ';
// Select all the definition information, and count the likes and dislike records
// Get whether the user has voted also
$query .= 'FROM definitions ';
// From definitions
$query .= 'INNER JOIN terms ON terms.id = definitions.term_id ';
$query .= 'LEFT JOIN votes AS likes ON definitions.id = likes.definition_id AND likes.score = 1 ';
// Join on the votes table by likes
$query .= 'LEFT JOIN votes AS dislikes ON definitions.id = dislikes.definition_id AND dislikes.score = -1 ';
// Join on the votes table again, but by dislikes
$query .= 'LEFT JOIN votes AS voted ON definitions.id = voted.definition_id AND voted.ip = ? ';
// Join on the votes table again, but for voting
$query .= 'LEFT JOIN definition_tag ON definitions.id = definition_tag.definition_id ';
$query .= 'LEFT JOIN tags ON tags.id = definition_tag.tag_id ';
// Join on the tags table
$query .= 'WHERE approved = 1 ';
// Only get approved definitions matching the term
if ( ! is_null( $term_id ) )
{
$query .= 'AND definitions.term_id = ? ';
}
$query .= 'GROUP BY definitions.id ';
// Group by the definition id
$query .= 'ORDER BY '.$orderBy;
// And order by highest likes
if ( ! is_null( $limit ) ) $query .= ' LIMIT '.$limit;
// Add the limit
我试图摆脱投票(喜欢和不喜欢)的一部分,所以我只是删除了相关的查询。
public static function getItems( $orderBy, $limit = null, $term_id = null )
{
$query = 'SELECT definitions.*, terms.term,
GROUP_CONCAT(DISTINCT(tags.name) SEPARATOR ",") AS tags ';
// Select all the definition information, and count the likes and dislike records
// Get whether the user has voted also
$query .= 'FROM definitions ';
// From definitions
$query .= 'INNER JOIN terms ON terms.id = definitions.term_id ';
$query .= 'LEFT JOIN definition_tag ON definitions.id = definition_tag.definition_id ';
$query .= 'LEFT JOIN tags ON tags.id = definition_tag.tag_id ';
// Join on the tags table
$query .= 'WHERE approved = 1 ';
// Only get approved definitions matching the term
if ( ! is_null( $term_id ) )
{
$query .= 'AND definitions.term_id = ? ';
}
$query .= 'GROUP BY definitions.id ';
// Group by the definition id
// And order by highest likes
if ( ! is_null( $limit ) ) $query .= ' LIMIT '.$limit;
// Add the limit
但是修改后首页的内容为零。所以我在SQL中查询了它是否返回结果,并且确实如此。视图文件夹中没有对投票系统的引用。 我从未使用过Laravel,所以我不知道从哪里开始寻找答案。