KodeFor.Me
KodeFor.Me

Reputation: 13511

Symfony 3.4 - Doctrine many-to-one returns the Object ID but not the Object itself

Since last week I have started learning Symfony, and while the general stuff it quite easy to learn, the Doctrine seems to be a big pain.

Currently, I have made two entities using the following signature:

<?php

namespace NutritionApiBundle\Entity;

// ...
use Doctrine\Common\Collections\ArrayCollection;
// ...

/**
 * Company
 *
 * @ORM\Table(name="company")
 *     @ORM\Entity(repositoryClass="NutritionApiBundle\Repository\CompanyRepository")
 */
class Company {
    /**
     * @var string
     *
     * @ORM\Column(name="id", type="guid")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    private $id;

    // ...

    /**
     * @var string
     * @ORM\OneToMany(targetEntity="NutritionApiBundle\Entity\Product", mappedBy="company")
     */
    protected $products;

    public function __construct() {
        $this->products = new ArrayCollection();
    }

    // ...
}

and

<?php
namespace NutritionApiBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
// ...

/**
 * Class Product
 *
 * @package NutritionApiBundle\Entity
 *
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product {
    /**
     * @var string
     * @ORM\Column(type="guid")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="UUID")
     */
    protected $id;

    // ...

    /**
     * @var Company
     *
     * @ORM\Column(type="guid", name="company", nullable=false)
     * @ORM\ManyToOne(targetEntity="NutritionApiBundle\Entity\Company", inversedBy="products")
     * @ORM\JoinColumn(name="company", referencedColumnName="id")
     */
    protected $company;

    // ...

    /**
     * Return the product company
     *
     * @return Company
     */
    public function getCompany() {
        return $this->company;
    }

    /**
     * Set the product company.
     *
     * @param Company $company
     *
     * @return Product
     */
    public function setCompany( Company $company ) {
        $this->company = $company;

        return $this;
    }
}

But when I try to execute the following code:

$product = $this->getDoctrine()->getRepository(Product::class)->findOneBy(['id' => '0642d735-fcfd-11e7-afae-0242c0a86002']);

return $this->render( '@NutritionApi/Default/index.html.twig', [ 'product' => $product ] );

And inside the index.html.twig I have this code:

{{ dump(product.company) }}

The output is the following:

"e65af24f-fd0a-11e7-afae-0242c0a86002"

While I need the full company object as output.

Do you see anything wrong with my code? I have read my code and my annotations multiple times in order to spot a mistake but I cannot find anything.

The only thing that I guess it could be the problem is the GUID id I use for the entities in my DB, but I am not sure this is the problem.

Any suggestion, please?

Upvotes: 1

Views: 945

Answers (1)

G1.3
G1.3

Reputation: 1879

You may have to remove

@ORM\Column(type="guid", name="company", nullable=false)

From Product $company property.

Upvotes: 4

Related Questions