草庐IT

php - Symfony 2 Entity Repository find()返回空数组

coder 2024-05-02 原文

谢谢大家从现在开始的回答。这就是问题所在。我有一个带有两个实体(任务和产品)的 symfony 2 应用程序。当我尝试查找 (findBy,findOneBy,findAll) 产品时,它返回一个空数组。

任务实体

    <?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;
    }

和产品实体

<?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;
    }

}

现在,当我尝试从 Controller 中查找产品时,它返回一个空数组 ({})。我看不出这有什么问题。

$productId = '18';
$product = $this->get('doctrine.orm.default_entity_manager')->getRepository('pabloUserBundle:Product')->find($productId);

最佳答案

实际上你有一个结果,它只是一个空对象,因为你还没有定义应该打印哪些属性。

最好的解决方案是让您的实体实现 JsonSerializable

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

现在它知道在将类转换为 json 对象时应该打印什么。

如果您还想要任务集合,请为任务实体实现JsonSerializable 并添加到产品实体中:

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

The JsonSerializable interface

关于php - Symfony 2 Entity Repository find()返回空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47335932/

有关php - Symfony 2 Entity Repository find()返回空数组的更多相关文章

随机推荐