【发布时间】:2019-09-25 16:36:46
【问题描述】:
我正在尝试按列名在表格行中查找值:
$entity = $this->getDoctrine()->getRepository($EntityName)->findOneBy(['uuid' => $uuid]);
$result = $entity->getCat();
困难在于,我希望能够用变量替换“Cat”。
很遗憾这是不可能的:
$myvariable = "Cat";
$result = $entity->'get'.$myvariable.();
所以我换了一种方法:
$entity = $this->getDoctrine()->getRepository($EntityName)->findBy(array('uuid' => $uuid));
$result = array_search($myvariable, $entity);
但在这里我得到一个空输出。
另一种方法:
foreach ($entity as $key => $value) {
if($myvariable == $key){
$result = $value;
}
}
这里的错误信息是:
在渲染模板期间引发了异常 (“可捕获的致命错误:App\Entity\Documents 类的对象可能 不能转换成字符串”)。
我只是很难找到实现我想要的正确方法。
【问题讨论】:
标签: arrays symfony object variables entity