注意:未定义的索引:Symfony 2.7中的角色

时间:2016-07-08 18:46:21

标签: symfony orm

我正在尝试学习Symfony2,目前:实体关系/关联(加入相关记录)。我有一个问题我无法正确

  

注意:未定义的索引:角色500内部服务器错误 -   ContextErrorException

这是我的代码:

*班级角色:

class Roles
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @var string
     *
     * @ORM\Column(name="nom_roles", type="string", length=225, nullable=false)
     *@Assert\NotBlank(message="le champ nom du role est obligatoire")
     *
     */
    Private $name_role;

    /**
     * @var ArrayCollection $groups
     *
     * @ORM\OneToMany(targetEntity="Groups", mappedBy="roles", cascade={"persist","merge"})
     *@Assert\Valid()
     */
    protected $groups;  
    /**
     * @var ArrayCollection Permissions $permision
     *
     * @ORM\ManyToMany(targetEntity="Permissions", inversedBy="roleGroup", cascade={"persist", "merge"})
     * @ORM\JoinTable(name="roles_permissions",
     *   joinColumns={@ORM\JoinColumn(name="id_rolesGR", referencedColumnName="id")},
     *   inverseJoinColumns={@ORM\JoinColumn(name="id_permGR", referencedColumnName="id_per")}
     * )
     * @Assert\Valid()
     */
    protected $permissions_role;

    /**
     * Roles constructor.
     */
    public function __construct()
    {
        $this->groups = new ArrayCollection();
        $this->permissions_role = new ArrayCollection();
    }

- 类权限:

class Permissions {

/**
 * @var integer
 *
 * @ORM\Column(name="id_per", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 * @ORM\Column(name="nom_permisions", length=255, nullable=true)
 * @Assert\NotBlank(message="le choix des permissions sont obligatoire")
 */
private $name_permissions;


/**
 * @var ArrayCollection Roles $roleGroup
 *
 * Inverse Side
 *
 * @ORM\ManyToMany(targetEntity="Roles", mappedBy="permissions", cascade={"persist", "merge"})
 *@Assert\Valid()
 */
protected $roleGroup;

-Class Groups:

class Groups
{

/**
 * @var integer
 *
 * @ORM\Column(name="id_groupes", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * 
 */
private $id;
/**
 * @var string
 *
 *-- contrainte sur le chmap
 *
 * @Assert\NotBlank(message="le champ nom du groupe est obligatoire")
 *
 * @Assert\Type(
 *     type="string",
 *     message="la valeur {{ value }} n'est pas valide {{ type }}.
 *     Elle est de type chaine des cractéres"
 * )
 *
 * @Assert\Length(
 *     min=5,
 *     max= 50,
 *     minMessage="votre nom du groupe doit comprendre au moins {{ limit }} caractéres",
 *     maxMessage="votre nom du groupe doit comprendre au maximun {{ limit }} caractéres"
 * )
 *
 * @ORM\Column(name="nom_groupe", type="string", length=225, nullable=false)
 *@Assert\NotBlank(message="le champ nom du groupe est obligatoire")
 *
 *
 */
 Private $name_groups;

/**
 * @var DateTime()
 *
 * @ORM\Column(name="date_creation", type="datetime",nullable=false)
 *
 *@Assert\DateTime()
 */
 private $date_create;

/**
 * @ORM\OneToOne(targetEntity="Images", cascade={"persist", "merge", "remove"})
 * @ORM\JoinColumn(name="image_id", referencedColumnName="id_images")
 * @Assert\Valid()
 */
protected $image;
/**
 * @var Roles $role
 *
 * @ORM\ManyToOne(targetEntity="Roles", inversedBy="groups", cascade={"persist", "merge"})
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="role_id", referencedColumnName="id", nullable=false)
 * })
 * @Assert\Valid()
 */
protected $role;

- 我的控制器

class GroupsController extends Controller

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

    $entities = $em->getRepository('GroupsBundle:Groups')->findAll();

    return $this->render('GroupsBundle:Groups:index.html.twig', array(
        'entities' => $entities,
    ));
}
/**
 * Creates a new Groups entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new Groups();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

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

        return $this->redirect($this->generateUrl('groups_show', array('id' => $entity->getId())));
    }

    return $this->render('GroupsBundle:Groups:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

/**
 * Creates a form to create a Groups entity.
 *
 * @param Groups $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(Groups $entity)
{
    $form = $this->createForm(new GroupsType(), $entity, array(
        'action' => $this->generateUrl('groups_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

/**
 * Displays a form to create a new Groups entity.
 *
 */
public function newAction()
{
    $entity = new Groups();
    $form   = $this->createCreateForm($entity);

    return $this->render('GroupsBundle:Groups:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

/**
 * Finds and displays a Groups entity.
 *
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('GroupsBundle:Groups')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Groups entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return $this->render('GroupsBundle:Groups:show.html.twig', array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Displays a form to edit an existing Groups entity.
 *
 */
public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('GroupsBundle:Groups')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Groups entity.');
    }

    $editForm = $this->createEditForm($entity);
    $deleteForm = $this->createDeleteForm($id);

    return $this->render('GroupsBundle:Groups:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
* Creates a form to edit a Groups entity.
*
* @param Groups $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Groups $entity)
{
    $form = $this->createForm(new GroupsType(), $entity, array(
        'action' => $this->generateUrl('groups_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}
/**
 * Edits an existing Groups entity.
 *
 */
public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('GroupsBundle:Groups')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Groups entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $em->flush();

        return $this->redirect($this->generateUrl('groups_edit', array('id' => $id)));
    }

    return $this->render('GroupsBundle:Groups:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}
/**
 * Deletes a Groups entity.
 *
 */
public function deleteAction(Request $request, $id)
{
    $form = $this->createDeleteForm($id);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('GroupsBundle:Groups')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Groups entity.');
        }

        $em->remove($entity);
        $em->flush();
    }

    return $this->redirect($this->generateUrl('groups'));
}

/**
 * Creates a form to delete a Groups entity by id.
 *
 * @param mixed $id The entity id
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm($id)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('groups_delete', array('id' => $id)))
        ->setMethod('DELETE')
        ->add('submit', 'submit', array('label' => 'Delete'))
        ->getForm()
    ;
}

}

问题:

  

捕获致命错误:参数1传递给   Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()必须是   类型数组,给定对象,调用   /home/cros/Desktop/Project_Console/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php   在555行并定义了500内部服务器错误 -   ContextErrorException

* Stack Trace:

第48行的vendor / doctrine / collections / lib / Doctrine / Common / Collections / ArrayCollection.php中的

-

 /*
 * @param array $elements
 */
public function __construct(array $elements = array())
{
    $this->elements = $elements;
}
  

在ErrorHandler - > handleError(' 4096','参数1传递给   Doctrine \ Common \ Collections \ ArrayCollection :: __ construct()必须是   类型数组,给定对象,调用   /home/cros/Desktop/Project_Console/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php   在第555行并定义了',   ' /home/Cros/Desktop/Project_Console/vendor/doctrine/collections/lib/Doctrine/Common/Collections/ArrayCollection.php',

我的GroupsType和RolesType:

  class GroupsType extends AbstractType
    {
        /**
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('image',new ImagesType())
                ->add('name_groups', 'text',array('required' => true, 'attr' => array('placeholder' => 'Nom du groupe')))
                ->add('role', new RolesType())
            ;
        }
    }

//My RolesType:
class RolesType extends AbstractType
    {  
public function buildForm(FormBuilderInterface $builder, array $options)
        {

            $builder
               ->add('groups', 'entity',array(
                                                'class' => 'GroupsBundle:Roles',
                                                'choice_label' => 'name_role',
                                                /*'query_builder' => function(EntityRepository $er)
                                                {
                                                 return $er->createQueryBuilder('r')
                                                 ->orderBy('r.id', 'ASC');
                                                },*/
                                                 'required' => true,
                                                 'placeholder' => 'Choisir le role du votre groupe'
                                             )
               )

               ->add('permissions_role','entity',array(
                       'class' => 'GroupsBundle:Permissions',
                       'multiple' => true,
                       'expanded' => true,
                       'query_builder' => function(EntityRepository $er) {
                           return $er->createQueryBuilder('u')
                               ->orderBy('u.id', 'ASC');
                       },
                       'required' => true
                   )
                   )
                // ->add('permissions_role','number')

            ;

谢谢

1 个答案:

答案 0 :(得分:1)

很难理解你在这里提供的所有代码,但我会尽力帮助。

如我所见,$groups课程中的Roles媒体资源已注明OneToManymappedBy设为"roles"

/**
 * @var ArrayCollection $groups
 *
 * @ORM\OneToMany(targetEntity="Groups", mappedBy="roles", cascade={"persist","merge"})
 * @Assert\Valid()
 */
protected $groups;

关系的目标实体是Groups,据我在您的商家信息中看到,此实体没有roles属性,而是role代替:

/**
 * @var Roles $role
 *
 * @ORM\ManyToOne(targetEntity="Roles", inversedBy="groups", cascade={"persist", "merge"})
 * @ORM\JoinColumns({
 *  @ORM\JoinColumn(name="role_id", referencedColumnName="id", nullable=false)
 * })
 * @Assert\Valid()
 */
protected $role;

您应该尝试将此属性重命名为roles