Symfony 如何访问细枝中的关系imageName(使用VichUploaderBundle)
我对Symfony 如何访问细枝中的关系imageName(使用VichUploaderBundle),symfony,twig,symfony4,vichuploaderbundle,Symfony,Twig,Symfony4,Vichuploaderbundle,我对{{app.user}和实体关系有问题。 我的用户与实体CustomerGroup有多通关系: ** * @ORM\Entity(repositoryClass="App\Repository\UserRepository") * @ORM\HasLifecycleCallbacks() */ class User implements UserInterface { /** * @ORM\Id() * @ORM\GeneratedValue()
{{app.user}
和实体
关系有问题。
我的用户与实体CustomerGroup
有多通关系:
**
* @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^ {#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
}
MyCustomerGroup
实体使用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;
}
在我的细枝模板中,我希望从用户访问CustomerGroup的imageName。我尝试的是:
{{app.user.CustomerGroup.imageName}
->null
{{app.user.CustomerGroup.name}}
{{app.user.CustomerGroup.imageName}}
{{app.user.getCustomerGroup().getImageName()}}
->null
{{app.user.CustomerGroup.name}}
{{app.user.CustomerGroup.imageName}}
但是,如果我这样做:`{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:
**
* @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^ {#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
}
第一次尝试仅在我使用返回CustomerGroup
实体的控制器时有效
谢谢你的帮助
最好的
朱利安我找到了一个好办法!
如果希望加载imageName
属性,则必须在模板中的某个位置加载CustomerGroup
关系。这样,实体被加载,我可以访问imageName
属性
例如:
{{app.user.CustomerGroup.imageName}
==>resultnull
{{app.user.CustomerGroup.name}}
{{app.user.CustomerGroup.imageName}}
结果:
Customer1
Customer1.png
因此,我调用了我的小树枝文件顶部的某个地方的CustomerGroup.name
(例如在body类中,然后我可以调用imageName
属性。您可以在这里{dump(app.user)}进行转储来提供更多细节吗
在你的小枝模板中?@qdequippe:完成了!谢谢!现在检查一下{dump(app.user.CustomerGroup)}
完成了!只是想澄清一下,如果我做了{app.user.CustomerGroup.name}
,它会起作用!你能尝试在用户中对CustomGroup进行额外的延迟加载吗?这里是文档,并告诉我结果