嗨我在数据库中插入数据时遇到此错误请告诉我symfony中的新问题在哪里
“类型为”AppBundle \ Entity \ Class_cat \“的预期值 关联字段\“AppBundle \ Entity \ User#$ class_cat \”,得到了 “整数”代替。“, “class”:“Doctrine \ ORM \ ORMInvalidArgumentException”
这是用户实体
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User
{
/**
* Constructor
*/
public function __construct()
{
$this->class_cat = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @var int
*
* @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="role", type="string", length=255)
*/
private $role;
/**
* @ORM\ManyToOne(targetEntity="Class_cat", inversedBy="users")
* @ORM\JoinColumn(name="class_id", referencedColumnName="id")
*/
private $class_cat;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return User
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set role
*
* @param string $role
*
* @return User
*/
public function setRole($role)
{
$this->role = $role;
return $this;
}
/**
* Get role
*
* @return string
*/
public function getRole()
{
return $this->role;
}
/**
* @return mixed
*/
public function getClassCat()
{
return $this->class_cat;
}
/**
* @param mixed $class_cat
*/
public function setClassCat($class_cat)
{
$this->class_cat = $class_cat;
}
这是class_cat enity
class Class_cat {
/**
* Constructor
*/
public function __construct()
{
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="class_name", type="string", length=255)
*/
private $className;
/**
* @ORM\OneToMany(targetEntity="User", mappedBy="class_cat")
*/
protected $users;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set className
*
* @param string $className
*
* @return Class_cat
*/
public function setClassName($className)
{
$this->className = $className;
return $this;
}
/**
* Get className
*
* @return string
*/
public function getClassName()
{
return $this->className;
}
public function getUsers()
{
return $this->users;
}
/**
* @param mixed $users
*/
public function setUsers($users)
{
$this->users = $users;
}
这是控制器代码
> public function postAction(Request $request)
> {
> $data = new User;
> $name = $request->get('name');
> $role = $request->get('role');
> $class = $request->get('class_id');
> if (empty($name) || empty($role) || empty($class)) {
> return new View("NULL VALUES ARE NOT ALLOWED", Response::HTTP_NOT_ACCEPTABLE);
> }
> $data->setName($name);
> $data->setRole($role);
> $data->setClassId(1);
> $em = $this->getDoctrine()->getManager();
> $em->persist($data);
> $em->flush();
> return new View("User Added Successfully", Response::HTTP_OK);
> }
答案 0 :(得分:1)
您需要传递不是id的实体,例如:
$em = $this->getDoctrine()->getManager();
$classCat = $em->getRepository('AppBundle:ClassCat')->find(1);
$data->setClassCat($classCat);
答案 1 :(得分:0)
嗨,您无需传递整数值,您需要传递Class_cat
类的对象。
当您将任何实体字段映射到任何其他实体时,symfony需要设置对象。
因此,您的解决方案很容易尝试这样的事情:
public function postAction(Request $request){
$em = $this->getDoctrine()->getManager();
$data = new User;
$name = $request->get('name');
$role = $request->get('role');
$class = $request->get('class_id');
if (empty($name) || empty($role) || empty($class)) {
return new View("NULL VALUES ARE NOT ALLOWED",
Response::HTTP_NOT_ACCEPTABLE);
}
$classCat = $em->getRepository('AppBundle:ClassCat')->find($class);
$data->setName($name);
$data->setRole($role);
$data->setClassId($classCat);
$em->persist($data);
$em->flush();
return new View("User Added Successfully", Response::HTTP_OK);
}
答案 2 :(得分:-1)
而不是整数值 使用这样的对象:
$data->setClassId(1);
$em = $this->getDoctrine()->getManager();
$classCat = $em->getRepository('AppBundle:ClassCat')->find(1);
$data->setClassCat($classCat);