Search code examples
symfony1symfony-1.4dql

how to insert values into database using ajax in symfony1.4


in my project i want to add comments to each post. For inserting comments i am using ajax. since INSERT statements are not allowed in DQL. How can i insert comments using ajax. any one please help me. thanks in advance..


Solution

  • Create a new comment object, fill it with values, then save it.

    $c = new Comment();
    $c->fromArray($request->getParameter("comment"));
    $c->save();
    

    This is the easiest way. But you should be using a form, so it's also validated:

    $f = new CommentForm();
    $f->bind($request->getParameter($f->getName()));
    if ($f->isValid()) {
      $f->save();
    }
    

    See the form framework documentation for more info.