I have the following code in cake php 2.1. I try to get a listing of unique string messages, all duplicates removed.
$this->loadModel('ErrorMessage');
$this->ErrorMessage->recursive = -1;
$error_messages = $this->ErrorMessage->find('list',
array(
'fields' => array('DISTINCT message'),
)
);
Which produces the following error:
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT
ErrorMessage. message
FROMintermatte
.error_messages
AS `ErrorMes' at line 1SQL Query: SELECT
ErrorMessage
.id
, DISTINCTErrorMessage. message
FROMintermatte
.error_messages
ASErrorMessage
WHERE 1 = 1
The interesting part is in the query produced; Cake has included the id field automatically!
I test query alone, it produces the expected result: (list of unique strings)
SELECT DISTINCT message FROM error_messages;
Here is the create table for reference:
CREATE TABLE `error_messages` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`message` MEDIUMTEXT NULL COLLATE 'utf8_unicode_ci',
`figure` VARCHAR(50) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
`created` DATETIME NULL DEFAULT NULL,
`modified` DATETIME NULL DEFAULT NULL,
PRIMARY KEY (`id`),
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT;
So the question is, why does cake include the id field automatically, and how can I tell cake not to do it?
I can of course use a query, but that is not 'nice'. ;)