错误:在数组上调用成员函数

我正在尝试从Category对象中的选择中获取产品,但出现以下错误

错误:在/Users/jurrejan/Documents/projects/audsur2/src/Audsur/ShopBundle/Controller/DefaultController.php第94行中的数组上调用成员函数getProducts()

我要去哪里错了?

该对象具有多个产品

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;
    }
}
纳马汀

findBy返回满足条件的实体数组。如果确定只有一个,则可以改用findOneBy而获得单个实体。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Laravel在数组错误上调用成员函数toArray()

致命错误:在数组上调用成员函数getAttributes()

错误:在数组 Symfony 3 上调用成员函数 contains()

在数组上调用成员函数move()

在数组上调用成员函数failure()

Laravel,jQuery文件上传错误-“在数组上调用成员函数store()”

在数组laravel上调用成员函数save()

在数组 laravel 5.4 上调用成员函数 first()

流明授权-在数组上调用成员函数parameter()

laravel-在数组上调用成员函数map()

Laravel Dusk-在数组上调用成员函数click()

Laravel 5.8:在数组上调用成员函数 slice()

在数组C上调用排序函数

错误:在 null 上调用成员函数 setPayrollperiodid()

错误:在null上调用成员函数add()

CodeIgniter错误-在null上调用成员函数

在对象上调用方法得到“调用数组上的成员函数”错误

错误致命错误:在非对象上调用成员函数insert()

致命错误:在非对象错误上调用成员函数prepare()

JS无法在数组项目上调用函数

我如何解决在数组错误时调用成员函数 only()

在laravel中的数组上调用成员函数links()

在Laravel中的数组上调用成员函数paginate()

在codeigniter中的数组上调用成员函数result()

Laravel-错误:在null上调用成员函数fullName()

PHP CURL致命错误:在null上调用成员函数getAttribute()

致命错误:在非对象上调用成员函数isUploaded()

致命错误:在布尔值上调用成员函数count()

致命错误:在非对象上调用成员函数isVirtual()