1

I have two entities. Category and Eshop. I need get all categories by Eshop entity. The code working. Returned array contain table data, but returned array is too big, so I don't know how extract what I want. Method look. The method looks like this:

/**
 * @param Eshop $eshop
 * @return Category[]
 */
private function getCategoriesFromDatabaseByEshop(Eshop $eshop)
{
    return $eshop->getCategories();
}

When I was inspired by the example here, so that I get the following output:

Code

print_r(\Doctrine\Common\Util\Debug::dump($this->getCategoriesFromDatabaseByEshop($eshop)));

Output

[84]=> object(stdClass)#2799 (12) {
    ["__CLASS__"]=> string(25) "AppBundle\Entity\Category"
    ["id"]=> int(85)
    ["name"]=> string(42) "/akcni-nabidky/akcni-nabidky-produkty/vina"
    ["parentCategory"]=> NULL
    ["link"]=> string(42) "/akcni-nabidky/akcni-nabidky-produkty/vina"
    ["createdAt"]=> string(8) "DateTime"
    ["lastCheckAt"]=> string(8) "DateTime"
    ["lastHttpStatusCode"]=> int(200)
    ["active"]=> bool(true)
    ["eshop"]=> string(22) "AppBundle\Entity\Eshop"
    ["products"]=> string(8) "Array(0)"
    ["leaf"]=> bool(false)
}

I need extract ["name"]?

Thank you very much for any advice.

Community
  • 1
  • 1
Radek
  • 43
  • 5

1 Answers1

0

It's a bit unclear.

You have many way to get the object property:

$names = [];
foreach ($eshop->getCategories() as $category) {
   $names[] = $category->getName()
}
return $names

or in twig

{% for category in eshop.categories %}
   {{ category.name }}
{% endfor %}
goto
  • 7,908
  • 10
  • 48
  • 58