如何创建具有教义关系,joincolumn和ManyToOne的数组?

时间:2018-12-06 17:12:50

标签: arrays symfony doctrine associations entity

我将两个实体与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;
  }
}

在我的控制器中,我通过“教义”将其称为:

$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"
  }
]

1 个答案:

答案 0 :(得分:1)

这不是教义ORM本质上的工作方式。

您可以向__toString实体添加Icons方法。 这样你就能回声 在树枝上使用{{ page.icon }}` and not {{page.icon.name}}`

有帮助吗?

具有图标“实例”的问题是什么?