Php 在symfony中使用两个同名实体
嗨,我需要使用两个名称相同但来自不同捆绑包的实体 下面是我如何在数据库中插入数据Php 在symfony中使用两个同名实体,php,doctrine,symfony,Php,Doctrine,Symfony,嗨,我需要使用两个名称相同但来自不同捆绑包的实体 下面是我如何在数据库中插入数据 use StudentsBundle\Entity\logintrys; $em = $this->getDoctrine()->getManager(); $start = new logintrys(); $start->setStudentId($username); $start->setLogintrys($array); $em = $this->g
use StudentsBundle\Entity\logintrys;
$em = $this->getDoctrine()->getManager();
$start = new logintrys();
$start->setStudentId($username);
$start->setLogintrys($array);
$em = $this->getDoctrine()->getManager();
$em->persist($start);
$em->flush();
和实体类
<?php
namespace StudentsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* logintrys
*
* @ORM\Table(name="logintrys")
* @ORM\Entity(repositoryClass="StudentsBundle\Repository\logintrysRepository")
*/
class logintrys
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="student_id", type="string", length=20)
*/
private $studentId;
/**
* @var array
*
* @ORM\Column(name="logintrys", type="json_array", nullable=true)
*/
private $logintrys;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set studentId
*
* @param string $studentId
*
* @return logintrys
*/
public function setStudentId($studentId)
{
$this->studentId = $studentId;
return $this;
}
/**
* Get studentId
*
* @return string
*/
public function getStudentId()
{
return $this->studentId;
}
/**
* Set logintrys
*
* @param array $logintrys
*
* @return logintrys
*/
public function setLogintrys($logintrys)
{
$this->logintrys = $logintrys;
return $this;
}
/**
* Get logintrys
*
* @return array
*/
public function getLogintrys()
{
return $this->logintrys;
}
}
我认为更好的方法是使用继承,其中主类包含您的logintrys成员
因为在php中,您不能执行类似的操作:
use StudentsBundle\Entity\logintrys;
use TeachersBundle\Entity\logintrys;
此示例提供了一个错误,因为脚本不知道要实例化的类的类型
我认为这是一个解决方案(总是有更好的,但应该有效):
要创建其中之一,请执行以下操作:
$start = new father();
$start->setLogintrys($array);
if($start instanceof student) $start->setStudentId($username);
我认为更好的方法是使用继承,其中主类包含您的logintrys成员
因为在php中,您不能执行类似的操作:
use StudentsBundle\Entity\logintrys;
use TeachersBundle\Entity\logintrys;
此示例提供了一个错误,因为脚本不知道要实例化的类的类型
我认为这是一个解决方案(总是有更好的,但应该有效):
要创建其中之一,请执行以下操作:
$start = new father();
$start->setLogintrys($array);
if($start instanceof student) $start->setStudentId($username);
当您通过以下方式呼叫您的实体:
use StudentsBundle\Entity\logintrys;
您可以添加一个“as”:
use StudentsBundle\Entity\logintrys as StudentLogintrys;
use TeachersBundle\Entity\logintrys as TeacherLogintrys;
然后,在代码中可以执行以下操作:
$start = new StudentLogintrys();
$start2 = new TeacherLogintrys();
当您通过以下方式呼叫您的实体:
use StudentsBundle\Entity\logintrys;
您可以添加一个“as”:
use StudentsBundle\Entity\logintrys as StudentLogintrys;
use TeachersBundle\Entity\logintrys as TeacherLogintrys;
然后,在代码中可以执行以下操作:
$start = new StudentLogintrys();
$start2 = new TeacherLogintrys();
使用两者有什么问题?无法使用TeachersBundle\Entity\logintrys作为logintrys,因为名称已在使用中,这就是问题使用TeachersBundle\Entity\logintrys作为Teacherlogintrys$teacherLogin=新的Teacherlogintrys();这解决了第一个问题,但我得到了SQLSTATE[42S22]:未找到列:1054“字段列表”中的未知列“docent\u id”我认为错误很明显。两者都使用有什么问题?无法使用TeachersBundle\Entity\logintrys作为登录项,因为名称已在使用,这就是问题使用TeachersBundle\Entity\logintrys作为Teacherlogintrys$teacherLogin=新的Teacherlogintrys();这解决了第一个问题,但我得到了这个返回SQLSTATE[42S22]:Column not found:1054未知列'field list'中的'docent_id',我认为错误很明显。