【问题标题】:upgrade symfony from 2.3 to 2.7 issues with form that do not retrieve the entity将 symfony 从 2.3 升级到 2.7 出现无法检索实体的表单问题
【发布时间】:2019-01-09 17:31:29
【问题描述】:

我正在检索几年未更新的旧应用程序的代码。我们的系统管理员已将代码放在 php7 服务器中(之前它在 php5 上正常工作)。代码工作得很好。我想做一些更新,我做的第一个是将 symfony 从 2.3 升级到 2.7.*。当然,现在问题出现了。 我有一个正确呈现的表单(所有字段都可以,即使是数据库中的字段)。这是我的建造者:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('object','text',array(
            "required"=>false,
            "attr"=>array(
                "placeholder"=>"Object"
                )
            ))
        ->add('date','date',array(
            'widget'=>'single_text',
            ))
        ->add('contact', 'entity', array(
            'label'=>'Contact',
            'class'=>'MyApp\AppliBundle\Entity\Contact',
            'choice_translation_domain' => true,
            'placeholder'=>'initials',
            'choice_label' => 'initials',
            'multiple'=>true
            ))
        ->add('text','redactor',array(
            "required"=>false,
            "redactor"=>"default"
            ))
    ;
}

这是我的控制器:

public function editMeetingAction($id,Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $meeting = $em->getRepository('MyAPPAppliBundle:Meeting')- 
      >findOneById($id);
    $form   = $this->createForm(new MeetingType, $meeting);
    $form->handleRequest($request);
    if ($form->isValid()) {
        $em->persist($meeting);
        $em->flush();
        $this->get('session')->getFlashBag()->add('success', 'Meeting 
    edited successfully');
        return $this->redirect($this- 
   >generateUrl('myapp_appli_manage_editmeeting', array("id" => $id)));
    }
    return array(
        "form" => $form->createView(),
        "id" => $id,
    );
}

现在当我尝试保存表单时,出现以下错误:

[语法错误] line 0, col -1: Error: Expected Literal, got end of string.

[1/2] QueryException: SELECT e FROM MyApp\AppliBundle\Entity\Contact e WHERE

该应用似乎无法检索表单中选择的联系人。 我不知道这里出了什么问题,因为它在以前的版本中可以正常工作。我按照本网站中的步骤帮助我进行迁移并修改了表单中的一些字段(占位符、choices_as_values 等) https://gist.github.com/mickaelandrieu/5211d0047e7a6fbff925

如果您能帮助我,将不胜感激。

[EDIT1]:在我将 symfony 从 2.3 更新到 2.7 之前,表单工作正常

[EDIT2]:实体联系人:

<?php

namespace MyApp\AppliBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\EntityRepository;

/**
 * Contact
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="MyApp\AppliBundle\Entity\ContactRepository")
 */
class Contact
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Name", type="string", length=255)
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(name="Email", type="string", length=255)
     */
    private $email;

    /**
     * @var string
     *
     * @ORM\Column(name="Initials", type="string", length=255)
     */
    private $initials;

    /**
     * @var integer
     *
     * @ORM\Column(name="id_binome", type="integer")
     */
    private $id_binome;

    /**
     * @var string
     *
     * @ORM\Column(name="JobTitles", type="string", length=255)
     */
    private $jobtitle;

    /**
     * Tostring method
     *
     */
    public function __toString()
    {
        return $this->name;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Contact
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     * @return Contact
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

    /**
     * Set initials
     *
     * @param string $initials
     * @return Contact
     */
    public function setInitials($initials)
    {
        $this->initials = $initials;

        return $this;
    }

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

    /**
     * Get id_binome
     *
     * @return integer
     */
    public function getIdBinome()
    {
        return $this->id_binome;
    }

    /**
     * Set id_binome
     *
     * @param integer $id
     * @return Contact
     */
    public function setIdBinome($id)
    {
        $this->id_binome = $id;

        return $this;
    }

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

    /**
     * Set jobtitle
     *
     * @param string $jobtitle
     * @return Contact
     */
    public function setjobtitle($jobtitle)
    {
        $this->jobtitle = $jobtitle;

        return $this;
    }

}


class ContactRepository extends EntityRepository
{
    public function findEmailBinome($id_binome)
    {
        $querybuilder = $this->createQueryBuilder("Contact")
            ->select("Contact.email")
            ->where("Contact.id = :idbinome")
            ->setParameter('idbinome',$id_binome)
            ;

        return $querybuilder
            ->getQuery()
            ->getSingleResult()
            ;
    }
}

[编辑 getter setter]

/**
 * Add contacts
 *
 * @param \MyApp\AppliBundle\Entity\Contact $contacts
 * @return Meeting
 */
public function addContact(\MyApp\AppliBundle\Entity\Contact $contacts)
{
    $this->contacts[] = $contacts;

    return $this;
}

/**
 * Remove contacts
 *
 * @param \MyApp\AppliBundle\Entity\Contact $contacts
 */
public function removeContact(\MyApp\AppliBundle\Entity\Contact $contacts)
{
    $this->contacts->removeElement($contacts);
}

/**
 * Get contacts
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getContacts()
{
    return $this->contacts;
}

【问题讨论】:

  • 你能发布你的实体联系方式吗
  • 确定我已经添加了。
  • 你能检查一下 'myApp\AppliBundle\Entity\Contact' 是否将其更改为 MyApp\AppliBundle\Entity\Contact 吗?
  • 它实际上是我拥有的代码中的 MyApp 而不是 myApp。我想匿名化我的代码。它以大写字母开头。我已经用它编辑了我的问题。
  • 在浏览器中,按 F12 并导航到网络选项卡。发布您的表单并查看实际发布的联系值是多少。也许您在编辑表单时做了一些事情。并仔细检查是否没有 ContactRepository::find() 方法可能会做一些时髦的事情。

标签: php forms symfony


【解决方案1】:

我想我找到了解决方案。由于这是与数据库连接有关的问题,我怀疑 dotrine/orm 有罪!我通过更改来更新 composer.json:

"doctrine/orm": "=2.2.*"

"doctrine/orm": ">=2.2.3"

当我尝试作曲家更新教义/orm 时,它并没有解决问题。但是,当我简单地尝试 composer update 时,我的应用程序再次运行。 非常感谢您的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2015-08-14
    • 2023-04-03
    • 2022-01-11
    • 2018-08-28
    • 1970-01-01
    相关资源
    最近更新 更多