2016-12-08 1 views
-3

UtilisateurType.phpКак отлаживать «Предупреждение: preg_match() ожидает, что параметр 2 будет строкой, объект указан« на странице загрузки Symfony/Twig?

<?php 

namespace biblioBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 

class UtilisateurType extends AbstractType 
{ 
/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('ncin') 
     ->add('pseudo') 
     ->add('motdepasse') 
     ->add('idpersonne', EntityType::class, array(
        'class' => 'biblioBundle:Personne', 
        'choice_label' => function ($category) { 
         return $category->getIdpersonne(); 
                  })) 
     ->getForm(); 

} 

/** 
* @param OptionsResolver $resolver 
*/ 
public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'biblioBundle\Entity\Utilisateur' 
    )); 
} 
} 

Utilisateur.php

<?php 

    namespace biblioBundle\Entity; 

    use Doctrine\ORM\Mapping as ORM; 

    /** 
    * Utilisateur 
    * 
    * @ORM\Table(name="utilisateur", indexes={@ORM\Index(name="AK_NCIN",  columns={"NCIN"})}) 
    * @ORM\Entity 
    */ 
    class Utilisateur 
    { 
/** 
* @var string 
* 
* @ORM\Column(name="NCIN", type="string", length=8, nullable=false) 
*/ 
private $ncin; 

/** 
* @var string 
* 
* @ORM\Column(name="Pseudo", type="string", length=100, nullable=false) 
*/ 
private $pseudo; 

/** 
* @var string 
* 
* @ORM\Column(name="MotDePasse", type="string", length=100, nullable=false) 
*/ 
private $motdepasse; 

/** 
* @var \biblioBundle\Entity\Personne 
* 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="NONE") 
* @ORM\OneToOne(targetEntity="biblioBundle\Entity\Personne") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="idPersonne", referencedColumnName="idPersonne") 
* }) 
*/ 
private $idpersonne; 



/** 
* Set ncin 
* 
* @param string $ncin 
* @return Utilisateur 
*/ 
public function setNcin($ncin) 
{ 
    $this->ncin = $ncin; 

    return $this; 
} 

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

/** 
* Set pseudo 
* 
* @param string $pseudo 
* @return Utilisateur 
*/ 
public function setPseudo($pseudo) 
{ 
    $this->pseudo = $pseudo; 

    return $this; 
} 

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

/** 
* Set motdepasse 
* 
* @param string $motdepasse 
* @return Utilisateur 
*/ 
public function setMotdepasse($motdepasse) 
{ 
    $this->motdepasse = $motdepasse; 

    return $this; 
} 

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

/** 
* Set idpersonne 
* 
* @param \biblioBundle\Entity\Personne $idpersonne 
* @return Utilisateur 
*/ 
public function setIdpersonne(\biblioBundle\Entity\Personne $idpersonne) 
{ 
    $this->idpersonne = $idpersonne; 

    return $this; 
} 

/** 
* Get idpersonne 
* 
* @return \biblioBundle\Entity\Personne 
*/ 
public function getIdpersonne() 
{ 
    return $this->idpersonne; 
} 
    } 

UtilisateurController.php

<?php 

    namespace biblioBundle\Controller; 

    use Symfony\Component\HttpFoundation\Request; 
    use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

    use biblioBundle\Entity\Utilisateur; 
    use biblioBundle\Form\UtilisateurType; 

    /** 
    * Utilisateur controller. 
    * 
    */ 
    class UtilisateurController extends Controller 
    { 
/** 
* Lists all Utilisateur entities. 
* 
*/ 
public function indexAction() 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $utilisateurs = $em->getRepository('biblioBundle:Utilisateur')->findAll(); 

    return $this->render('utilisateur/index.html.twig', array(
     'utilisateurs' => $utilisateurs, 
    )); 
} 

/** 
* Creates a new Utilisateur entity. 
* 
*/ 
public function newAction(Request $request) 
{ 
    $utilisateur = new Utilisateur(); 
    $form = $this->createForm('biblioBundle\Form\UtilisateurType', $utilisateur); 
    $form->handleRequest($request); 

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

     return $this->redirectToRoute('utilisateur_show', array('id' => $utilisateur->getIdpersonne())); 
    } 

    return $this->render('utilisateur/new.html.twig', array(
     'utilisateur' => $utilisateur, 
     'form' => $form->createView(), 
    )); 
} 

/** 
* Finds and displays a Utilisateur entity. 
* 
*/ 
public function showAction(Utilisateur $utilisateur) 
{ 
    $deleteForm = $this->createDeleteForm($utilisateur); 

    return $this->render('utilisateur/show.html.twig', array(
     'utilisateur' => $utilisateur, 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

/** 
* Displays a form to edit an existing Utilisateur entity. 
* 
*/ 
public function editAction(Request $request, Utilisateur $utilisateur) 
{ 
    $deleteForm = $this->createDeleteForm($utilisateur); 
    $editForm = $this->createForm('biblioBundle\Form\UtilisateurType', $utilisateur); 
    $editForm->handleRequest($request); 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($utilisateur); 
     $em->flush(); 

     return $this->redirectToRoute('utilisateur_edit', array('id' => $utilisateur->getIdpersonne())); 
    } 

    return $this->render('utilisateur/edit.html.twig', array(
     'utilisateur' => $utilisateur, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

/** 
* Deletes a Utilisateur entity. 
* 
*/ 
public function deleteAction(Request $request, Utilisateur $utilisateur) 
{ 
    $form = $this->createDeleteForm($utilisateur); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->remove($utilisateur); 
     $em->flush(); 
    } 

    return $this->redirectToRoute('utilisateur_index'); 
} 

/** 
* Creates a form to delete a Utilisateur entity. 
* 
* @param Utilisateur $utilisateur The Utilisateur entity 
* 
* @return \Symfony\Component\Form\Form The form 
*/ 
private function createDeleteForm(Utilisateur $utilisateur) 
{ 
    return $this->createFormBuilder() 
     ->setAction($this->generateUrl('utilisateur_delete', array('id' => $utilisateur->getIdpersonne()))) 
     ->setMethod('DELETE') 
     ->getForm() 
    ; 
} 
    } 

index.html.twig -> UTILIS ateur

{% extends 'base.html.twig' %} 

{% тела блок%} список

Utilisateur

<table> 
    <thead> 
     <tr> 
      <th>Ncin</th> 
      <th>Pseudo</th> 
      <th>Motdepasse</th> 
      <th>Actions</th> 
     </tr> 
    </thead> 
    <tbody> 
    {% for utilisateur in utilisateurs %} 
     <tr> 
      <td><a href="{{ path('utilisateur_show', { 'id': utilisateur.idpersonne }) }}">{{ utilisateur.ncin }}</a></td> 
      <td>{{ utilisateur.pseudo }}</td> 
      <td>{{ utilisateur.motdepasse }}</td> 
      <td> 
       <ul> 
        <li> 
         <a href="{{ path('utilisateur_show', { 'id': utilisateur.idpersonne }) }}">show</a> 
        </li> 
        <li> 
         <a href="{{ path('utilisateur_edit', { 'id': utilisateur.idpersonne }) }}">edit</a> 
        </li> 
       </ul> 
      </td> 
     </tr> 
    {% endfor %} 
    </tbody> 
</table> 

<ul> 
    <li> 
     <a href="{{ path('utilisateur_new') }}">Create a new entry</a> 
    </li> 
</ul> 
    {% endblock %} 

Каждый раз, когда я пытаюсь загрузить страницу, я получаю эту ошибку. Перед тем, как добавить что-то в моей базе данных она работает отлично, как только я добавляю я проблема:

Предупреждение: preg_match() ожидает параметр 2 будет строка, данный объект

тогда, когда я обновить Я получаю вторую ошибку.

enter image description here

+3

Пожалуйста, отредактируйте ваш вопрос и перепишите свой заголовок, чтобы описать свой фактический вопрос. – Blorgbeard

+0

Пожалуйста, перепишите свое название, чтобы описать свой фактический вопрос, как спросил @Blorgbeard. 'Мне нужна помощь, пожалуйста ... я не понимаю, это не полезное дополнение к названию. – halfer

+0

Ошибка довольно понятная, но что такое строка 18 в 'index.html.twig'? –

ответ

1

Я думаю, что вам нужно передать идентификатор Personne, в вашем случае, если я не ошибаюсь, было бы так:

<a href="{{ path('utilisateur_show', { 'id': utilisateur.idpersonne.idpersonne }) }}"> 

По другой стороны, вы можете вывести объект для проверки значения следующим образом:

{{ dump(utilisateur.idpersonne) }} 
Смежные вопросы