简体   繁体   English

无法确定属性“文件”的访问类型

[英]Could not determine access type for property "file"

I have a little problem with my image upload, can u help me please:我的图片上传有点问题,你能帮我吗:

Could not determine access type for property "file".无法确定属性“文件”的访问类型。

Controller控制器

/**
 * 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(),
    ));
}

Form形式

/**
 * @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';
}

Entity实体

    /**
 * @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;
}

Thanks for your help guys :)感谢您的帮助家伙:)

Please add " 'mapped' => false, " to form builder. 请在表单构建器中添加“ 'mapped'=> false ”。

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. 我不是那个弄错了的人。

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

A second solution is to add function setXxx to the property that gives an error in class Entity . 第二种解决方案是将函数setXxx添加到在类Entity中给出错误的属性。

public $xxx;

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

Or 要么

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

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

My English is bad, I can tell you. 我的英语很糟糕,我可以告诉你。

Setting mapped => false is not the real solution, because the field IS mapped to the entity. 设置mapped => false不是真正的解决方案,因为字段IS映射到实体。

In my case, I have a OneToMany relation, so I need the field mapped in order to use the 'cascase' => {"all"} option. 在我的例子中,我有一个OneToMany关系,所以我需要映射字段才能使用'cascase'=> {“all”}选项。

If the field is not mapped, then you must to persist the related entity manually. 如果未映射该字段,则必须手动保留相关实体。 That's no good. 那不好。

Stumbled on this while searching solution for my problem, that was shooting the same error and I was using also ArrayCollection class, so this may be helpful to someone: 在为我的问题搜索解决方案时偶然发现了这一点,那就是拍摄同样的错误,我也在使用ArrayCollection类,所以这可能对某人有帮助:

My field in ArrayCollection is called $files , so I had to add constructor like this: 我在ArrayCollection中的字段叫做$files ,所以我不得不像这样添加构造函数:

use Doctrine\Common\Collections\ArrayCollection;

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

Then I added add and remove methods, like this: 然后我添加了添加和删除方法,如下所示:

public function addFile(MyFile $file) : self
{
    $file->setParentMyItem($this); // Setting parent item 
    $this->files->add($file);
    return $this;
}

public function removeFile(MyFile $file) : self
{
    $this->files->removeElement($file);
    return $this;
}

But catch is that even my field name was $files I had to name add and remove methods addFile() and removeFile() , without 's' at end, which is totally not logical to me, but that solved the problem. 但问题是即使我的字段名称是$files我必须addremove方法addFile()removeFile() ,最后没有's',这对我来说完全不合逻辑,但这解决了问题。

Same error for me but on a OneToMany relation. 同样的错误,但在OneToMany关系上。 Although setting "mapped => false" also solved the error there is another option. 虽然设置“mapped => false”也解决了错误,但还有另一种选择。 I only had a getter, adder and remover method on the entity. 我只在实体上有一个getter,adder和remover方法。 I needed to add a "setter" too 我也需要添加一个“setter”

In my case it was: 在我的情况下它是:

/**
 * @param ArrayCollection|SubStatusOptions[]
 */
public function setSubStatusOptions(ArrayCollection $subStatusOptions)
{
    $this->subStatusOptions = $subStatusOptions;
}

This happened because I had an entity property but the getter/setters were missing.发生这种情况是因为我有一个实体属性,但缺少 getter/setter。 I used:我用了:

php bin/console make:entity --regenerate MyBundle\\\\Entity\\\\MyEntity

to regenerate them使它们再生

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony - 无法确定属性“id”的访问类型 - Symfony - Could not determine access type for property “id” 嵌入表单集合错误:无法确定属性的访问类型 - Embed a Collection of Forms Error: Could not determine access type for property 在类“ App \\ Entity \\ GameGenre”中无法确定属性“游戏”的访问类型: - Could not determine access type for property “games” in class “App\Entity\GameGenre”: Symfony 4 | ManyToMany关系-无法确定属性的访问类型 - Symfony 4 | ManyToMany Relation - Could not determine access type for property 错误:无法确定类“ Articles”中属性“ articlesCategories”的访问类型 - Error : Could not determine access type for property “articlesCategories” in class “Articles” 无法确定类“ AppBundle \\ Entity \\”中属性“ skills”的访问类型 - Could not determine access type for property “skills” in class “AppBundle\Entity\” 无法确定 class “App\Form\OrderType”中的属性“offerOrders”的访问类型:该属性都不是 - Could not determine access type for property “offerOrders” in class “App\Form\OrderType”: Neither the property 无法使用Sonata管理项目和Sonata翻译包确定Symfony 3.2。*中属性“翻译”的访问类型 - Could not determine access type for property “translations” in Symfony 3.2.* with Sonata admin project and Sonata translation bundle 无法确定属性“ id”的访问类型-HandleRequest失败,除非我将实体ID公开 - Could not determine access type for property “id” - HandleRequest failing unless I make the entity id public 对于某些,但不是全部,表单提交得到“无法确定属性的访问类型”id“。” - for some, but not all, form submission got “Could not determine access type for property ”id“.”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM