Symfony 2实体存储库find()返回空数组 [英] Symfony 2 Entity Repository find() returns empty array

查看:90
本文介绍了Symfony 2实体存储库find()返回空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



任务实体

 <?php 

名称空间pablo\UserBundle\Entity;

使用Doctrine\Common\Collections\ArrayCollection;
使用Doctrine\ORM\映射作为ORM;
使用Symfony\Component\Validator\Constraints作为断言;

/ **
*任务
*
* @ 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 =级联)
* /
受保护的$ user;

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

公共功能__construct()
{
$ this-> tasks = new ArrayCollection();
}


/ **
*设置产品
*
* @param \pablo\UserBundle\Entity\产品$ product
*
* @返回任务
* /
公共功能setProduct(\pablo\UserBundle\Entity\Product $ product = null)
{
$ this-> product = $ product;

返回$ this;
}

/ **
*获取产品
*
* @return \pablo\UserBundle\Entity\Product
* /
公共函数getProduct()
{
return $ this-> product;
}

/ **
* @return ArrayCollection
* /
公共函数getTasks()
{
return $这个任务
}

/ **
* @param ArrayCollection $ tasks
* /
公共函数setTasks($ tasks)
{
$ this->任务= $任务;
}

和产品实体

 <?php 

名称空间pablo\UserBundle\Entity;

使用Doctrine\Common\Collections\ArrayCollection;
使用Doctrine\ORM\映射作为ORM;
使用Symfony\Component\Validator\Constraints作为断言;
使用Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

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

/ **
* @ ORM\OneToMany(targetEntity = Task,mappingBy = product)
* /
受保护的$ task;

/ **
* @ ORM\OneToMany(targetEntity = Publicaciones,mappingBy = product)
* /
受保护的$ publicaciones;

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

/ **
* @变量字符串
*
* @ ORM\Column(name = products_name,type = string,length = 255 )
* @ Assert\NotBlank()
* /
private $ productsName;

公共功能__construct()
{
$ this-> task = new ArrayCollection();
}

/ **
*获取ID
*
* @return int
* /
public function getId( )
{
返回$ this-> id;
}

/ **
*设置productsName
*
* @param string $ productsName
*
* @return产品
* /
公共功能setProductsName($ productsName)
{
$ this-> productsName = $ productsName;

返回$ this;
}

/ **
*获取产品名称
*
* @返回字符串
* /
公共函数getProductsName( )
{
return $ this-> productsName;
}

公共函数__toString(){
return $ this-> productsName;
}

/ **
*获取任务
*
* @return \Doctrine\Common\Collections\ArrayCollection
* /
公共函数getTask()
{
return $ this-> task;
}

/ **
*设置任务
*
* @param \Doctrine\Common\Collections\ArrayCollection $ typeSponsor
*
* @返回任务
* /
公共功能setTask($ task)
{
$ this-> task = $ task;
}

/ **
* @返回混合的
* /
公共函数getPublicaciones()
{
return $这->公众;
}

/ **
* @param mixed $ publicaciones
* /
公共功能setPublicaciones($ publicaciones)
{
$ this-> publicaciones = $ publicaciones;
}

}

现在,当我尝试查找时控制器中的产品返回空数组({})。我看不出这是怎么回事。

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


解决方案

实际上,您得到的结果只是一个空



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

 类产品实现\JsonSerializable 
{
...
公共函数jsonSerialize()
{
return [
id => $ this-> getId(),
name => $ this-> getProductsName()
];
}

现在,它知道将类转换为json对象时应打印的内容。 / p>

如果您还希望收集任务,请为任务实体实施 JsonSerializable 并添加产品实体:

 公共函数jsonSerialize()
{
return [
id => $ this-> getId(),
name => $ this-> getProductsName(),
task => $ this-> getTask()-> toArray()
];
}

JsonSerializable接口


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);

解决方案

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

这篇关于Symfony 2实体存储库find()返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆