[英]How do I search by properties of entity which do not have @Column anotation in Doctrine2?
/**
* @ORM\Entity
*/
class Order extends BaseEntity
{
// this is trait for @Id
use Identifier;
/**
* @ORM\Column(type="integer")
*/
protected $costPerUnit;
/**
* @ORM\Column(type="integer")
*/
protected $numberOfUnits;
// i want to search by this property
protected $totalCost;
public function getTotalCost()
{
return $this->numberOfUnits * $this->costPerUnit;
}
}
I have an entity like this and I'd like to be able to do for example 我有一个这样的实体,例如
$orderRepository->findOneByTotalCost('999')
$orderRepository->findBy(['totalCost' => '400']);
Is this possible in Doctrine2? 在Doctrine2中这可能吗? Or would I go about it differently?
还是我会另辟?径?
Like I said in my comments, it's likely you're wrestling with an issue that shouldn't have occurred in the first place. 就像我在评论中说的那样,很可能您正在努力解决本来不应该发生的问题。 Still, having a
SUM
value mapped to a property is possible using doctrine, in a variety of ways: Check the aggregate field docs to find out which would solve your problem best. 不过,有
SUM
映射到一个属性值可能使用原则,以各种不同的方式: 检查合计字段的文档 ,以找出最能解决你的问题。
To my eyes, is that you're using entities as more than what they really are: Entities represent records in a database, Doctrine is a DBAL. 在我看来,您所使用的实体要比它们实际使用的实体多:实体表示数据库中的记录,Doctrine是DBAL。 Searching data using entities (or repositories) is querying the database.
使用实体(或存储库)搜索数据正在查询数据库。 You could solve the problem by adding custom methods to your entity manager or a custom repository class that'll query all of the data required to compute the totalCost value for all entities, and return only those you need.
您可以通过将自定义方法添加到实体管理器或自定义存储库类来解决问题,该类将查询计算所有实体的totalCost值所需的所有数据,并仅返回所需的数据。 Alternatively, use the connection from your DBAL to query for the id's you're after (for example), then use those values to get to the actual entities.
或者,使用来自DBAL的连接来查询您想要的ID(例如),然后使用这些值获取实际的实体。 Or, like I said before: use aggregate fields.
或者,就像我之前说过的:使用聚合字段。
The problems you have with the findOneByTotalCost
and findBy
examples you show is that the first requires you to write a method Called findOneByTotalCost
yourself. 您显示的
findOneByTotalCost
和findBy
示例存在的问题是,第一个示例需要您自己编写一个称为 findOneByTotalCost
的方法。 The problem with your use of findBy
is simply that your argument is malformed: the array should be associative: use the mapped column names as keys, and the values are what you want to query for: 使用
findBy
的问题很简单,就是您的参数格式错误:数组应该是关联的:将映射的列名称用作键,并且值是您要查询的内容:
$repo->findBy(
['totalCost' => 400]
);
is what you're looking for, not ['totalCost', 400]
. 是您要寻找的内容,而不是
['totalCost', 400]
。 As for the entity itself, you'll need to add an annotation: 至于实体本身,您需要添加一个注释:
Yes it is, judging by your use of @ORM\\Entity
annotations in the doc-blocks, this ought to do it: 是的,通过在文档块中使用
@ORM\\Entity
批注来判断,应该这样做:
/**
* @ORM\Column(type="string", length=255)
*/
protected $regioun = 'Spain';
The update the table, and you'll be able to: 更新表,您将能够:
$entities = $repo->findBy(
['region' => 'Spain']
);
Don't forget that this code represents a table in a DB: you can search on any of the fields, but use indexes, which you can do by adding annotations at the top of your class definition: 别忘了这段代码代表数据库中的一个表:您可以搜索任何字段,但是可以使用索引,可以通过在类定义的顶部添加批注来实现:
/**
* @ORM\Table(name="tblname", indexes={
* @ORM\Index(name="region", columns={"region"})
* })
*/
class Foo
{}
As ever: in DB's, indexes matter 一如既往:在数据库中,索引很重要
You should write a method findOneByTotalCost on your entity repository, something like: 您应该在实体存储库上编写方法findOneByTotalCost,例如:
public function findOneByTotalCost ($queryParams){
$query = 'select o
from <yourEntity> o
where o.numberOfUnits * o.costPerUnit = :myParam';
$dql = $this->getEntityManager()->createQuery($query);
$dql->setParameter('myParam', $queryParams);
return $dql ->execute();
}
Them, $orderRepository->findOneByTotalCost('999') should work. 他们,$ orderRepository-> findOneByTotalCost('999')应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.