【发布时间】:2014-09-07 17:44:56
【问题描述】:
我有这个数据库模型:
然后我创建了这个实体(我只留下关系部分,因为另一个与主题无关):
Orders.php
class Orders {
/**
* @ORM\ManyToOne(targetEntity="Person", inversedBy="orders")
* @ORM\JoinColumn(name="person_id", referencedColumnName="id")
* */
protected $person;
public function setPerson(Person $person)
{
$this->person = $person;
return $this;
}
public function getPerson()
{
return $this->person;
}
}
Person.php
class Person {
/**
* @ORM\OneToMany(targetEntity="NaturalPerson", mappedBy="person")
* */
private $naturals;
/**
* @ORM\OneToMany(targetEntity="LegalPerson", mappedBy="person")
* */
private $legals;
/**
* @ORM\OneToMany(targetEntity="Orders", mappedBy="person")
* */
private $orders;
public function __construct()
{
$this->naturals = new ArrayCollection();
$this->legals = new ArrayCollection();
$this->orders = new ArrayCollection();
}
public function getNaturals()
{
return $this->naturals;
}
public function getLegals()
{
return $this->legals;
}
public function getOrders()
{
return $this->orders;
}
}
NaturalPerson.php
class NaturalPerson {
/**
* @ORM\Id
* @ORM\ManyToOne(targetEntity="Person", inversedBy="naturals")
* @ORM\JoinColumn(name="person_id", referencedColumnName="id")
*/
protected $person;
/**
* @ORM\Column(name="identification_type", type="ci_type", nullable=false)
* @DoctrineAssert\Enum(entity="Tanane\FrontendBundle\DBAL\Types\CIType")
*/
protected $identification_type;
/**
* @ORM\Column(name="ci", type="integer", nullable=false)
*/
protected $ci;
public function setPerson(Person $person)
{
$this->person = $person;
return $this;
}
public function getPerson()
{
return $this->person;
}
public function setIdentificationType($identification_type)
{
$this->identification_type = $identification_type;
return $this;
}
public function getIdentificationType()
{
return $this->identification_type;
}
public function setCI($ci)
{
$this->ci = $ci;
return $this;
}
public function getCI()
{
return $this->ci;
}
}
我省略了LegalPerson,因为它与NaturalPerson 几乎相同,所以这就是问题所在。映射看起来不错,但我如何从Orders 获取相关记录?
这背后的想法是对于每个Orders,我需要知道Person 也属于哪个(订单)以及存储在NaturalPerson 或LegalPerson 的额外信息,具体取决于person.type。
查看此代码:
public function getOrdersAction()
{
$response = array();
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository("FrontendBundle:Orders")->findAll();
if (!$entities)
{
$response['message'] = "No se encontraron resultados";
}
$orders = array();
foreach ($entities as $entity)
{
$personType = $entity->getPerson()->getPersonType();
$order = array();
$order[] = $entity->getNickname();
// Here I'm trying to access to `Naturals` methods from `Orders`
if ($personType == 1)
{
$order[] = $entity->getPerson()->getNaturals()[0]->getIdentificationType() . $entity->getPerson()->getNaturals()[0]->getCI();
}
elseif ($personType == 2)
{
$order[] = $entity->getPerson()->getLegals()[0]->getIdentificationType() . $entity->getPerson()->getLegals()[0]->getRIF();
}
$orders[] = $order;
}
$response['data'] = $orders;
return new JsonResponse($response);
}
但我收到此错误:
错误:在 a 上调用成员函数 getIdentificationType() 非对象在 /var/www/html/tanane/src/Tanane/BackendBundle/Controller/OrderController.php 第 115 行
也许我的映射是错误的,因为我应该在 Person 和 NaturalPerson 之间有 OneToOne(这对我的逻辑来说听起来是错误的,如 DER 所示)或者可能不是,但是我不知道如何获取仅一条记录的相关属性,我阅读了文档here 和here 但他们没有谈论这部分,或者我没有看到它,有什么建议吗?想法?提示?
尝试使用 Repositories 和 DQL 解决问题
我正在 Repository 类中构建一个函数来获取数据,而不是像我的问题那样变得复杂,所以我这样做了:
public function getOrders($person_type = 1)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('ord.*, ps.*')
->from("FrontendBundle:Orders", "ord")
->join('FrontendBUndle:Person', 'ps', 'WITH', 'ps.id = ord.person_id')
->orderBy('ord.created', 'DESC');
if ($person_type == 1)
{
$qb
->select('np.*')
->join('FrontendBundle:NaturalPerson', 'np', 'WITH', 'ps.id = np.person'); // Join NaturalPerson table
}
elseif ($person_type == 2)
{
$qb
->select('lp.*')
->join('FrontendBundle:LegalPerson', 'lp', 'WITH', 'ps.id = lp.person'); // Join NaturalPerson table
}
return $qb->getQuery()->getResult();
}
我还没有经过测试,所以也许它不会起作用,但是,如果我的想法是获取两个表的额外信息,那么使用这个 DQL 我制作了我如何通过 $person_type 内部的 Person桌子?这有点复杂,至少对我来说是这样
运行原始查询以查看列是否为 NULL
我构建这个简单的查询只是为了测试结果是否为NULL:
SELECT
ord.id,
ord.person_id as ord_person_id,
ord.nickname,
ps.id,
ps.description,
np.person_id as natural_person_id,
np.identification_type,
np.ci
FROM
orders ord
LEFT JOIN person ps ON ord.person_id = ps.id
LEFT JOIN natural_person np ON np.person_id = ps.id
WHERE
ps.person_type = 1;
这是查询返回的内容:
所以那里没有 NULL 列
CRUD 用于创建新订单
// Set Person entity
$entityPerson = new Person();
$person_type === 1 ? $entityPerson->setDescription($orders['nat']['person']['description']) : $entityPerson->setDescription($orders['leg']['person']['description']);
$person_type === 1 ? $entityPerson->setContactPerson($orders['nat']['person']['contact_person']) : $entityPerson->setContactPerson($orders['leg']['person']['contact_person']);
$entityPerson->setPersonType($person_type);
$em->persist($entityPerson);
$em->flush();
...
if ($person_type === 1)
{
// Set NaturalPerson entity
$entityNatural = new NaturalPerson();
$entityNatural->setIdentificationType($orders['nat']['identification_type']);
$entityNatural->setCI($orders['nat']['ci']);
$em->persist($entityNatural);
$em->flush();
}
elseif ($person_type === 2)
{
// Set LegalPerson entity
$entityLegal = new LegalPerson();
$entityLegal->setIdentificationType($orders['leg']['identification_type']);
$entityLegal->setRIF($orders['leg']['rif']);
$em->persist($entityLegal);
$em->flush();
}
【问题讨论】:
-
我猜
Person可能是NaturalPerson或LegalPerson,对吧?还是两者兼而有之? -
不,它可以是自然人或合法人之一
标签: php symfony orm doctrine-orm mapping