Reputation: 5395
In a controller method I have the following code:
$this->loadModel('Navigation');
$this->Navigation->updateAll(array('Navigation.name' => $this->request->data['Tag']['name']), array('Navigation.tag_id' => $this->request->data['Tag']['id']));
It's producing the following error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '
Germany' in 'field list'
SQL Query: UPDATE `dev_hub_subdb_v5_interface`.`navigations` AS
`Navigation` SET `Navigation`.`name` = Germany WHERE
`Navigation`.`tag_id` = 3
The reason appears to be that it's not treating "Germany" as a string when trying to write it to a column called name
which is a varchar(255)
field in MySQL.
However, the data is in string format. If I var_dump
the request data that's about to be saved, I get this:
array(2) { ["Save"]=> string(6) "Update" ["Tag"]=> array(2) { ["id"]=> string(1) "3" ["name"]=> string(7) "Germany" } }
Why doesn't this work? According to the documentation updateAll
accepts 2 arrays: the data to be saved and the conditions. So I can't see how else to write this.
Upvotes: 0
Views: 685
Reputation: 5395
So it appears to be the case that you have to quote strings, even if they are strings...
The documentation I referred to above has a rather poor explanation/example but says:
The
$fields
array accepts SQL expressions. Literal values should be quoted manually usingDboSource::value()
. For example if one of your model methods was callingupdateAll()
you would do the following:
$db = $this->getDataSource();
$value = $db->value($value, 'string');
$this->updateAll(
array('Baker.status' => $value),
array('Baker.status' => 'old')
);
I've no idea what that example means as they appear to be updating Baker.status
twice, with strings. Clear as mud!
Anyway, in my case, adding a quote around $this->request->data['Tag']['name']
fixed it:
$this->Navigation->updateAll(array('name' => "'".$this->request->data['Tag']['name']."'"), array('tag_id' => $this->request->data['Tag']['id']));
Upvotes: 1