Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 734 Vote(s) - 3.45 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Doctrine findOneBy method not working

#1
I am creating small application with just two entities, Order and Shipment.

The Shipment entity is as follows: (methods removed to keep it short)

/**
* @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_id` but my `findOneByOrderId` method is not working.

$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 `find` methods join the variables at underscores and capitalize them. What am I doing wrong?
Reply

#2
You could use the inbuilt relationship capabilities of Doctrine2 instead of using an id of order in your entity **Shipment** manually
That way you would have a relationship Doctrine is aware of.

$orders = $shipment->getOrders();

Look here:

[To see links please register here]

Reply

#3
I managed to solve the problem with the hint from pomaxa and Doctrine2 documentation.

The correct code would be:

$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneBy(array('order_id' => $orderId));

explained at:

[To see links please register here]


Thanks everyone for the help. I appreciate it.
Reply

#4
Problem in this line
> private $order_id;

Use it

> private $orderId;

It is ok. For db you will have order_id.
Reply

#5
Just to clarify, the reason for the error was that you needed to pass an Array into the `findOneBy();`

This is wrong: , `->findOneByOrderId($orderId);` in


$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneByOrderId($orderId);

An array must be passed. `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

$orderExists = $this->getDoctrine()
->getRepository('ShipBundle:Shipment')
->findOneBy(['order_id'=> $orderId]);

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through