1

I have one table name Blog bellow is it's structure.

FieldName       Type
Id              int(5)
blog_title      string(255)
blog_url        string(255)
blog_desc       text
image           string(255)
tags            string(255)
blog_created_at Date
blog_status     string(1)

i have created one form for this table in controller.

  public function addAction()
{
    $entity = new Blog();

    $form = $this->createFormBuilder()
        ->add('blog_title')
        ->add('blog_desc' ,'textarea')
        ->add('blog_url')
        ->add('image')
        ->add('tags')
        ->add('blog_status')
        ->getForm();

    $request =  $this->getRequest();
    if ($request->getMethod() == 'POST') 
    {
        $form->bindRequest($request);
        if ($form->isValid()) 
        {
            $em = $this->getDoctrine()->getManager();
             $em->persist($entity);
             $em->flush();
            return $this->redirect($this->generateUrl('admin_index'));
        }
      }
     return $this->render('AdminBlogBundle:default:add.html.twig',array(
        'form' => $form->createView(),
    )); 

}

I want to enter the value of this form to my database but it gives me error of

 An exception occurred while executing 'INSERT INTO Blog (blog_title, blog_desc,
  blog_url, image, tags, blog_created_at, blog_status) VALUES (?, ?, ?, ?, ?, ?, ?)'
  with params {"1":null,"2":null,"3":null,"4":null,"5":null,"6":null,"7":null}:

all the value which i am posting from the form am getting in object array of request which i m getting bye calling the print_r($request); but while binding it is giving me this error. so pls help me.

Viraj.S
  • 305
  • 1
  • 8
  • 18

3 Answers3

1

You have to pass your entity to the createFormBuilder method, else the form builder does not know, that it has to map the form fields to the entity. Like so:

// ...
$form = $this->createFormBuilder($entity)
// ...

It's all in the documentation: http://symfony.com/doc/current/book/forms.html#building-the-form

bekay
  • 1,800
  • 1
  • 12
  • 15
0

You have to pass the model object to the builder like this:

$form = $this->createFormBuilder()
    // ...
    ->setData($entity)
    ->getForm();

Or this:

$form = $this->createFormBuilder('form', $entity)
    // ...
    ->getForm();
Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
  • my question is while i submiting the form it's required all the field while inserting into mysql table. while i have only 6 field as blog_created_date always insert automatically. and currently i dont want to insert all 7 field to databas i just want to insert six field to database. and dont know how to do with symfony and doctrine – Viraj.S Mar 09 '13 at 11:36
  • Well, from the error you provided, nothing was getting to the database. So, my answer fixes that. – Elnur Abdurrakhimov Mar 09 '13 at 11:50
  • To solve the date field problem, set `$createdAt` in your entity to `new \DateTime` in its constructor. You won't have to provide the date in the form then. But Doctrine will pass it to the database — even if it's just `null`. – Elnur Abdurrakhimov Mar 09 '13 at 11:51
  • 2
    What is the problem *exactly*? – Elnur Abdurrakhimov Mar 09 '13 at 12:19
  • i have 7 field value in my database. and here we are setting only 6 fields value in my form so it's giving error – Viraj.S Mar 09 '13 at 12:28
  • Are you still talking about the error you posted first? This error occurs because symfony wants to write the null value in all of your sql columns, which is only possible when all your columns are nullable (which is not the case by default). And symfony wants to insert the null value in all of your columns because you don't bind your entity to the form builder... – bekay Mar 09 '13 at 12:59
0


Like this:- username: type: string length: 255 fixed: false nullable: false

kush
  • 1