0

I'm trying to populate form from database by using two entities. Here's the code I use

CategoryType

<?php

namespace AppBundle\Form;

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

    class CategoryType extends AbstractType
    {
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('name', 'text');
        }


       public function configureOptions(OptionsResolver $options)
        {
            return array(
                'data_class' => 'AppBundle\Entity\Category',
            );
        }

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

ProductType

<?php

namespace AppBundle\Form;

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

class ProductType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');
    }

    public function configureOptions(OptionsResolver $options)
    {
        return array(
            'data_class' => 'AppBundle\Entity\Product',
        );
    }

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

CommonType

<?php

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use AppBundle\Form\CategoryType;
use AppBundle\Form\ProductType;


class CommonType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('category', new CategoryType());
        $builder->add('product', new ProductType());
    }

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

DefaultController

<?php

namespace AppBundle\Controller;

use AppBundle\Form\CommonType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        $data = $this->getDoctrine()->getRepository('AppBundle:Product')->findOneBy(['id' => 2]);
        $form = $this->createForm(new CommonType(), $data);

        return $this->render('AppBundle:Default:index.html.twig', ['form' => $form->createView()]);

    }
}

But I get this error

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class Proxies\__CG__\AppBundle\Entity\Category. You can avoid this error by setting the "data_class" option to "Proxies\__CG__\AppBundle\Entity\Category" or by adding a view transformer that transforms an instance of class Proxies\__CG__\AppBundle\Entity\Category to scalar, array or an instance of \ArrayAccess.

So what I'm doing wrong and is this the correct way to populate form based on two or more entities?

1 Answer 1

1

Your Common form's model data format is an array (as far as no data_class given). So the right data to set is an array with product and category keys:

    $product = $this->getDoctrine()->getRepository('AppBundle:Product')->findOneBy(['id' => 2]);
    $form = $this->createForm(new CommonType(), ['product' => $product]);
8
  • I changed the code as you suggested but now I get this error The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class AppBundle\Entity\Product. You can avoid this error by setting the "data_class" option to "AppBundle\Entity\Product" or by adding a view transformer that transforms an instance of class AppBundle\Entity\Product to scalar, array or an instance of \ArrayAccess.
    – Vodokan
    Commented Nov 23, 2015 at 10:34
  • Here's the dump of the data DefaultController.php on line 20: Product {#555 ▼ #id: 2 #name: "second" #price: "12.00" #description: "testing" #category: Category {#572 ▼ +__isInitialized__: false -id: 2 -name: null #products: null …2 } }
    – Vodokan
    Commented Nov 23, 2015 at 10:35
  • What version of Symfony do u use? getDefaultOptions is deprecated in modern builds, configureOptions or at least setDefaultOptions preferred.
    – Ziumin
    Commented Nov 23, 2015 at 10:39
  • yeah, getDefaultOptions is useless then.
    – Ziumin
    Commented Nov 23, 2015 at 11:58
  • I changed with configureOptions but got the same error
    – Vodokan
    Commented Nov 23, 2015 at 11:58

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.