StrikoMirko
StrikoMirko

Reputation: 415

I am unable to save a value from my selectbox to database

I am building a project in Symfony 2 standard edition.

I made a form to insert my Disease Entity, I have 2 selectboxes in that form, one is a ManyToOne field called group linked to the id of another table called Groups and another is a parent linked to the same Disease table.

Selectbox group works fine and sends a normal variable but the parent selectbox doesent seem to send anything here is the error code

An exception occurred while executing 
'INSERT INTO disease (parent, name, latin_name, code, notice, modified, group_id) VALUES (?, ?, ?, ?, ?, ?, ?)' 
with params {"1":{},"2":"Alamanja","3":"mirkus","4":"A011","5":"sad sada","6":"2012-01-01 00:00:00","7":"1"}:

Catchable Fatal Error: Object of class Acme\BlogBundle\Entity\Disease could not be converted to string in D:\xampp\htdocs\Symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 138

My Form object looks like this

namespace Acme\BlogBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class DiseaseType extends AbstractType
{
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Acme\BlogBundle\Entity\Disease',
    ));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder->add('name');
    $builder->add('latinName');
    $builder->add('code');
    $builder->add('notice', 'textarea');
    $builder->add('parent', 'entity', array(
        'class' => 'AcmeBlogBundle:Disease',
        'property' => 'name',
        'empty_value' => '--Izaberi grupu--',
    ));
    $builder->add('group', 'entity', array(
        'class' => 'AcmeBlogBundle:Groups',
        'query_builder' => function($repository) { return $repository->createQueryBuilder('p')->orderBy('p.id', 'ASC'); },
        'property' => 'name',
    ));
    $builder->add('modified', null, array('widget' => 'single_text'));
}

public function getName()
{
    return 'disease';
}
}

I am calling my form object from my controller

namespace Acme\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\BlogBundle\Entity\Groups;
use Acme\BlogBundle\Entity\Disease;
use Symfony\Component\HttpFoundation\Request;
use Acme\BlogBundle\Form\Type\BlogType;
use Acme\BlogBundle\Form\Type\DiseaseType;

class DiseaseController extends Controller
{

public function newAction(Request $request)
{
    // create a task and give it some dummy data for this example
    $disease = new Disease();    
    $form = $this->createForm(new DiseaseType(), $disease);

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($disease);
            $em->flush();

            return $this->redirect($this->generateUrl('disease_show'));
        }
    }else{
        return $this->render('AcmeBlogBundle:Default:newDisease.html.twig', array(
        'form' => $form->createView(),
        ));
    }
}
}

Please help me, I've searched everywhere but nothing :(.

Upvotes: 1

Views: 554

Answers (1)

Mick
Mick

Reputation: 31919

Catchable Fatal Error: Object of class Acme\BlogBundle\Entity\Disease 
could not be converted to string.

The $parent property is declared as a string in your entity and as an entity in your form.


To resolve this, you have 2 options :

Option 1

You create a OneToOne relationship, self-referencing.

/**
 * @OneToOne(targetEntity="Acme\BlogBundle\Entity\Disease")
 * @JoinColumn(name="disease_id", referencedColumnName="id")
 */
private $parent;

Option 2

You create a data transformer for the parent property to convert the disease entity into a string.


Which option you decide to take depends on what you need in your $parent property, if you need a string go to option 2, if you need to store the entire entity go with option 1. A bit hard to understand why you would need to do that though..

Upvotes: 1

Related Questions