gpt4 book ai didi

forms - symfony2 表单中的实体未找到错误

转载 作者:行者123 更新时间:2023-12-02 04:38:28 27 4
gpt4 key购买 nike

我有一个带有 buildForm 的 symfony2 Form CategoryType:

  public function buildForm(FormBuilderInterface $builder, array $options) {

$builder->add('language', 'entity', array(
'class' => 'Evr\HomeBundle\Entity\Language',
'property' => 'language'
)
)
->add('category', 'text', array('label' => 'category.category', 'required' => true));
}

如你所料,我有两个实体Category和Language,其中Category是Language的child(一种语言可以有多个类别,一个类别属于1种或0种语言)

类别

<?php

namespace Evr\HomeBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* Category
*
* @ORM\Table(name="ev_category")
* @ORM\Entity
*/
class Category
{
/**
* @var integer
*
* @ORM\Column(name="category_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

/**
*
* @ORM\ManyToOne(targetEntity="Language",inversedBy="categories")
* @ORM\JoinColumn(name="language_id",referencedColumnName="language_id")
*/
private $language;

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

/**
* @ORM\OneToMany(targetEntity="Subcategory", mappedBy="category")
*/
protected $subcategories;

public function __construct(){
$this->subcategories=new ArrayCollection();
}

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

/**
* Set language
*
* @param integer $language
* @return Category
*/
public function setLanguage($language) {
$this->language = $language;

return $this;
}

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

/**
* Set category
*
* @param \string $category
* @return Category
*/
public function setCategory($category)
{
$this->category = $category;

return $this;
}

/**
* Get category
*
* @return \string
*/
public function getCategory()
{
return $this->category;
}

}

语言

<?php

namespace Evr\HomeBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* Language
*
* @ORM\Table(name="ev_language")
* @ORM\Entity
*/
class Language
{
/**
* @var integer
*
* @ORM\Column(name="language_id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

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

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

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

/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="language")
*/
protected $categories;

/**
* @ORM\OneToMany(targetEntity="Country", mappedBy="language")
*/
protected $countries;

public function __construct(){
$this->categories=new ArrayCollection();
$this->countries=new ArrayCollection();
}

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

/**
* Set language
*
* @param string $language
* @return Language
*/
public function setLanguage($language)
{
$this->language = $language;

return $this;
}

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

/**
* Set code
*
* @param string $code
* @return Language
*/
public function setCode($code)
{
$this->code = $code;

return $this;
}

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

/**
* Set direction
*
* @param boolean $direction
* @return Language
*/
public function setDirection($direction)
{
$this->direction = $direction;

return $this;
}

/**
* Get direction
*
* @return boolean
*/
public function getDirection()
{
return $this->direction;
}
}

在编辑类别时,我需要将当前值显示在一个表单中,以便用户可以修改并保存。

这里我有一个 Controller editAction(),它的任务是显示编辑形式:

   public function editAction($id) { //id of the category to modify

$category = $this->getDoctrine()->getRepository('EvrHomeBundle:Category')->find($id); //return the category with id

$categoryForm = $this->createForm(new CategoryType(),$category); //building the form


return $this->render('EvrAdminBundle:Categories:edit.html.twig', array('categoryForm' => $categoryForm->createView(), 'category' => $category));
}//render the form

请记住,CategoryType 有一个类型为 entity 的元素,它在选择框中加载语言。

但是当试图用当前数据(类别和语言)填充表单 CategoryType 时,Symfony 返回一个错误:Entity Not Found

Symfony 没有指定错误发生在哪一行,但我认为它在这一行附近:

$categoryForm = $this->createForm(new CategoryType(),$category); //building the form

因为当我删除 createForm 的第二个参数:$category 时,它会显示一个空表单(就像 add category 表单>/p>

这个问题有解决办法吗?考虑到表单包含实体元素这一事实,我如何使用数据库中的当前数据创建表单。

谢谢

最佳答案

如果您的数据库没有强制执行参照完整性,那么您可以删除一条记录,即使另一条记录指向它(它们应该通过外键链接)。然后,当您尝试获取记录时,它似乎可以工作,但是当您想要访问该实体的属性时(这是对数据库的真正访问发生时),它找不到具有该 record_id 的实体,因此“找不到实体".

例如:

$user=new User();
$car=$user->getCar(); /*The user had a CAR record assigned with Id = 1 but the record was
deleted.
The record USER still has the id of 1.*/
/*No error up till here*/
$carName=$car->getName(); /*Error occurs because it tries to grab the record with id of 1
which was deleted*/

关于forms - symfony2 表单中的实体未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21413086/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com