Symfony 2教义2一对多关系不返回数据 [英] Symfony 2 Doctrine 2 one to many relationship not returning data

查看:79
本文介绍了Symfony 2教义2一对多关系不返回数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间后,我回到了教义。我有一些教条和symfony的问题,我根本无法想像,为什么会这样。一个帮助会很好。



我有两个实体:
1.类别:

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


/ **
* @ ORM\OneToMany(targetEntity =Items,mappedBy =category)
* /
private $项目;


public function __construct()
{
$ this-> items = new \Doctrine\Common\Collections\ArrayCollection();
}



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

/ **
*
* @param项目$ items
* @return类别
* /
public函数addItem(Items $ item)
{
$ this-> items [] = $ item;

return $ this;
}

/ **
*
* @param项目$ item
* /
public function removeItem(Items $ item)
{
$ this-> items-> removeElement($ item);
}

/ **
* @return项目
* /
public function getItems()
{
return $这 - >物品;
}

其次是:项目

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

/ **
* @var整数
*
* @ ORM\ManyToOne(targetEntity =Category,inversedBy =items)
* @ ORM\JoinColumn(name =category_id,referencedColumnName =id)
* /
private $ category;


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

/ **
*设置类别
*
* @param类别$类别
* @return项目
* /
public function setCategory(Category $ category = null)
{
$ this-> category = $ category;

return $ this;
}

/ **
*获取类别
*
* @return类别
* /
public function getCategory )
{
return $ this-> category;
}

现在在Controller中,我只是想通过类别检索项目: / p>

  $ categories = $ em-> getRepository('Category') - > findAll(); 
//只是例子
foreach($ categories as $ category){
print_r($ category-> getItems());
}

应该是$ category-> getItems()返回该类别的项目?或者我有幻想,它应该工作?请建议。



此处返回null,但不存在数据库中的数据错误。



表结构是:

  CREATE TABLE`category`(

`id` int(11)NOT NULL AUTO_INCREMENT,

PRIMARY KEY(`id`)

)ENGINE = InnoDB AUTO_INCREMENT = 14 DEFAULT CHARSET = utf8
pre>

  CREATE TABLE`items`(

`id` int(11)NOT NULL AUTO_INCREMENT,

`category_id` int(11)DEFAULT NULL,

PRIMARY KEY(`id`)

KEY`fk_category`(`category_id`),

CONSTRAINT`fk_category` FOREIGN KEY(`category_id`)参考`Category`(`id`)

)ENGINE = InnoDB AUTO_INCREMENT = 8 DEFAULT CHARSET = utf8

谢谢

解决方案

2年以后..我也希望通过最新的Symfony和Doctrine以及$ category-> getItems()



我必须做的是,在你的情况下:



控制器:

  $ items = $ em-> getRepository('AppBundle:Items') - > findByCategory($ category); 

由于某些原因 - > getItems()为空。


I got back to doctrine after a while. I have some problem with doctrine and symfony which I can't figure out at all, why it could be. A help would be nice.

I've got two entities: 1. Category:

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


/**
 * @ORM\OneToMany(targetEntity="Items", mappedBy="category")
 */
private $items;


public function __construct() 
{
    $this->items = new \Doctrine\Common\Collections\ArrayCollection();
}



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

/**
 *
 * @param Items $items
 * @return Category
 */
public function addItem(Items $item)
{
    $this->items[] = $item;

    return $this;
}

/**
 *
 * @param Items $item
 */
public function removeItem(Items $item)
{
    $this->items->removeElement($item);
}

/**
 * @return Items
 */
public function getItems()
{
    return $this->items;
}

And Second is: Items

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

 /**
 * @var integer
 *
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="items")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 */
 private $category;


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

 /**
 * Set category
 *
 * @param Category $category
 * @return Items
 */
 public function setCategory(Category $category = null)
 {
    $this->category = $category;

    return $this;
 }

 /**
  * Get category
  *
  * @return Category 
  */
 public function getCategory()
 {
    return $this->category;
 }

Now in Controller, I'm simply trying to retrieve items via categories:

    $categories = $em->getRepository('Category')->findAll();  
    //just example 
    foreach ($categories as $category) {
        print_r($category->getItems());
    }

Should $category->getItems() return the Items of that category? Or I'm in illusion that it should work? Please suggest.

It returns null at the moment but not errors although there is data in database.

The table structure is:

  CREATE TABLE `category` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

  PRIMARY KEY (`id`)

 ) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8

And

 CREATE TABLE `items` (

 `id` int(11) NOT NULL AUTO_INCREMENT,

 `category_id` int(11) DEFAULT NULL,

  PRIMARY KEY (`id`),

  KEY `fk_category` (`category_id`),

 CONSTRAINT `fk_category` FOREIGN KEY (`category_id`) REFERENCES `Category` (`id`)

 ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8

Thanks

解决方案

2 years later.. I'm also looking to make this work with the latest Symfony and Doctrine and by $category->getItems()

What I had to do is, in your case:

Controller:

$items = $em->getRepository('AppBundle:Items')->findByCategory($category);

For some reason ->getItems() is empty.

这篇关于Symfony 2教义2一对多关系不返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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