0

I try to update the table posts using the updateAll command. But i got an error. I have wot tables namely users and posts. My controller is,

<?php
App::uses('AppModel', 'Model');
class Post extends AppModel {
public $belongsTo = array(
    'User' => array(
        'className'    => 'User',
        'foreignKey'   => 'user_id',
        'fields'    => "User.id,User.name,User.uname"
    )
);
function updatePost($title,$content,$pid){
    $this->updateAll(
        array('Post.title' => $title,'Post.content'=>$content),
        array('Post.id' => $pid)
    );
}
}

?>

The error message is, "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 'Post 2, Post.content =

Models are the classes that sit as the business la' at line 1"

"SQL Query: UPDATE blog.posts AS Post LEFT JOIN blog.users AS User ON (Post.user_id = User.id) SET Post.title = test_title, Post.content = sample body WHERE Post.id = 2 "; How to correct it ?

4
  • 1. User has many Post that relationship is there in UserModel file ??? 2. Check that user_id exists in user table ?
    – AnNaMaLaI
    Apr 29, 2013 at 9:14
  • yes id exist in user table not user_id, posts.user_id=users.id
    – Vinod VT
    Apr 29, 2013 at 9:16
  • thats good... change ur code like this and check $this->updateAll( array('Post.titile' => '\'' .$title. '\'', 'Post.content'=>'\'' .$content. '\''), array('Post.id' => $pid) );
    – AnNaMaLaI
    Apr 29, 2013 at 9:20
  • The problem is caused because you're using updateAll() which doesn't escape/quote values, see my answer for more info. You're probably looking for Model::save()
    – thaJeztah
    Apr 29, 2013 at 9:27

2 Answers 2

2
Try...
$this->updateAll(
        array('Post.title' => "'$title'",'Post.content' => "'$content'"),
        array('Post.id' => $pid)
    );
0
1

First of all, you didn't include an actual error message? I can only see a query?

However, I do see a few issues;

  • It looks like you've got a typo; Post.titile looks like a typo for Post.title

  • Fields in CakePHP should be specified as an array, not a comma-separated string, so

        'fields'    => array("User.id", "User.name", "User.uname")
    

But again, please show the actual error message and/or what you're expecting and what you got instead

Additionally, as mentioned in the documentation, fields in updateAll() accept SQL expressions, so should be quoted manually. See Model::updateAll()

Tip The $fields array accepts SQL expressions. Literal values should be quoted manually using >Sanitize::escape().

Because you seem to be updating a single post, seems to me you're looking for Model::save() in stead?

See: Saving your data

update

To do this with Model::save() rewrite your updatePost method to something like this;

function updatePost($title,$content,$pid){
    $this->save(
        array(
            'Post' => array(
                'id' => $pid,
                'title' => $title,
                'content'=> $content
            )
        )
    );
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.