【问题标题】:Symfony 2 Entity Repository find() returns empty arraySymfony 2 Entity Repository find() 返回空数组
【发布时间】:2018-04-30 08:41:01
【问题描述】:

感谢大家从现在开始的回答。这就是问题所在。我有一个带有两个实体(任务和产品)的 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;
    }

}

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

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

【问题讨论】:

  • 可以在打印对象的地方添加代码吗?
  • 这是控制器返回的(来自 Ajax 调用)-> return new JsonResponse(['success' => 1,'product' => $product]);这是浏览器中的响应 -> {"success":1,"product":{}}

标签: php symfony doctrine-orm


【解决方案1】:

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

最好的解决方案是让您的实体实现 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

【讨论】:

  • 天啊@Jannes 你现在是我的英雄!!效果很好。
猜你喜欢
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多