Hi我需要使用两个名称相同但来自不同捆绑的实体
以下是我如何在数据库中插入数据
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;
}
}
我的第二个插入查询与第一个完全相同,只是现在我不能使用$start=new logintry();因为这与第一个的名称相同
这是我在教师包中的实体
<?php
namespace TeachersBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
/**
* logintrys
*
* @ORM'Table(name="logintrys")
* @ORM'Entity(repositoryClass="TeachersBundle'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="docent_id", type="string", length=20)
*/
private $docentId;
/**
* @var array
*
* @ORM'Column(name="logintrys", type="json_array", nullable=true)
*/
private $logintrys;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set docentId
*
* @param string $docentId
*
* @return logintrys
*/
public function setDocentId($docentId)
{
$this->docentId = $docentId;
return $this;
}
/**
* Get docentId
*
* @return string
*/
public function getDocentId()
{
return $this->docentId;
}
/**
* 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;
}
}
我的问题是如何在同一脚本中使用StudentsBundle和TeachersBundle的登录
这是错误
无法将TeachersBundle''Entity''lintrys用作登录,因为该名称已在使用
提前感谢
当您用调用实体时
use StudentsBundle'Entity'logintrys;
您可以添加一个"as":
use StudentsBundle'Entity'logintrys as StudentLogintrys;
use TeachersBundle'Entity'logintrys as TeacherLogintrys;
http://php.net/manual/en/language.namespaces.importing.php
然后,在你的代码中你可以做:
$start = new StudentLogintrys();
$start2 = new TeacherLogintrys();
我认为更好的方法是使用继承,其中主类包含loginrys成员。
因为在php中你不能做这样的事情:
use StudentsBundle'Entity'logintrys;
use TeachersBundle'Entity'logintrys;
这个例子提供了一个错误,因为脚本不知道要实例化的类的类型。
我认为这是一个解决方案(总有更好的,但它应该有效):
class father{
private logintrys
}
class student extends father{
private id;
private studentId;
}
class teacher extends father{
private id;
private docentId;
}
创建其中一个:
$start = new father();
$start->setLogintrys($array);
if($start instanceof student) $start->setStudentId($username);