如何创建一个包含学说关系、joincolumn 和 ManyToOne 的数组?
How can I create an array with doctrine relations, joincolumn and ManyToOne?
我将两个实体与 ManyToOne
相关联:
* @ORM\ManyToOne(targetEntity="Icons")
* @ORM\JoinColumn(name="icon", referencedColumnName="id")
这是 pages
实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PagesRepository")
*/
class Pages
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=10, unique=true)
*/
private $unique_id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $template;
/**
* @var \Icons
*
* @ORM\ManyToOne(targetEntity="Icons")
* @ORM\JoinColumn(name="icon", referencedColumnName="id")
*/
private $icon;
public function getIcon(): ?Icons
{
return $this->icon;
}
public function setIcon(?Icons $icon): self
{
$this->icon = $icon;
return $this;
}
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
public function getId()
{
return $this->id;
}
public function setUniqueId(string $unique_id): self
{
$this->unique_id = $unique_id;
return $this;
}
public function getUniqueId(): ?string
{
return $this->unique_id;
}
public function setUnique_id(string $unique_id): self
{
$this->unique_id = $unique_id;
return $this;
}
public function getUnique_id(): ?string
{
return $this->unique_id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTemplate(): ?string
{
return $this->template;
}
public function setTemplate(string $template): self
{
$this->template = $template;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}
在我的控制器中,我通过 Doctrine 调用它:
$pages = $this->getDoctrine()->getRepository(Pages::class)->findAll();
输出为:
array:5 [▼
0 => Pages {#6356 ▶}
1 => Pages {#6401 ▶}
2 => Pages {#6402 ▶}
3 => Pages {#6403 ▶}
4 => Pages {#6404 ▼
-id: 5
-unique_id: "90c29507fd"
-name: "Felder"
-template: ""
-icon: Icons {#6396 ▼
+__isInitialized__: true
-id: 1
-name: "adjust"
…2
…2}
-slug: "fields"
}
]
但我需要的是这个输出:
array:5 [▼
0 => Pages {#6356 ▶}
1 => Pages {#6401 ▶}
2 => Pages {#6402 ▶}
3 => Pages {#6403 ▶}
4 => Pages {#6404 ▼
-id: 5
-unique_id: "90c29507fd"
-name: "Felder"
-template: ""
-icon: "adjust"
-slug: "fields"
}
]
这不是 Doctrine ORM 天生的工作方式。
您可以将 __toString
方法添加到 Icons
实体。
这样你就可以回显
在树枝中使用 {{ page.icon }}` and not
{{ page.icon.name }}`
有帮助吗?
有图标 "instance" 有什么问题?
我将两个实体与 ManyToOne
相关联:
* @ORM\ManyToOne(targetEntity="Icons")
* @ORM\JoinColumn(name="icon", referencedColumnName="id")
这是 pages
实体:
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\PagesRepository")
*/
class Pages
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=10, unique=true)
*/
private $unique_id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $template;
/**
* @var \Icons
*
* @ORM\ManyToOne(targetEntity="Icons")
* @ORM\JoinColumn(name="icon", referencedColumnName="id")
*/
private $icon;
public function getIcon(): ?Icons
{
return $this->icon;
}
public function setIcon(?Icons $icon): self
{
$this->icon = $icon;
return $this;
}
/**
* @ORM\Column(type="string", length=255)
*/
private $slug;
public function getId()
{
return $this->id;
}
public function setUniqueId(string $unique_id): self
{
$this->unique_id = $unique_id;
return $this;
}
public function getUniqueId(): ?string
{
return $this->unique_id;
}
public function setUnique_id(string $unique_id): self
{
$this->unique_id = $unique_id;
return $this;
}
public function getUnique_id(): ?string
{
return $this->unique_id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getTemplate(): ?string
{
return $this->template;
}
public function setTemplate(string $template): self
{
$this->template = $template;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
}
在我的控制器中,我通过 Doctrine 调用它:
$pages = $this->getDoctrine()->getRepository(Pages::class)->findAll();
输出为:
array:5 [▼
0 => Pages {#6356 ▶}
1 => Pages {#6401 ▶}
2 => Pages {#6402 ▶}
3 => Pages {#6403 ▶}
4 => Pages {#6404 ▼
-id: 5
-unique_id: "90c29507fd"
-name: "Felder"
-template: ""
-icon: Icons {#6396 ▼
+__isInitialized__: true
-id: 1
-name: "adjust"
…2
…2}
-slug: "fields"
}
]
但我需要的是这个输出:
array:5 [▼
0 => Pages {#6356 ▶}
1 => Pages {#6401 ▶}
2 => Pages {#6402 ▶}
3 => Pages {#6403 ▶}
4 => Pages {#6404 ▼
-id: 5
-unique_id: "90c29507fd"
-name: "Felder"
-template: ""
-icon: "adjust"
-slug: "fields"
}
]
这不是 Doctrine ORM 天生的工作方式。
您可以将 __toString
方法添加到 Icons
实体。
这样你就可以回显
在树枝中使用 {{ page.icon }}` and not
{{ page.icon.name }}`
有帮助吗?
有图标 "instance" 有什么问题?