php - 如何将实体查询结果作为对象获取
问题描述
$contacts = $this->getDoctrine()->getManager()
->getRepository(Contact::class)
->createQueryBuilder('c')
->select('c')
->where('c.author = :id')
->setParameter('id',$this->getUser()->getId())
->getQuery()->getResult();
我对 symfony 和教义非常陌生,但我试图得到它。
所以我有一些查询构建的结果作为教义,你可以
我如何得到这个结果作为 Json
只要我知道如何循环它
foreach($contact as $contact){
$contact->getId() // or whatever
}
除此之外,我想将所有结果作为 1 个数组对象
我认为问题标签是错误的希望有人会为我解决它,因为我没有解释它多么抱歉
这是联系实体
class Contact
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=25)
*/
private $Name;
/**
* @ORM\Column(type="string", length=10)
* @Assert\NotBlank(message="Утасны дугаар оруулна уу")
*/
private $mobile;
/**
* @ORM\Column(type="string", length=25, nullable=true)
*/
private $email;
/**
* @ORM\Column(type="string", length=10, nullable=true)
* @Assert\NotBlank(message="Утасны дугаар оруулна уу")
*/
private $mobilesecond;
/**
* @ORM\Column(type="string", length=25, nullable=true)
*/
private $work;
/**
* @ORM\Column(type="string", length=25, nullable=true)
*/
private $position;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\user", inversedBy="contacts")
*/
private $author;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->Name;
}
public function setName(string $Name): self
{
$this->Name = $Name;
return $this;
}
public function getEmail(): ?string
{
return $this->email;
}
public function setEmail(string $email): self
{
$this->email = $email;
return $this;
}
public function getMobile(): ?string
{
return $this->mobile;
}
public function setMobile(string $mobile): self
{
$this->mobile = $mobile;
return $this;
}
public function getMobilesecond(): ?string
{
return $this->mobilesecond;
}
public function setMobilesecond(string $mobilesecond): self
{
$this->mobilesecond = $mobilesecond;
return $this;
}
public function getWork(): ?string
{
return $this->work;
}
public function setWork(string $work): self
{
$this->work = $work;
return $this;
}
public function getPosition(): ?string
{
return $this->position;
}
public function setPosition(string $position): self
{
$this->position = $position;
return $this;
}
public function getAuthor(): ?user
{
return $this->author;
}
public function setAuthor(?user $author): self
{
$this->author = $author;
return $this;
}
}
我有测试联系人数据
我想我更喜欢这种物体
{
['51','dfgds','sfg','gfsgfs','s','fg','fgs','1'],
['54','dfg','gdf','gd','fgdf','dfgdg','fgdg','1'],
['55','wer','','ewe','','','erwrewwer','1'],
}
或任何东西
解决方案
如果您在控制器中,则可以通过 => return new JsonResponse ($ objectList) 返回对象
推荐阅读
- amazon-data-pipeline - AWS Data Pipeline:将 CSV 文件从 S3 上传到 DynamoDB
- swift - 为什么我在 Xcode 中的 pod init 期间收到加载错误?
- ios - Safari 未能使用 Angular 的 HttpClient 发送有效的 GET 请求
- javascript - 在 React 中传递数据的 Fetch/TemplateLiteral 问题
- flutter - Flutter 中的条子是否可以同时具有“扩展”和“收缩”效果?
- react-native - 如何从我的反应原生应用程序中删除“dropped s far”?
- vue.js - 如何在 nativescript vue 中制作反应数组?
- r - 我在 R 中安装 ConvergenceConcepts 包时遇到问题,它会抛出一条错误消息,指出“ConvergenceConcepts”的退出状态非零
- rust - 如何将所有权转移给调用函数?
- c++ - 如何将 ascii 文本的 char[] 转换为 uint8_t[] 字节