Doctrine findOneBy method not working

67,476

Solution 1

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: 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.

Solution 2

Problem in this line

private $order_id;

Use it

private $orderId;

It is ok. For db you will have order_id.

Solution 3

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: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html

Share:
67,476
Aayush
Author by

Aayush

Working @ Webmuch Software, creating Symfony2 Web applications, leading development of a Magento store and learning iOS and Android applications and game development.

Updated on April 12, 2020

Comments

  • Aayush
    Aayush about 4 years

    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?

  • Aayush
    Aayush over 11 years
    Thanks for the answer. But in my entity, I have no relationships between both the entities. All I need is to query my Shipment entity using it's order_id variable.
  • user276648
    user276648 over 6 years
    Indeed you need to follow Symfony's and Doctrine's conventions, else things don't work well.
  • Takman
    Takman over 4 years
    Nothing wrong, he can use both. And findOneByXyz() accepts scalars. The problem is field name $order_id that should be $orderId.