Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 Entity Repository find() returns empty array

Thank you all for your answers from now. Thatś the question. I have a symfony 2 app with two entities (Tasks and Products). When i tried to find (findBy,findOneBy,findAll) a product it returns an empty array.

Tasks Entity

    <?php

namespace pablo\UserBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Task
 *
 * @ORM\Table(name="tasks")
 * @ORM\Entity(repositoryClass="pablo\UserBundle\Repository\TaskRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Task
{
    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="tasks")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $user;

    /**
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="task")
     *  @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $product;

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


/**
     * Set product
     *
     * @param \pablo\UserBundle\Entity\Product $product
     *
     * @return Task
     */
    public function setProduct(\pablo\UserBundle\Entity\Product $product = null)
    {
        $this->product = $product;

        return $this;
    }

    /**
     * Get product
     *
     * @return \pablo\UserBundle\Entity\Product
     */
    public function getProduct()
    {
        return $this->product;
    }

    /**
     * @return ArrayCollection
     */
    public function getTasks()
    {
        return $this->tasks;
    }

    /**
     * @param ArrayCollection $tasks
     */
    public function setTasks($tasks)
    {
        $this->tasks = $tasks;
    }

And Products Entity

<?php

namespace pablo\UserBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Products
 *
 * @ORM\Table(name="products")
 * @ORM\Entity(repositoryClass="pablo\UserBundle\Repository\ProductsRepository")
 * @UniqueEntity("productsName")
 */
class Product
{

    /**
     * @ORM\OneToMany(targetEntity="Task", mappedBy="product")
     */
    protected $task;

    /**
     * @ORM\OneToMany(targetEntity="Publicaciones", mappedBy="product")
     */
    protected $publicaciones;

    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="products_name", type="string", length=255)
     * @Assert\NotBlank()
     */
    private $productsName;

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

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set productsName
     *
     * @param string $productsName
     *
     * @return Product
     */
    public function setProductsName($productsName)
    {
        $this->productsName = $productsName;

        return $this;
    }

    /**
     * Get productsName
     *
     * @return string
     */
    public function getProductsName()
    {
        return $this->productsName;
    }

    public function __toString() {
        return $this->productsName;
    }

    /**
     * Get task
     *
     * @return \Doctrine\Common\Collections\ArrayCollection
     */
    public function getTask()
    {
        return $this->task;
    }

    /**
     * Set task
     *
     * @param \Doctrine\Common\Collections\ArrayCollection $typeSponsor
     *
     * @return Task
     */
    public function setTask($task)
    {
        $this->task = $task;
    }

    /**
     * @return mixed
     */
    public function getPublicaciones()
    {
        return $this->publicaciones;
    }

    /**
     * @param mixed $publicaciones
     */
    public function setPublicaciones($publicaciones)
    {
        $this->publicaciones = $publicaciones;
    }

}

Now, when i tried to find a product from controller it returns an empty array ({}). I can't see what is wrong with this.

$productId = '18';
$product = $this->get('doctrine.orm.default_entity_manager')->getRepository('pabloUserBundle:Product')->find($productId);
like image 254
Pablo Gonzalez Avatar asked Dec 17 '25 06:12

Pablo Gonzalez


1 Answers

Actually you have a result, it just is an empty object because you have not defined which of the properties should be printed.

The best solution is for your entity to implement JsonSerializable.

class Product  implements \JsonSerializable
{
...
public function jsonSerialize()
{
    return [
        "id"=> $this->getId(),
        "name" => $this->getProductsName()
    ];
}

Now it knows what it should print when converting the class to json object.

If you want the task collection also, implement JsonSerializable for the Task Entity and add in Product Entity:

 public function jsonSerialize()
{
    return [
        "id"=> $this->getId(),
        "name" => $this->getProductsName(),
        "task" => $this->getTask()->toArray()
    ];
} 

The JsonSerializable interface

like image 120
Jannes Botis Avatar answered Dec 19 '25 22:12

Jannes Botis