我正在创建只有两个实体的小型应用程序,Order 和 Shipment。
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;
在我的 Controller 中,我尝试使用 order_id
进行查询,但我的 findOneByOrderId
方法不起作用。
$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneByOrderId($orderId);
var_dump($orderExists); die();
我得到的错误是:
Entity 'ShipBundle\Entity\Shipment' has no field 'orderId'. You can therefore not call 'findOneByOrderId' on the entities' repository.
如果我没记错的话,Doctrine find
方法在下划线处加入变量并将它们大写。我做错了什么?
最佳答案
根据 pomaxa 和 Doctrine2 文档的提示,我设法解决了这个问题。
正确的代码是:
$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneBy(array('order_id' => $orderId));
感谢大家的帮助。我很感激。
关于php - 教义 findOneBy 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11932618/