I am having a bear of a time saving the simplest record from a model called ItemView:
if($this->save($this->data)) {
echo \'worked\';
} else {
echo
To debug Model->save() check the validation errors and the last sql query
$this->Model->save($data);
debug($this->Model->validationErrors); //show validationErrors
debug($this->Model->getDataSource()->getLog(false, false)); //show last sql query
What is the name of controller you are using?
Mismatching the name of controller and model also cause some errors. If you are using Posts controller then the Post
model should be used. If you are using Post
model inside other controller; say BlogsController
then the Post
model should be loaded like following:
<?php
class BlogsController extends AppController
{
public function index()
{
$this->loadModel("Post");
//Here I'm loading other model inside blogs controller
if($this->request->is('post'))
{
$this->Post->save($this->request->data);
}
}
}
?>
It may be, Validations returns false.. You can check like
$this->Model->save($this->data, false);
Put false and check whether now data inserts, if it is then it is validation error
What always gets me is that if I modify the actual table (usually by adding attributes to it), then you must flush your cache. Usually just deleting everything inside the following two folders does the trick for me:
tmp > cache > models
tmp > cache > persistent
Same problem here. I wasted 2 hours of my life.
Solution: I forget to this:
array('post', 'put')
In cakephp 3.x, you can debug during insert/update
if ($this->TableName->save($entity)) {
// success
} else {
// if not save, will show errors
debug($entity->errors());
}