2015-06-05 1 views
2

Я пытаюсь получить продукты от моего выбора в объекте категории, но я даю мне следующую ошибкуОшибка: Вызов функции-члена на массиве

Error: Call to a member function getProducts() on array in /Users/jurrejan/Documents/projects/audsur2/src/Audsur/ShopBundle/Controller/DefaultController.php line 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/Entity/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

findby дать вам массив категории, используйте getProducts на одного из членов этого массива – Med

ответ

3

findBy возвращает массив объектов, которые удовлетворяют состояние. Если вы уверены, что есть только один, вы можете просто использовать findOneBy, и вы получите единую сущность.

Заканчивать http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-simple-conditions

+0

Объект имеет несколько продуктов, которые я хочу принести. Я добавил объект к вопросу выше. – dubbelj

+0

Да, но в '-> findBy (array ('name' => $ slug))' вы извлекаете массив из 1 категории, а не одну категорию, как вы бы получили с findOneBy. Посмотрите на код, который вы только что внесли: 'array (1) {'. Вы также можете сохранить этот массив в переменной '$ categories' и запустить' $ categories [0] -> getProducts() ', если хотите. – nacmartin

+1

Спасибо за объяснение! – dubbelj

Смежные вопросы