簡體   English   中英

無法確定屬性“文件”的訪問類型

[英]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 ”。

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

那些說解散是錯誤的人,請看那里的測試。 我不是那個弄錯了的人。

測試代碼: https//github.com/symfony/property-access/blob/master/Tests/PropertyAccessorCollectionTest.php#L151

第二種解決方案是將函數setXxx添加到在類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

我的英語很糟糕,我可以告訴你。

設置mapped => false不是真正的解決方案,因為字段IS映射到實體。

在我的例子中,我有一個OneToMany關系,所以我需要映射字段才能使用'cascase'=> {“all”}選項。

如果未映射該字段,則必須手動保留相關實體。 那不好。

在為我的問題搜索解決方案時偶然發現了這一點,那就是拍攝同樣的錯誤,我也在使用ArrayCollection類,所以這可能對某人有幫助:

我在ArrayCollection中的字段叫做$files ,所以我不得不像這樣添加構造函數:

use Doctrine\Common\Collections\ArrayCollection;

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

然后我添加了添加和刪除方法,如下所示:

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;
}

但問題是即使我的字段名稱是$files我必須addremove方法addFile()removeFile() ,最后沒有's',這對我來說完全不合邏輯,但這解決了問題。

同樣的錯誤,但在OneToMany關系上。 雖然設置“mapped => false”也解決了錯誤,但還有另一種選擇。 我只在實體上有一個getter,adder和remover方法。 我也需要添加一個“setter”

在我的情況下它是:

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

發生這種情況是因為我有一個實體屬性,但缺少 getter/setter。 我用了:

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

使它們再生

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM