symfony:preg_match()期望参数

时间:2016-12-08 12:15:31

标签: php html symfony twig preg-match

*在我将数据添加到我的数据库之前,只要我添加第一行,evry thigns正常工作我收到此消息  "警告:preg_match()期望参数2是字符串,对象给定" 以及存储在我的数据库中的数据....所以当它想要检索它时,我得到了这个消息 "在渲染模板期间抛出异常("警告:preg_match()期望参数2为字符串,对象给定")在第20行的etudiant \ index.html.twig中。 " 谁能帮我 ?艾特知道我该怎么做*

EtudiantController.php

<?php

  namespace biblioBundle\Controller;

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

  use biblioBundle\Entity\Etudiant;
  use biblioBundle\Form\EtudiantType;

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

    $etudiants = $em->getRepository('biblioBundle:Etudiant')->findAll();

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

/**
 * Creates a new Etudiant entity.
 *
 */
public function newAction(Request $request)
{
    $etudiant = new Etudiant();
    $form = $this->createForm(EtudiantType::class, $etudiant);
    $form->handleRequest($request);

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

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

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

EtudiantType.php

<?php

  namespace biblioBundle\Form;

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


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

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

index.html.twig

<table>
    <thead>
        <tr>
            <th>Ncin</th>
            <th>Carteetudiant</th>
            <th>Groupe</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    {% for etudiant in etudiants %}
        <tr>
            <td><a href="{{ path('etudiant_show', { 'id': etudiant.idpersonne }) }}">{{ etudiant.ncin }}</a></td>
            <td>{{ etudiant.carteetudiant }}</td>
            <td>{{ etudiant.groupe }}</td>
            <td>
                <ul>
                    <li>
                        <a href="{{ path('etudiant_show', { 'id': etudiant.idpersonne }) }}">show</a>
                    </li>
                    <li>
                        <a href="{{ path('etudiant_edit', { 'id': etudiant.idpersonne }) }}">edit</a>
                    </li>
                </ul>
            </td>
        </tr>
    {% endfor %}
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

根据您的Form EtudiantType,etudiant.idpersonne是biblioBundle \ Entity \ Personne实体。也许你想要做的是:

<td><a href="{{ path('etudiant_show', { 'id': etudiant.idpersonne.id }) }}">{{ etudiant.ncin }}</a></td>

在个人情况下,我会将Etudiant::$idpersonne重构为Etudiant::$personne以反映您在其中存储实体的事实,而不是整数。然后我会用它来称呼它:

<td><a href="{{ path('etudiant_show', { 'id': etudiant.personne.id }) }}">{{ etudiant.ncin }}</a></td>