无法确定属性“文件"的访问类型 [英] Could not determine access type for property "file"

查看:26
本文介绍了无法确定属性“文件"的访问类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的图片上传有点问题,你能帮我吗:

I have a little problem with my image upload, can u help me please:

无法确定属性文件"的访问类型.

Could not determine access type for property "file".

控制器

/**
 * Creates a new Produits entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new Produits();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('adminProduits_show', array('id' => $entity->getId())));
    }

    return $this->render('EcommerceBundle:Administration:Produits/layout/new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

/**
 * Creates a form to create a Produits entity.
 *
 * @param Produits $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(Produits $entity)
{
    $form = $this->createForm(ProduitsType::class, $entity);

    $form->add('submit', SubmitType::class, array('label' => 'Ajouter'));

    return $form;
}

/**
 * Displays a form to create a new Produits entity.
 *
 */
public function newAction()
{
    $entity = new Produits();
    $form   = $this->createCreateForm($entity);

    return $this->render('EcommerceBundle:Administration:Produits/layout/new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

表格

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
    ->add('file', FileType::class, array('data_class' => null))
    ->add('name', TextType::class)
    ;
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Ecommerce\EcommerceBundle\Entity\Media'
    ));
}
/**
 * @return string
 */
public function getName()
{
    return 'ecommerce_ecommercebundle_media';
}

实体

    /**
 * @ORM\Column(name="name",type="string",length=255)
 * @Assert\NotBlank()
 */
private $name;

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

/**
 * @Assert\File(
 *     maxSize = "1024k",
 *     mimeTypes = {"image/png", "image/jpg", "image/bmp"},
 *     mimeTypesMessage = "Please upload a valid PDF"
 * )
 */
public $file;

public function getUploadRootDir()
{
    return __dir__.'/../../../../web/uploads';
}

public function getAbsolutePath()
{
    return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
}

public function getAssetPath()
{
    return 'uploads/'.$this->path;
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload()
{
    $this->tempFile = $this->getAbsolutePath();
    $this->oldFile = $this->getPath();
    $this->updateAt = new \DateTime();

    if (null !== $this->file)
        $this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension();
}

/**
 * @ORM\PostPersist()
 * @ORM\PostUpdate()
 */
public function upload()
{
    if (null !== $this->file) {
        $this->file->move($this->getUploadRootDir(),$this->path);
        unset($this->file);

        if ($this->oldFile != null) unlink($this->tempFile);
    }
}

/**
 * @ORM\PreRemove()
 */
public function preRemoveUpload()
{
    $this->tempFile = $this->getAbsolutePath();
}

/**
 * @ORM\PostRemove()
 */
public function removeUpload()
{
    if (file_exists($this->tempFile)) unlink($this->tempFile);
}

/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

public function getPath()
{
    return $this->path;
}

public function getName()
{
    return $this->name;
}

public function getFile()
{
    return $this->file;
}

/**
 * Set path
 *
 * @param string $path
 * @return String
 */
public function setPath($path)
{
    $this->path = $path;
    return $this;
}

/**
 * Set alt
 *
 * @param string $alt
 * @return String
 */
public function setAlt($alt)
{
    $this->alt = $alt;
    return $this;
}

/**
 * Set name
 *
 * @param string $name
 * @return String
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}

/**
 * Set updateAt
 *
 * @param \DateTime $updateAt
 *
 * @return Media
 */
public function setUpdateAt($updateAt)
{
    $this->updateAt = $updateAt;

    return $this;
}

/**
 * Get updateAt
 *
 * @return \DateTime
 */
public function getUpdateAt()
{
    return $this->updateAt;
}

感谢您的帮助:)

推荐答案

请在表单生成器中添加'mapped' => false,".

Please add "'mapped' => false," to form builder.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
   ->add('file', FileType::class, 
     array(
        'data_class' => null,
        'mapped' => false,            
    ))
    ->add('name', TextType::class)
    ;
 }

那些说解散是错误的,看那里的测试.不是我做错了.

Those who say that it is wrong to dissolve, see the test there. I'm not the one who made it wrong.

测试代码:https://github.com/symfony/property-access/blob/master/Tests/PropertyAccessorCollectionTest.php#L151

第二种解决方案是将函数 setXxx 添加到在 class Entity 中给出错误的属性.

A second solution is to add function setXxx to the property that gives an error in class Entity.

public $xxx;

public function setXxx(Array $xxx)
{
    $this->xxx = $xxx;
}

public function __construct()
{
    $this->xxx = new ArrayCollection();
}

视频链接:https://knpuniversity.com/screencast/doctrine-relations/create-genus-note

我的英语不好,我可以告诉你.

My English is bad, I can tell you.

这篇关于无法确定属性“文件"的访问类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆