doctrine 2 findby函数,获取键和值

我能动态获取密钥和值吗?

PHP中,你会:foreach($array as $key => $value)

但你可以这样做:

$this->_em->getRepository('Members')->findBy(array('id' =>5));

任何方式从他们的价值中得到钥匙..?

我可以通过将它变成一个数组并提取它来做到这一点,但我不会得到数组内的任何关联结果..

我想这样做,因为我希望能够提取此对象的所有属性和值,并提取其中的所有其他对象..

最佳答案 我遇到了和你现在一样的问题,经过一些研究后我发现了一个你可能感兴趣的解决方案.你需要的是键/值的关联数组而不是object.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. 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

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

更新:
要获取关联的实体,您需要定义提取连接.这是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";}

上面提到的代码将您的实体作为数组数组获取,您可以使用$keys和$values执行任何操作.
希望这可以帮助…

点赞