php 学说 findOneBy 方法不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11932618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine findOneBy method not working
提问by Aayush
I am creating small application with just two entities, Order and Shipment.
我正在创建只有两个实体的小型应用程序,订单和发货。
The Shipment entity is as follows: (methods removed to keep it short)
Shipment 实体如下:(删除了方法以保持简短)
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $username
*
* @ORM\Column(name="username", type="string", length=255)
*/
private $username;
/**
* @var string $password
*
* @ORM\Column(name="password", type="string", length=255)
*/
private $password;
/**
* @var integer $order_id
*
* @ORM\Column(name="order_id", type="integer")
*/
private $order_id;
/**
* @var smallint $payment_type
*
* @ORM\Column(name="payment_type", type="smallint")
*/
private $payment_type;
In my controller I am trying to query using the order_idbut my findOneByOrderIdmethod is not working.
在我的控制器中,我尝试使用 进行查询,order_id但我的findOneByOrderId方法不起作用。
$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneByOrderId($orderId);
var_dump($orderExists); die();
The error I get is:
我得到的错误是:
Entity 'ShipBundle\Entity\Shipment' has no field 'orderId'. You can therefore not call 'findOneByOrderId' on the entities' repository.
If I am not wrong, Doctrine findmethods join the variables at underscores and capitalize them. What am I doing wrong?
如果我没记错的话,Doctrinefind方法在下划线处连接变量并将它们大写。我究竟做错了什么?
回答by Aayush
I managed to solve the problem with the hint from pomaxa and Doctrine2 documentation.
我设法通过 pomaxa 和 Doctrine2 文档中的提示解决了这个问题。
The correct code would be:
正确的代码是:
$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneBy(array('order_id' => $orderId));
explained at: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-simple-conditions
解释在:http: //docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-simple-conditions
Thanks everyone for the help. I appreciate it.
感谢大家的帮助。我很感激。
回答by Taras Hanych
Problem in this line
这一行的问题
private $order_id;
私人 $order_id;
Use it
用它
private $orderId;
私人 $orderId;
It is ok. For db you will have order_id.
没关系。对于 db,您将拥有 order_id。
回答by player-one
You could use the inbuilt relationship capabilities of Doctrine2 instead of using an id of order in your entity Shipmentmanually That way you would have a relationship Doctrine is aware of.
您可以使用 Doctrine2 的内置关系功能,而不是在您的实体Shipment 中手动使用订单 id这样您就会拥有 Doctrine 知道的关系。
$orders = $shipment->getOrders();
Look here: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html
看这里:http: //docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html
回答by luminol
Just to clarify, the reason for the error was that you needed to pass an Array into the findOneBy();
只是为了澄清,错误的原因是您需要将一个数组传递到 findOneBy();
This is wrong: , ->findOneByOrderId($orderId);in
这是错误的: ,->findOneByOrderId($orderId);在
$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneByOrderId($orderId);
An array must be passed. array('order_id' => $orderId)
必须传递一个数组。 array('order_id' => $orderId)
$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneBy(array('order_id' => $orderId));
OR SHORTHAND ['order_id'=> $orderId]as long as you are in PHP >= 5.4
或简短['order_id'=> $orderId],只要您使用 PHP >= 5.4
$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneBy(['order_id'=> $orderId]);

