gpt4 book ai didi

php - Symfony 按实体获取数据库表数据

转载 作者:行者123 更新时间:2023-11-30 22:07:02 25 4
gpt4 key购买 nike

我有两个实体。 类别网店。我需要通过 Eshop 实体获取所有类别。代码工作。返回的数组包含表数据,但返回的数组太大,所以我不知道如何提取我想要的。方法看。该方法如下所示:

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

当我被例子启发时here ,这样我得到以下输出:

代码

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

输出

[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)
}

我需要提取["name"]吗?

非常感谢您的任何建议。

最佳答案

有点不清楚。

获取对象属性的方法有很多:

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

或者在 Twig 上

{% for category in eshop.categories %}
{{ category.name }}
{% endfor %}

关于php - Symfony 按实体获取数据库表数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41358544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com