Symfony Forms Neither the property "Category" nor one of the methods "getCategory()" error

1.4k Views Asked by At

I keep getting the following error:

Neither the property "Category" nor one of the methods "getCategory()", "category()", "isCategory()", "hasCategory()", "__get()" exist and have public access in class "Test\TesterBundle\Model\Products"

I've got 3 tables: Products, Category and ProductCategories. The ProductCategories is the ID of the Producs and Category Table (Many to Many). But I keep getting the error above.

I've got the following form builders:

class ProductsType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('ProductName');
        $builder->add('ProductDescription', 'textarea', array("required"=> false));
        $builder->add('ShortDescription', null);
        $builder->add('SKU', null);
        $builder->add('UnitWeight', null);
        $builder->add('UnitPrice', null);
        $builder->add('UnitLength', null);
        $builder->add('UnitHeight', null);
        $builder->add('UnitDepth', null);
        $builder->add('URL', null);
        $builder->add('MetaTitle', null);
        $builder->add('MetaDescription', null);
        $builder->add('MetaKeywords', null);
        $builder->add('Category', 'collection', array(
                        'type' => new \Test\TesterBundle\Form\Type\CategoryType(),
                        'allow_add' => true,
                        'allow_delete' => true,
                        'by_reference' => false, 

                    ));

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'validation_groups' => array('newProducts'),
            'data_class' => 'Test\TesterBundle\Model\Products',
        ));
    }

    public function getName(){
        return "Products";
    } 


}

And The second being:

class CategoryType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('CategoryName', 'model', array(
                        'class' => 'Test\TesterBundle\Model\Productcategory',
                        'required' => false,
                        'multiple' => true,
                        'expanded' => false,
                        'property' => 'label'
                    ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'Test\TesterBundle\Model\Productcategory'
        ));
    }

    public function getName(){
        return "ProductCategory";
    }   


}

Any thoughts on what would be causing the issue? I'm not sure if it's the instance that is used in the controller or if it is because I've setup the form builders incorrectly and cannot get a direct relation to the Category field (which it won't). If I use mapped => false, then it works however the builder has to populate the select box from the database, which settings mapped to false stops it doing this.

0

There are 0 best solutions below