Reputation: 67
I've a custom entity (campaign) that must be able to manage an image file, so I can upload one via the administrator.I've done it trying to follow Taxon entity as an example. The problem is that when trying to create a new campaign through the administration form it returns the following error:
Catchable Fatal Error: Argument 1 passed to Sylius\Bundle\CoreBundle\Entity\Campaign::setFile() must be an instance of SplFileInfo, string given, called in /NetBeansProjects/tiendacoleman/vendor/symfony/symfony/src/Symfony/Component/PropertyAccess/PropertyAccessor.php on line 350 and defined in /NetBeansProjects/tiendacoleman/src/Sylius/Bundle/CoreBundle/Entity/Campaign.php line 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;
}
}
This is the code of the form:
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';
}
}
Thank you very much for your help!
Upvotes: 2
Views: 2434
Reputation: 86
I had the same error as you. I have fixed it following this answer: Symfony 2.2 upload files
You have to add "{{ form_enctype(upload_form) }}" to your form tag in the twig template. Replace 'upload_form' with your form variable.
Upvotes: 3