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')
;
당신에게 감사
언제이 오류가 발생합니까? 존재하지 않는 배열 키에 액세스하려는 것 같습니다. 이 오류의 문맥을 보여줄 수 있습니까? (아마 컨트롤러?). –
그룹을 만들려면 모든 필드 (이름, 역할, 권한)를 완료해야 문제가 나타납니다. (주제를 바꿨습니다) 여기에서 문제가 될 수도 있습니다 -> mappedBy = "roles"!! – Cros
스택 추적은 여기에서 매우 유용 할 것입니다. 그렇지 않으면 거의 추측 할 수 있습니다. – kix