Hello everyone,

Im experiencing troubles getting following model working:


User.php
[code]
<?php
namespace ALOGI\SportPort\UsersBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var integer $id
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $login;

    /**
     * @ORM\Column(type="text")
     */
    protected $description;

    /**
     * @ORM\ManyToMany(targetEntity="Role")
     * @ORM\JoinTable(name="user_role",
     *     joinColumns={@ORM\JoinColumn(name="user_id",
referencedColumnName="id")},
     *     inverseJoinColumns={@ORM\JoinColumn(name="role_id",
referencedColumnName="id")}
     * )
     *
     * @var ArrayCollection $roles
     */
    protected $roles;

    public function __construct()
    {
        $this->roles = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set login
     *
     * @param string $login
     */
    public function setLogin($login)
    {
        $this->login = $login;
    }

    /**
     * Get login
     *
     * @return string
     */
    public function getLogin()
    {
        return $this->login;
    }

    /**
     * Set description
     *
     * @param text $description
     */
    public function setDescription($description)
    {
        $this->description = $description;
    }

    /**
     * Get description
     *
     * @return text
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Add roles
     *
     * @param ALOGI\SportPort\UsersBundle\Entity\Role $roles
     */
    public function addRoles(\ALOGI\SportPort\UsersBundle\Entity\Role
$roles)
    {
        $this->roles[] = $roles;
    }

    /**
     * Get roles
     *
     * @return Doctrine\Common\Collections\Collection
     */
    public function getRoles()
    {
        return $this->roles;
    }
}
[/code]


Role.php
[code]
<?php
namespace ALOGI\SportPort\UsersBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="role")
 */
class Role
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @var integer $id
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length="255")
     *
     * @var string $name
     */
    protected $name;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    public function __toString()
    {
        return $this->getName();
    }
}
[/code]

RegisterController.php
[code]
<?php

namespace ALOGI\SportPort\UsersBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use ALOGI\SportPort\UsersBundle\Entity\User;
use ALOGI\SportPort\UsersBundle\Entity\Role;

class RegisterController extends Controller
{

    public function indexAction($name)
    {
            $em = $this->getDoctrine()->getEntityManager();

            $user = new User();

            $user->setLogin('hubert2');
            $user->setDescription('moj user');

            $role1 = new Role();
            $role1->setName('ROLE_ADMIN');
            $em->persist($role1);

            $user->addRoles($role1);
            $em->persist($user);



            $form = $this->createFormBuilder($user)
            ->add('login', 'text')
            ->add('description', 'textarea')
            //->add('roles','entity',array('em'=>$em,'multiple'=>true,
'property'=>'name', 'expanded'=>false, 'class'=>'ALOGI\\SportPort\
\UsersBundle\\Entity\\Role'))
            ->getForm();


        return $this-
>render('ALOGISportPortUsersBundle:Register:index.html.twig',
array('name' => $name, 'form' => $form->createView()));
    }
}

[/code]


index.html.twig
[code]
<p>Hello {{ name }}!</p>
<p>
<form action="{{ path('index') }}"
method="post" {{ form_enctype(form) }}>
    {{ form_widget(form) }}

    <input type="submit" />
</form>
</p>
[/code]

When I uncomment
//->add('roles','entity',array('em'=>$em,'multiple'=>true,
'property'=>'name', 'expanded'=>false, 'class'=>'ALOGI\\SportPort\
\UsersBundle\\Entity\\Role'))
page fails with:
Warning: Illegal offset type in isset or empty in /opt/devel/apache/
Symfony/vendor/symfony/src/Symfony/Bundle/DoctrineBundle/Registry.php
line 120
Stack Trace
[code]
    in /opt/devel/apache/Symfony/vendor/symfony/src/Symfony/Component/
HttpKernel/Debug/ErrorHandler.php at line 65  -+
                }
                if (error_reporting() & $level && $this->level &
$level) {
                    throw new \ErrorException(sprintf('%s: %s in %s
line %d', isset($this->levels[$level]) ? $this->levels[$level] :
$level, $message, $file, $line));
                }
                return false;
    at ErrorHandler ->handle ('2', 'Illegal offset type in isset or
empty', '/opt/devel/apache/Symfony/vendor/symfony/src/Symfony/Bundle/
DoctrineBundle/Registry.php', '120', array('name' =>
object(EntityManager)))
    in /opt/devel/apache/Symfony/vendor/symfony/src/Symfony/Bundle/
DoctrineBundle/Registry.php at line 120  -+
                    $name = $this->defaultEntityManager;
                }
                if (!isset($this->entityManagers[$name])) {
                    throw new
\InvalidArgumentException(sprintf('Doctrine EntityManager named "%s"
does not exist.', $name));
                }
    at Registry ->getEntityManager (object(EntityManager))
    in /opt/devel/apache/Symfony/vendor/symfony/src/Symfony/Bridge/
Doctrine/Form/Type/EntityType.php at line 58  -+
                if (!isset($options['choice_list'])) {
                    $defaultOptions['choice_list'] = new
EntityChoiceList(
                        $this->registry-
>getEntityManager($options['em']),
                        $options['class'],
                        $options['property'],
                        $options['query_builder'],
    at EntityType ->getDefaultOptions (array('em' =>
object(EntityManager), 'multiple' => true, 'property' => 'name',
'expanded' => false, 'class' => 'ALOGI\SportPort\UsersBundle\Entity
\Role', 'data' => null))
    in /opt/devel/apache/Symfony/vendor/symfony/src/Symfony/Component/
Form/FormFactory.php at line 228  -+
                        $type = $this->getType($type);
                    }
                    $defaultOptions = $type-
>getDefaultOptions($options);
                    $optionValues =
array_merge_recursive($optionValues, $type-
>getAllowedOptionValues($options));
                    foreach ($type->getExtensions() as $typeExtension)
{
[/code]
What I'm doing wrong?
I'm new to symfony, so maybe someone will show me how the way to
display multiple select field based on Entity content ?

-- 
If you want to report a vulnerability issue on symfony, please send it to 
security at symfony-project.com

You received this message because you are subscribed to the Google
Groups "symfony developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-devs?hl=en

Reply via email to