【发布时间】:2016-03-17 15:38:13
【问题描述】:
我正在使用一个 symfony 网络应用程序并创建了一个包来获取旧版 WordPress 数据库,并且它曾经可以正常工作:
class PostsProvider extends AbstractProvider
{
// ...
public function searchPostsWith($queries, $locale)
{
// "p" stands for Posts class
$qb = $this->repPost->createQueryBuilder('p');
$qb
->select('p.id')
->addSelect('p.postTitle as title')
->addSelect('p.postContent as content')
->addSelect('p.postType as type')
->addSelect('p.postName as slug')
->where('p.postStatus = :publish')
->andWhere('p.postType = :post')
->orWhere('p.postType = :page')
->setParameter('publish', 'publish')
->setParameter('post', 'post')
->setParameter('page', 'page')
->leftJoin('WordPressBundle:TermRelationships', 'r', 'HAVING', 'p.id = r.objectId')
->innerJoin('WordPressBundle:Terms', 't', 'WITH', 'r.termTaxonomyId = t.termId')
->addSelect('t.slug as locale')
->having('t.slug = :locale')
->setParameter('locale', $locale)
;
// ...
但自从我在本地环境中升级到 mySQL 5.7 后,我得到了:
SQLSTATE[42000]:语法错误或访问冲突:1055 SELECT 列表的表达式 #6 不在 GROUP BY 子句中,并且包含在功能上不依赖于 GROUP BY 中的列的非聚合列 '(db_name).a1_.slug'条款;这与 sql_mode=only_full_group_by 不兼容
所以我尝试添加以下内容:
->leftJoin('WordPressBundle:TermRelationships', 'r', 'HAVING', 'p.id = r.objectId')
->groupBy('p.id')
->innerJoin('WordPressBundle:Terms', 't', 'WITH', 'r.termTaxonomyId = t.termId')
->groupBy('r.termTaxonomyId')
但这并没有帮助。
mySQL 文档建议更改模式或添加 group by 子句,但我不希望更改配置。
关于调整我的代码以通过后台兼容性消除此错误的最佳方法的任何建议?
谢谢。
【问题讨论】:
-
您可以尝试从
my.cnf文件中删除选项ONLY_FULL_GROUP_BY,如下所述:craftcms.stackexchange.com/questions/12084/…
标签: php mysql wordpress symfony doctrine-orm