如何访问树枝中的关系 imageName(使用 VichUploaderBundle)

How to access relation imageName in twig (using VichUploaderBundle)

我对 {{app.user}}Entity 关系有疑问。 我的用户与实体 CustomerGroup:

有 ManyToOne 关系
**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class User implements UserInterface
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\CustomerGroup")
     * @ORM\JoinColumn(nullable=false)
     */
    private $CustomerGroup;

...

我的 CustomerGroup 实体使用 VichUploaderBundle :

/**
 * @ORM\Entity(repositoryClass="App\Repository\CustomerGroupRepository")
 * @Vich\Uploadable

 */
 class CustomerGroup
 {
    /**
 * NOTE: This is not a mapped field of entity metadata, just a simple property.
 *
 * @Vich\UploadableField(mapping="customer_logo", fileNameProperty="imageName", size="imageSize")
 *
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="string", length=255, nullable=true)
 *
 * @var string
 */
private $imageName;

/**
 * @ORM\Column(type="integer", nullable=true)
 *
 * @var integer
 */
private $imageSize;

public function __construct(?File $imageFile = null)
{
    $this->customerEntities = new ArrayCollection();
    $this->models = new ArrayCollection();
    $this->masterTypes = new ArrayCollection();
    $this->documents = new ArrayCollection();
    $this->deployModels = new ArrayCollection();
    $this->imageFile = $imageFile;

    if (null !== $imageFile) {
        // It is required that at least one field changes if you are using doctrine
        // otherwise the event listeners won't be called and the file is lost
        $this->dateUpd = new \DateTimeImmutable();
    }
}

/**
 * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
 * of 'UploadedFile' is injected into this setter to trigger the update. If this
 * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
 * must be able to accept an instance of 'File' as the bundle will inject one here
 * during Doctrine hydration.
 *
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $imageFile
 */
public function setImageFile(?File $imageFile = null): void
{
    $this->imageFile = $imageFile;

    if (null !== $imageFile) {
        // It is required that at least one field changes if you are using doctrine
        // otherwise the event listeners won't be called and the file is lost
        $this->dateUpd = new \DateTimeImmutable();
    }
}

public function getImageFile(): ?File
{
    return $this->imageFile;
}

public function setImageName(?string $imageName): void
{
    $this->imageName = $imageName;
}

public function getImageName(): ?string
{
    return $this->imageName;
}

public function setImageSize(?int $imageSize): void
{
    $this->imageSize = $imageSize;
}

public function getImageSize(): ?int
{
    return $this->imageSize;
}

在我的 Twig 模板中,我想从用户那里访问 CustomerGroup 的 imageName。我试过的: {{ app.user.CustomerGroup.imageName }} -> null

{{ app.user.getCustomerGroup().getImageName() }} -> null

但是,如果我这样做:`{{ app.user.CustomerGroup.name}} --> 我得到正确的值

当我转储 {{app.user}} 时:

User^ {#824 ▼
  -id: 1
  -email: "xxxxxxxxxxxxxx"
  -roles: array:1 [▶]
  -password: "xxxxxxxxxxxxxxx"
  -CustomerGroup: CustomerGroup^ {#809 ▼
    +__isInitialized__: false
    -id: 1
    -name: null
    -abbreviation: null
    -isActive: null
    -customerEntities: null
    -dateAdd: null
    -dateUpd: null
    -createdBy: null
    -modifiedBy: null
    -models: null
    -masterTypes: null
    -documents: null
    -deployModels: null
    -imageFile: null
    -imageName: null
    -imageSize: null
     …2
  }
  -CustomerEntity: CustomerEntity^ {#754 ▶}
  -customerSites: PersistentCollection^ {#842 ▶}
  -dateAdd: DateTime @1566424800 {#827 ▶}
  -dateUpd: DateTime @1566579539 {#826 ▶}
  -createdBy: User^ {#824}
  -modifiedBy: User^ {#824}
  -firstName: "xxxxx"
  -lastName: "xxxxxx"
  -isActive: true
  -isDeleted: false
}

如果我转储 app.user.CustomerGroup:

CustomerGroup^ {#809 ▼
  +__isInitialized__: false
  -id: 1
  -name: null
  -abbreviation: null
  -isActive: null
  -customerEntities: null
  -dateAdd: null
  -dateUpd: null
  -createdBy: null
  -modifiedBy: null
  -models: null
  -masterTypes: null
  -documents: null
  -deployModels: null
  -imageFile: null
  -imageName: null
  -imageSize: null
   …2
}

只有当我在 returns CustomerGroup 实体的控制器上时,第一次尝试才有效。

感谢您的帮助

最佳,

朱利安

我找到了一个笨拙的解决方案! 如果我想要加载 imageName 属性,我必须在模板中的某处加载 CustomerGroup 关系。这样,实体就被加载了,我可以访问 imageName 属性.

示例: {{app.user.CustomerGroup.imageName}} ==> 结果 null

{{app.user.CustomerGroup.name}}
{{app.user.CustomerGroup.imageName}}

结果: Customer1 Customer1.png

所以,我在我的 twig 文件顶部的某处调用 CustomerGroup.name(例如在正文 class 中,然后我可以调用 imageName 属性 .