【问题标题】:symfony : preg_match() expecting parametersymfony : preg_match() 期待参数
【发布时间】:2016-12-08 12:15:30
【问题描述】:

*在我将数据添加到我的数据库之前,只要我添加第一行,我就会收到这条消息 “ 警告:preg_match() 期望参数 2 是字符串,给定对象” 以及存储在我的数据库中的数据....所以当它试图检索它时,我会收到这条消息 “在第 20 行的 etudiant\index.html.twig 中的模板渲染过程中引发了异常(“警告:preg_match() 期望参数 2 是字符串,对象给定”)。“ 谁能帮我 ?不知道该怎么办*

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>

【问题讨论】:

  • 请任何人帮助我
  • 你的 index.html 文件的第 20 行是什么?
  • {{ etudiant.ncin }} 这是第 20 行
  • idpersonne 必须是字符串。好像是一堂课。你确定 getIdpersonne() 正在返回一个字符串吗?
  • 你能把你的 $etudiants 收藏放在这里吗?

标签: php html symfony twig preg-match


【解决方案1】:

根据您的 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>

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签