我试图从我的选择中获取Category对象中的产品,但我给了我以下错误
错误:在数组中调用成员函数getProducts() /Users/jurrejan/Documents/projects/audsur2/src/Audsur/ShopBundle/Controller/DefaultController.php 第94行
我哪里错了?
该对象有多个产品
array(1) {
[0]=>
object(stdClass)#329 (6) {
["__CLASS__"]=>
string(33) "Audsur\ShopBundle\Entity\Category"
["id"]=>
int(4)
["name"]=>
string(8) "Receiver"
["slug"]=>
string(8) "receiver"
["description"]=>
string(5) "descr"
["products"]=>
array(47) {
[0]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[1]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[2]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[3]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[4]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[5]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[6]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[7]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[8]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[9]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[10]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[11]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[12]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[13]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[14]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[15]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[16]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[17]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[18]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[19]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[20]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[21]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[22]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[23]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[24]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[25]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[26]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[27]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[28]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[29]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[30]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[31]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[32]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[33]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[34]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[35]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[36]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[37]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[38]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[39]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[40]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[41]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[42]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[43]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[44]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[45]=>
string(32) "Audsur\ShopBundle\Entity\Product"
[46]=>
string(32) "Audsur\ShopBundle\Entity\Product"
}
}
}
控制器
<?php
namespace Audsur\ShopBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Audsur\ShopBundle\Entity\Category;
use Audsur\ShopBundle\Entity\Brand;
use Audsur\ShopBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
class DefaultController extends Controller
{
/* removed irrelevant functions */
public function getGroupAction($slug, $type = null, $grouped = true)
{
$group = $this->getDoctrine()
->getRepository('AudsurShopBundle:'.$type)
->findBy(array( 'name' => $slug ))
->getProducts();
return $this->render('AudsurShopBundle:Default:productOverview.html.twig', array(
'group' => $group
)
);
}
}
的src / Audsur / ShopBundle /实体/ Category.php
<?php
namespace Audsur\ShopBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="category")
*/
class Category
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100, unique=true)
*/
protected $name;
/**
* @ORM\Column(type="string", length=255, nullable=false, unique=true)
*/
protected $slug;
/**
* @ORM\Column(type="text")
*/
protected $description;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="category")
*/
protected $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set slug
*
* @param string $slug
* @return Category
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* @return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set description
*
* @param string $description
* @return Category
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Add products
*
* @param \Audsur\ShopBundle\Entity\Product $products
* @return Category
*/
public function addProduct(\Audsur\ShopBundle\Entity\Product $products)
{
$this->products[] = $products;
return $this;
}
/**
* Remove products
*
* @param \Audsur\ShopBundle\Entity\Product $products
*/
public function removeProduct(\Audsur\ShopBundle\Entity\Product $products)
{
$this->products->removeElement($products);
}
/**
* Get products
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getProducts()
{
return $this->products;
}
}
答案 0 :(得分:8)
findBy
返回满足条件的实体数组。如果您确定只有一个,那么您只需使用findOneBy
即可获得一个实体。
答案 1 :(得分:0)
就我而言,我试图一次为所有对象设置属性值。 但这不应该那样。
最后,我使用了 foreach 来解决问题。
foreach ($notifications as $notification){
$notification->setSeen(1)->setSeenAt(new \DateTime());
}
$this->getDoctrine()->getEntityManager()->flush();
我很好。