Question

when i am update my form, i got this error 500 | Internal Server Error | Doctrine_Connection_Mysql_Exception

executeEdit

public function executeEdit(sfWebRequest $request)
{
    $this->form = new ContactForm();

    $this->rs = Doctrine::getTable('login')-> find($request->getParameter('id'));

    $id=$request->getParameter('id');
    $unm=$this->rs['username'];
    $msg=$this->rs['message'];
    $em=$this->rs['email'];
    $sub=$this->rs['subject'];
    $this->form->setDefault('id', $id);
    $this->form->setDefault('username', $unm);
    $this->form->setDefault('message', $msg);
    $this->form->setDefault('email', $em);
    $this->form->setDefault('subject', $sub);
    //$this->forward404Unless($this->rs);   


    if ($request->isMethod('post'))
    {
        $this->form->bind($request->getParameter('contact'), $request->getFiles('contact'));

        if ($this->form->isValid())
        {
            $name="'".$this->form->getValue('username')."'";
            $message="'".$this->form->getValue('message')."'";
            $email="'".$this->form->getValue('email')."'";
            $sub="'".$this->form->getValue('subject')."'";
            $id=$this->form->getValue('id');

            $file = $this->form->getValue('doc');
            $filename = sha1($unm).'_'.sha1($file->getOriginalName());
            $extension = $file->getExtension($file->getOriginalExtension());
            $path=sfConfig::get('sf_upload_dir').'/'.$filename.$extension;
            $qs= Doctrine_Query::create()
                ->update('login l')
                ->set('l.username', $name)
                ->set('l.message', $message)
                ->set('l.email', $email)
                ->set('l.subject', $sub)
                ->set('l.doc', $path)
                ->where('l.id=?',$id)
                ->execute();
                $this->redirect('user/show?id=' . $id);
        }
    }
}

this is my edit action code. so whats the exaclty error. and what is my mistake and what can i do to solve this error? please help me

Was it helpful?

Solution

Why do you add quotes to the values you are saving? Doctrine will handle this for you. Try removing this part:

$name="'".$this->form->getValue('username')."'";
$message="'".$this->form->getValue('message')."'";
$email="'".$this->form->getValue('email')."'";
$sub="'".$this->form->getValue('subject')."'";

You're not utilising the power of Doctrine and forms fully.

E.g. instead of this:

$this->form = new ContactForm();

$this->rs = Doctrine::getTable('login')-> find($request->getParameter('id'));

$id=$request->getParameter('id');
$unm=$this->rs['username'];
$msg=$this->rs['message'];
$em=$this->rs['email'];
$sub=$this->rs['subject'];
$this->form->setDefault('id', $id);
$this->form->setDefault('username', $unm);
$this->form->setDefault('message', $msg);
$this->form->setDefault('email', $em);
$this->form->setDefault('subject', $sub);

You could use this:

$this->rs =  Doctrine_Core::getTable('login')-> find($request->getParameter('id'));
$this->form = new ContactForm($this->rs);

You only have to "connect" your form to a proper model by adding this function:

public function getModelName()
{
    return 'login';
}    

You could then use the save method on the form so it automatically saves your object without having to create the update query.

Also have a look at the sfValidatorFile as it will automatically create a unique filename for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top