学说2 findby函数,获取键和值
我可以动态获取键和值吗?
在 php 中,您可以: foreach ($array as $key => $value)
但您可以这样做:
$this->_em->getRepository('Members')->findBy(array('id' =>5));
有什么方法可以从中获取键及其值..?
我可以通过将其转换为数组并提取它来做到这一点,但我不会在数组内得到任何关联结果..
我想这样做,因为我希望能够提取该对象的所有属性和值并提取所有其他对象里面也有..
can i get the keys and values dynamically.
in php, you would: foreach ($array as $key => $value)
but you can do this for :
$this->_em->getRepository('Members')->findBy(array('id' =>5));
any way to get the keys from this with their values..?
i can do this by turning it into an array and extract it but i wouldnt get any association results inside the array ..
i want to do this as i want to be able to extract all properties and values of this object and extract all other objects within it too..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了与您现在相同的问题,经过一些研究,我刚刚找到了您可能感兴趣的解决方案。您需要的是键/值的关联数组,而不是对象。findBy()方法仅返回实体 OBJECT.so您将需要使用 DQL(教义查询语言)。
来自学说文档:
要了解有关“查询生成器”的更多信息,请参阅 doctrine2 文档
更新:
要获取关联实体,您需要定义获取连接。这是 教义文档:
上面提到的代码将以数组数组的形式获取您的实体,您可以做任何您想做的事情想要 $keys 和 $values。
希望这有帮助...
Ih had the same issue as you now have and after some research i just found a solution which you might be interested.what you need is an associative array of keys/values and not an object.findBy()method only returns entity OBJECT.so you will need to use DQL(doctrine query language).
from doctrine documentation:
To learn more about 'The Query Builder' please refer to doctrine2 documentation
Update:
To fetch associated Entities you will need to define fetch joins.Here is an example provided in doctrine documentation:
The code mentioned above will fetch your entities as array of arrays and you can do whatever you want with $keys and $values.
Hope this helps...