Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Call to a member function on array

I'm trying to get the products from my selection in the Category object but I gives me the following error

Error: Call to a member function getProducts() on array in /Users/jurrejan/Documents/projects/audsur2/src/Audsur/ShopBundle/Controller/DefaultController.php line 94

Where am I going wrong?

The object has multiple products

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"
    }
  }
}

Controller

<?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;
    }
}
like image 787
dubbelj Avatar asked Jun 05 '15 12:06

dubbelj


1 Answers

findBy returns an array of entities that satisfy the condition. If you are sure there is only one, you can just use findOneBy instead and you will get a single entity.

like image 105
nacmartin Avatar answered Oct 13 '22 10:10

nacmartin