如何将图像添加到Sylius中的自定义实体?

时间:2013-12-11 11:40:10

标签: symfony sylius

我有一个必须能够管理图像文件的自定义实体(广告系列),因此我可以通过管理员上传一个。我已经尝试按照Taxon实体作为示例。问题是,当尝试通过管理表单创建新的广告系列时,它会返回以下错误:

  

捕获致命错误:传递给Sylius \ Bundle \ CoreBundle \ Entity \ Campaign :: setFile()的参数1必须是SplFileInfo的一个实例,给定字符串,在/ NetBeansProjects / tiendacoleman / vendor / symfony / symfony / src /中调用第350行的Symfony / Component / PropertyAccess / PropertyAccessor.php,在/NetBeansProjects/tiendacoleman/src/Sylius/Bundle/CoreBundle/Entity/Campaign.php第100行中定义

namespace Sylius\Bundle\CoreBundle\Entity;

use Sylius\Bundle\CoreBundle\Model\ImageInterface;
use SplFileInfo;
use DateTime;

class Campaign implements ImageInterface
{
     protected $id;
     protected $campaign;
     protected $link;
     protected $isActive;
     protected $campaignOrder;

    /**
     * @var SplFileInfo
     */
    protected $file;
    /**
     * @var string
     */
    protected $path;

    /**
     * @var \DateTime
     */
    protected $createdAt;

    /**
     * @var \DateTime
     */
    protected $updatedAt;

    public function __construct()
    {
        $this->createdAt = new DateTime();
    }

    public function getId()
    {
        return $this->id;
    }

    public function getCampaign()
    {
        return $this->campaign;
    }

    public function setCampaign($campaign)
    {
        $this->campaign = $campaign;
    }

    public function getLink()
    {
        return $this->link;
    }

    public function setLink($link)
    {
        $this->link = $link;
    }

    public function getIsActive()
    {
        return $this->isActive;
    }

    public function setIsActive($isActive)
    {
        $this->isActive = $isActive;
    }

    public function getCampaignOrder()
    {
        return $this->campaignOrder;
    }

    public function setCampaignOrder($campaignOrder)
    {
        $this->campaignOrder = $campaignOrder;
    }

    public function hasFile()
    {
        return null !== $this->file;
    }

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

    public function setFile(SplFileInfo $file)
    {
        $this->file = $file;
    }

    public function hasPath()
    {
        return null !== $this->path;
    }

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

    public function setPath($path)
    {
        $this->path = $path;
    }

    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    public function setCreatedAt(DateTime $createdAt)
    {
        $this->createdAt = $createdAt;
    }

    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(DateTime $updatedAt)
    {
        $this->updatedAt = $updatedAt;
    }
}

这是表格的代码:

namespace Sylius\Bundle\CoreBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class CampaignType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('campaign', 'text', array(
                'label' => 'sylius.form.campaign.name'
            ))
            ->add('link', 'text', array(
                'label' => 'sylius.form.campaign.link'
            ))
            ->add('isActive', 'checkbox', array(
                'label' => 'sylius.form.campaign.active'
            ))
            ->add('file','file',array(
                'label' => 'sylius.form.campaign.file'
            ))
        ;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'sylius_campaign';
    }
}

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:3)

我和你有同样的错误。我已按照这个答案修复了它: Symfony 2.2 upload files

您必须在树枝模板中的“表单标记”中添加“{{form_enctype(upload_form)}}”。将“upload_form”替换为表单变量。