У меня проблема с отношением {{app.user}}
и Entity
.У моего пользователя есть связь ManyToOne с сущностью 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
сущность использует 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 я хочу получить доступ к группе клиентов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
}
Только первая попыткаработает, когда я на контроллере, который возвращает объект CustomerGroup
.
Спасибо за вашу помощь
Best,
Julien