简体   繁体   English

如何在Doctrine2中没有@Column注释的实体属性中进行搜索?

[英]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. 您显示的findOneByTotalCostfindBy示例存在的问题是,第一个示例需要您自己编写一个称为 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.

相关问题 如何配置在Symfony2项目中扩展PersistentObject的Doctrine2实体? - How do I configure a Doctrine2 entity which extends PersistentObject within Symfony2 project? 如何检查是否在Doctrine2存储库中找到了对象? - How do I check if object was found in a Doctrine2 repository? 我如何在Doctrine2中进行MySQL计数(*)? - How would I do MySQL count(*) in Doctrine2? 我可以在 Doctrine2 中迭代实体的属性吗? - Can I iterate over an Entity's properties in Doctrine2? 如果没有该名称的实体Doctrine2 Symfony2,我该如何访问存储库 - How can I access the repository if don't have entity for that name Doctrine2 Symfony2 zf2 doctrine2如何在实体列中使用tinyint数据类型 - zf2 doctrine2 how to use tinyint datatype in entity column 如何保存 Doctrine2 实体 - How to save Doctrine2 Entity 我是否需要担心与Doctrine2 EntityManager的多个实例不一致? - Do I have to worry about inconsistencies with multiple instances of Doctrine2 EntityManagers? 如果我的Web服务器是远程的,我应该将Netbeans指向哪个doctrine2脚本? -使用Symfony2 - To which doctrine2 script do I point Netbeans if my webserver is remote? - using Symfony2 我们是否需要清除Doctrine2中2个查询之间的查询生成器? - Do we have to clear the query builder between 2 queries in Doctrine2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM