1

I am new to Symfony2.0. I am learning it from the site http://symfony.com/doc/2.0/book/index.html. I've gone through all the topics given in the site but i am not getting output for the topic 'Embedding a Single Object',which is included within the Topic of Forms (http://symfony.com/doc/2.0/book/forms.html). I wrote all the code as they have given for 'Embedding a Single Object' but it does not make an entry to the database. When i submit my form it gives me error i stated below. I can make an entry without using embedding object code.

Error : "Catchable Fatal Error: Argument 1 passed to Acme\TaskBundle\Entity\Task::setCategory() must be an instance of Acme\TaskBundle\Entity\Category, array given, called in /opt/lampp/htdocs/kau.symfony2.com/vendor/symfony/src/Symfony/Component/Form/Util/PropertyPath.php on line 347 and defined in /opt/lampp/htdocs/kau.symfony2.com/src/Acme/TaskBundle/Entity/Task.php line 52"

I searched a lot but could't find the solution.

FilePath: Acme/TaskBundle/Form/Type/TaskType.php

<?php
// src/Acme/TaskBundle/Form/Type/TaskType.php
namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class TaskType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('task', 'text', array('max_length' => 20));
        $builder->add('address', 'textarea', array('required' => true, 'read_only' => false));
        $builder->add('dueDate', 'date');
        $builder->add('category', new CategoryType());    
    }

    public function getName()
    {
        return 'task';
    }
}
?>

FilePath : Acme/TaskBundle/Resources/Views/Default/new.html.twig

<form action="{{ path('AcmeTaskBundle_newpage') }}" method="post" {{ form_enctype(form) }} novalidate>
    {{ form_errors(form) }}

<div>
    {{ form_errors(form.task) }}
    {{ form_label(form.task, 'Task Description', { 'label_attr': {'class': 'task_field' }}) }}
    {{ form_widget(form.task, { 'attr': {'class': 'task_field'} }) }}
</div>

<div>
    {{ form_errors(form.address) }}
    {{ form_label(form.address) }}
    {{ form_widget(form.address) }}
</div>

<div>
    {{ form_errors(form.dueDate) }}
    {{ form_label(form.dueDate) }}
    {{ form_widget(form.dueDate) }}
</div>

<h3>Category</h3>
<div class="category">
    {{ form_row(form.category.name) }}
</div>

    {{ form_rest(form) }}

    <input type="submit" />
</form>

FilePath: Acme/TaskBundle/Form/Type/CategoryType.php

<?php
namespace Acme\TaskBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\TaskBundle\Entity\Category',
        ));
    }

    public function getName()
    {
        return 'category';
    }
}
?>
Kaushal Bhatt
  • 346
  • 1
  • 10

1 Answers1

0

Can u show your code ? Coz that error telling u that u use array - when you should use instance of object of Category

// show /Acme/TaskBundle/Entity/Task.php plz :)

and try to change :

$builder->add('category', new CategoryType());

to

$builder->add('category', 'collection', array('type' => new CategoryType));
sl4sh
  • 61
  • 2
  • 6
  • Thanks for the reply sl4sh. I have edited the question and added code files as you said. – Kaushal Bhatt Jan 04 '13 at 08:19
  • I have done the changes as you described but it gives error : 'Method "name" for object "Symfony\Component\Form\FormView" does not exist in AcmeTaskBundle:Default:new.html.twig at line 24 '..Error occurs at : {{ form_row(form.category.name) }} in new.html.twig file. – Kaushal Bhatt Jan 04 '13 at 10:19
  • Please be noted that i have included 4 lines (From line 22 to 25) in to new.html.twig file.

    Category

    {{ form_row(form.category.name) }}
    – Kaushal Bhatt Jan 04 '13 at 10:42