我在两个实体“user”和“file manager”之间存在多种关系
用户可以拥有多个文件
用户只能拥有一个头像
我正在处理这样的关系:class Users extends BaseUser
{
/**
* @var File
* @ORM\ManyToOne(targetEntity="App\Entity\File",cascade={"persist"})
* @Assert\Valid()
*/
private $avatar;
/**
* @var ArrayCollection
* @ORM\OneToMany(targetEntity="App\Entity\File",mappedBy="user",cascade={"persist"})
*/
private $files;
public function __construct()
{
$this->sessions = new ArrayCollection();
$this->departments = new ArrayCollection();
$this->jobTitles = new ArrayCollection();
$this->sessionMessages = new ArrayCollection();
$this->files = new ArrayCollection();
parent::__construct();
}
/**
* @return File
*/
public function getAvatar()
{
return $this->avatar;
}
/**
* @param File $avatar
*/
public function setAvatar(File $avatar=null)
{
$this->avatar = $avatar->getId();
$avatar->setUser($this);
}
/**
* @return Collection|File[]
*/
public function getFiles()
{
return $this->files;
}
public function addFile(File $file)
{
if (!$this->files->contains($file) && !is_null($file)) {
$this->files[] = $file;
$file->setUser($this);
}
return $this;
}
public function removeFile(File $file)
{
if ($this->files->contains($file)) {
$this->files->removeElement($file);
// set the owning side to null (unless already changed)
if ($file->getUser() === $this) {
$file->setUser(null);
}
}
return $this;
}
}
这是我的文件实体
class File
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="boolean",nullable=true)
*
*/
private $status;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*/
private $path;
/**
* @var Users
* @ORM\ManyToOne(targetEntity="App\Entity\Users",inversedBy="files")
*/
private $user;
/**
*@Assert\File()
*/
private $file;
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
public function getId()
{
return $this->id;
}
public function getStatus(): ?bool
{
return $this->status;
}
public function setStatus(bool $status): self
{
$this->status = $status;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(string $path): self
{
$this->path = $path;
return $this;
}
public function getUser(): ?Users
{
return $this->user;
}
public function setUser($user)
{if (!is_null($this->file)){
$this->user = $user;
}
return $this;
}
}
问题是当我提交我的注册表单时,我有一个错误,说文件无法上传第一,我想这是因为我的formType就是这样:
class RegistrationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('avatar', FileManagerType::class,[
'data_class'=>File::class,
'by_reference'=>false
])
->add('files', CollectionType::class,[
"entry_type"=> UserFileManagerType::class,
"allow_add" => true,
"allow_delete"=>true
])
. . .
但事实并非如此,问题是提交后avatar的fileType的id为null,因此无法上传。
答案 0 :(得分:2)
在检查了我的代码和symfony文档后,我终于意识到可以通过更改我的用户类来解决这个问题:
public function setAvatar(File $avatar=null)
{
$this->avatar = $avatar->getId();
$avatar->setUser($this);
}
正如你所看到的那样容易但是我的时间很多,我希望它有助于其他
我在代码中发现了另一个导致文件错误的问题, 我的fileName由于某种原因无效,所以移动函数返回false所以我用以下替换它并且它正常工作
$fileName = $files->getFilename().'.'.$files->guessExtension();
$result= $files->move($this->resumePath,$fileName);