2016-07-08 3 views
0

Symfony2를 배우려고하고 있는데 현재 : 엔티티 관계/연관 (관련 레코드에 가입). 내가 얻을 수없는 문제가 이것은 내 코드입니다알림 : 정의되지 않은 색인 : Symfony 2.7의 역할

Notice: Undefined index: roles 500 Internal Server Error - 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 권한 :

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 그룹 :

(210)

- 여기 내 컨트롤러

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() 
    ; 
} 

}입니다

문제 :

Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in /home/cros/Desktop/Project_Console/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 555 and defined 500 Internal Server Error - ContextErrorException

* 스택 추적 :

공급 업체/교리

/컬렉션/lib 디렉토리/교리/Common/Collections/ArrayCollection.php at line 48 -

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

at ErrorHandler ->handleError ('4096', 'Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in /home/cros/Desktop/Project_Console/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php on line 555 and defined', '/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') 

      ; 

당신에게 감사

+0

언제이 오류가 발생합니까? 존재하지 않는 배열 키에 액세스하려는 것 같습니다. 이 오류의 문맥을 보여줄 수 있습니까? (아마 컨트롤러?). –

+0

그룹을 만들려면 모든 필드 (이름, 역할, 권한)를 완료해야 문제가 나타납니다. (주제를 바꿨습니다) 여기에서 문제가 될 수도 있습니다 -> mappedBy = "roles"!! – Cros

+0

스택 추적은 여기에서 매우 유용 할 것입니다. 그렇지 않으면 거의 추측 할 수 있습니다. – kix

답변

1

그것은 당신이 여기서 제공 한 모든 코드를 통해 발굴하는 것은 매우 어렵습니다,하지만 난 도움을하려고합니다. 내가 보는 것처럼

은, 당신의 Roles 클래스의 $groups 속성은 OneToMany"roles"mappedBy 설정을 갖는 주석된다.

/** 
* @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으로 변경하십시오.

+0

제안한 해결책을 시도했지만 추가 할 때 또 다른 문제가 나타납니다 : -> Catchable Fatal Error : 인수 1이 Doctrine \ Common \ Collections \ ArrayCollection에 전달되었습니다. :: __ construct()는/home/syrine01/Desktop/Project_Console/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork에서 호출 된 array, object 유형이어야합니다.php on line 555 and defined – Cros

+0

이것은 양식 중 하나가 유효하지 않을 수 있음을 나타냅니다. XDebug를 사용하여 스택 추적을 검사하거나 프로그램 흐름을 추적하여 예외가 발생한 정확한 위치를 확인하십시오. – kix

+0

constract에서의 문제 나는 왜 그런지 모른다? – Cros