学说2 findby函数,获取键和值

发布于 2024-12-05 04:54:59 字数 335 浏览 0 评论 0原文

我可以动态获取键和值吗?

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2024-12-12 04:54:59

我遇到了与您现在相同的问题,经过一些研究,我刚刚找到了您可能感兴趣的解决方案。您需要的是键/值的关联数组,而不是对象。findBy()方法仅返回实体 OBJECT.so您将需要使用 DQL(教义查询语言)。

//create a QueryBuilder instance
$qb = $this->_em->createQueryBuilder();
$qb->add('select', 'a')
//enter the table you want to query
->add('from', 'Members a')
->add('where', 'a.id = :id')
//order by username if you like
//->add('orderBy', 'a.username ASC')
//find a row with id=5
->setParameter('id', '5');
query = $qb->getQuery();
//if you dont put 3 or Query::HYDRATE_ARRAY inside getResult() an object is returned and if you put 3 an array is returned
$accounts = $query->getResult(3);

来自学说文档:

13.7.4。水合模式

每种水合模式都会对结果进行假设
返回到用户土地。您应该了解所有要制作的细节
充分利用不同的结果格式:

不同水合模式的常数为:
查询::HYDRATE_OBJECT
查询::HYDRATE_ARRAY
查询::HYDRATE_SCALAR
查询::HYDRATE_SINGLE_SCALAR

要了解有关“查询生成器”的更多信息,请参阅 doctrine2 文档

更新:
要获取关联实体,您需要定义获取连接。这是 教义文档

$dql = "SELECT b, e, r, p FROM Bug b JOIN b.engineer e ".
   "JOIN b.reporter r JOIN b.products p ORDER BY b.created DESC";
$query = $entityManager->createQuery($dql);
$bugs = $query->getArrayResult();

foreach ($bugs AS $bug) {
  echo $bug['description'] . " - " . $bug['created']->format('d.m.Y')."\n";
  echo "    Reported by: ".$bug['reporter']['name']."\n";
  echo "    Assigned to: ".$bug['engineer']['name']."\n";
foreach($bug['products'] AS $product) {
  echo "    Platform: ".$product['name']."\n";}
  echo "\n";}

上面提到的代码将以数组数组的形式获取您的实体,您可以做任何您想做的事情想要 $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).

//create a QueryBuilder instance
$qb = $this->_em->createQueryBuilder();
$qb->add('select', 'a')
//enter the table you want to query
->add('from', 'Members a')
->add('where', 'a.id = :id')
//order by username if you like
//->add('orderBy', 'a.username ASC')
//find a row with id=5
->setParameter('id', '5');
query = $qb->getQuery();
//if you dont put 3 or Query::HYDRATE_ARRAY inside getResult() an object is returned and if you put 3 an array is returned
$accounts = $query->getResult(3);

from doctrine documentation:

13.7.4. Hydration Modes

Each of the Hydration Modes makes assumptions about how the result is
returned to user land. You should know about all the details to make
best use of the different result formats:

The constants for the different hydration modes are:
Query::HYDRATE_OBJECT
Query::HYDRATE_ARRAY
Query::HYDRATE_SCALAR
Query::HYDRATE_SINGLE_SCALAR

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:

$dql = "SELECT b, e, r, p FROM Bug b JOIN b.engineer e ".
   "JOIN b.reporter r JOIN b.products p ORDER BY b.created DESC";
$query = $entityManager->createQuery($dql);
$bugs = $query->getArrayResult();

foreach ($bugs AS $bug) {
  echo $bug['description'] . " - " . $bug['created']->format('d.m.Y')."\n";
  echo "    Reported by: ".$bug['reporter']['name']."\n";
  echo "    Assigned to: ".$bug['engineer']['name']."\n";
foreach($bug['products'] AS $product) {
  echo "    Platform: ".$product['name']."\n";}
  echo "\n";}

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文