下面的代码工作正常但是当我用相同的DB复制整个cakephp3项目时,我得到以下错误。由于相同的代码在其他地方工作,我不确定如何解决这个问题。我正在使用最新的Wamp64,win10。 cakephp3项目的其余部分工作正常。另一台计算机使用了wamp64和win7。
有解决方案,但这涉及更改数据库设置,我更喜欢更改代码。
Error related to only_full_group_by when executing a query in MySql
错误:SQLSTATE [42000]:语法错误或访问冲突:1055 SELECT列表的表达式#11不在GROUP BY子句中,并且包含非聚合列'aptutori_test.Lessons.id',它在功能上不依赖于GROUP BY中的列条款;这与sql_mode = only_full_group_by
不兼容 $options['fields'] = array('DISTINCT Student.* ','Guardian.* ');
$options['conditions'] = array('Lessons.tutor_id' => $tutorId,'Student.student_inactive'=>0,
'Lessons.lesson_date >='=> $currDate, 'Lessons.makeup_lesson' => 0,'Lessons.forefit' => 0);
$fields = [
'Student.first_name','Student.last_name','Student.id',"Student.address_street","Student.address_suburb","Student.address_postcode","Student.class_year",
"Guardian.id",'Guardian.guardian_first_name',"Guardian.guardian_last_name"
];
$query = $this->Lessons->find('all');
$students = $query->where($options['conditions'])->autoFields(true)
->select($fields)->distinct(["Student.id",'Guardian.id'])
->join($options['joins'])
;
$this->set( 'student',$students);
答案 0 :(得分:0)
以下是可以在不更改数据库设置的情况下解决问题的代码。
$students = $this->Lessons->find()
->contain([
'Students',
'Students.Guardians'])
->select([
'Students.last_name',
'Students.first_name',
'Students.id',
'Lessons.student_id',
'Lessons.tutor_id',
'Students.has_credit',
'Lessons.student_id',
'Lessons.id',
'Students.class_year',
'Lessons.subject_id',
'Guardians.guardian_last_name',
'Guardians.guardian_first_name',
'Students.address_street',
'Students.address_suburb',
'Students.address_postcode',
'Guardians.id'])
->where([
'Lessons.tutor_id' => $tutorId,
'Students.student_inactive' => 0,
'Lessons.lesson_date >=' => $currDate,
'Lessons.cancelled_lesson' => 0,
'Lessons.forefit' => 0])
->order([
'Lessons.lesson_date' => 'ASC',
'Lessons.start_time' => 'ASC'])
->hydrate(true);
$gdata = array();
$studentIds = array();
foreach ($students as $i => $item) {
if (in_array($item['student']['id'], $studentIds)) {
continue;
} else {
array_push($studentIds,$item['student']['id']);
$gdata[$i]['student']['id'] = $item['student']['id'];
$gdata[$i]['student_id'] = $item['student_id'];
$gdata[$i]['student']['first_name'] = $item['student']['first_name'];
$gdata[$i]['student']['last_name'] = $item['student']['last_name'];
$gdata[$i]['student']['guardian']['guardian_first_name'] = $item['student']['guardian']['guardian_first_name'];
$gdata[$i]['student']['guardian']['guardian_last_name'] = $item['student']['guardian']['guardian_last_name'];
$gdata[$i]['student']['address_street'] = $item['student']['address_street'];
$gdata[$i]['student']['address_suburb'] = $item['student']['address_suburb'];
$gdata[$i]['student']['address_postcode'] = $item['student']['address_postcode'];
$gdata[$i]['student']['class_year'] = $item['student']['class_year'];
}
}