I am trying to display some images in my bundle (using easyadmin bundle) & symfony 3.4
However, for some reason, the photos aren't showing.
here's my code:
in config.yml:
Employee:
class: BackofficeBundle\Entity\Employee
role_prefix: ROLE_EMPLOYEE_MANAGER
list:
fields:
- id
- Name
- { property: 'photo', label: 'EMPLOYEE AVATAR', type: 'image' , base_path: '/images/' }
in my Employee entity:
/**
* #ORM\Column(name="photo", type="string", length=500)
* #Assert\File(maxSize="500k", mimeTypes={"image/jpeg", "image/jpg", "image/png", "image/GIF"})
*/
private $photo;
in my EmployeeType.php
$builder->add('photo', FileType::class, array('data_class'=>null, 'required'=>false
));
The photo aren't showing in the List page and when i click on the edit i get this error :
The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.
For this case, please consider using VichUploadBundle https://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html
Related
I am trying to implement a backend using easyadmin, I think its a great idea and I love the implementation... so far is going well, but I have a little problem, I want to show in the form a boolean field, for ex: 'published', my configuration looks like this:
Blog:
label: 'Posts list'
class: MyCompany\MyBundle\Entity\Post
list:
fields: ['title', 'published']
new:
fields:
- 'title'
- 'summary'
- 'body'
- { property: 'published', type: 'boolean' }
When I run this code, I get the error:
Could not load type "boolean"
This is how I define the field in the Entity:
/**
*
* #ORM\Column(name="published", type="boolean", length=1)
*/
protected $published = 1;
I know it's been a while since this question's posted, but for anyone still getting this error, in easyadmin documentation (https://github.com/javiereguiluz/EasyAdminBundle/blob/master/Resources/doc/book/4-edit-new-configuration.md) it is specified which types we need to use for each scenario (list, edit views).
In your question (edit scenario) so far the only workaround is to use "checkbox"as type in config.yml. Edit view only accepts Symfony Form Types (http://symfony.com/doc/current/reference/forms/types.html).
You don't have to specify type in configuration yml file. It will take automatically from entity.
I have set the languages level collection type:
The user is faced with many textfields where he can specify his level in some languages.
$builder->add('languages', 'collection', array(
'type' => 'text',
));
Then in the entity :
* #Assert\Collection(
* fields={
* "french" = #Assert\Required(),
* "english" = #Assert\Required()
* },
* allowExtraFields = true
* )
*/
private $languages;
The corresponding mutators:
public function getLanguages()
{
return $this->languages;
}
public function setLanguages($languages)
{
$this->languages= $languages;
return $this;
}
When instantiating the entity, the languages fields are initialized with empty strings in the constructor.
In the form page I get the error mentionned above.
You need to use data.languages.french not data.languages.[french].
check your view. I think you should try this :
{% if data.languages is not empty %} {{data.languages['frensh']}} {% endif %}
I resolved the porblem by upgrading Symfony from 2.5 to 2.6 version.
Now the annotation constraint #Assert\Collection works perfectly.
I have started using the Sonata Admin bundle and I was following the example on how to map an entity with the bundle to create an administrative interface.
I created an entity called Post and this is the configuration yml file:
Emiliano\PostsBundle\Entity\Post:
type: entity
table: null
repositoryClass: Emiliano\PostsBundle\Entity\PostRepository
id:
id:
type: integer
id: true
generator:
strategy: AUTO
fields:
title:
type: string
column: Title
lenght: 100
published:
type: boolean
column: Published
publishingDate:
type: datetime
column: Publishing_Date
nullable: TRUE
lifecycleCallbacks: { }
Then in my Admin class I have the configureFormFields method:
protected function configureFormFields(FormMapper $formMapper) {
$formMapper->add('title', 'text')
->add('published', 'checkbox', array('required' => false))
->add('publishingDate', 'sonata_type_model_hidden');
}
I found the sonata_type_model_hidden on the sonata admin documentation. What I would like to achieve is to programmatically handle the publishing date (e.g. set the date only if the checkbox published is checked) hiding the implementation to the user.
Everything works fine for create, delete and read, when it comes to modify an entity I get this message in the stacktrace:
No entity manager defined for class DateTime
In sonata-project/doctrine-orm-admin-bundle/Sonata/DoctrineORMAdminBundle/Model/ModelManager.php at line 214
If I show the field everything works fine, I tried also to use:
->add('publishingDate', 'hidden');
without success.
What is exactly the problem here? Is it because Sonata Admin tries to fill a form with the entity values and for publishingDate there's a DateTime while in the form specification I wrote a sonata_type_model_hidden? If so, how can I circumvent this?
sonata_type_model_hidden isn't just hidden field genereator, according to documentation:
sonata_type_model_hidden will use an instance of ModelHiddenType to render hidden field. The value of hidden field is identifier of related entity.
If I understand your problem, You want to set publishing date only when field published == true
You could use entity preSave/preUpdate lifecycle callback for eaxmple
public function preSave()
{
/**
* Check if item is published
*/
if($this->getPublished()) {
$this->setPublishingDate(new \DateTime());
} else {
$this->setPublishingDate(null);
}
}
and remove publishingDate field from SonataAdmin form.
I have a column in MySQL table defined as follows:
`fuel_type` set('gasoline','diesel','LPG','CNG','ethanol','bio-diesel','hydrogen') DEFAULT NULL,
I generated entities usingn doctrine's database introspection feature. The generated code in the entity in question is this:
/**
* #var simplearray
*
* #ORM\Column(name="fuel_type", type="simplearray", nullable=true)
*/
private $fuelType;
/**
* Set fuelType
*
* #param \simplearray $fuelType
* #return NomEngine
*/
public function setFuelType(\simplearray $fuelType)
{
$this->fuelType = $fuelType;
return $this;
}
/**
* Get fuelType
*
* #return \simplearray
*/
public function getFuelType()
{
return $this->fuelType;
}
In my sonata admin class the configureFormsFields method is thefined this way:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('name')
->add('fuel_type', 'choice', array(
'choices' => array(
'gasoline' => 'Gasoline',
'diesel' => 'Diesel',
'LPG' => 'LPG',
'CNG' => 'CNG',
'ethanol' => 'Ethanol',
'bio-diesel' => 'Bio Diesel',
'hydrogen' => 'Hydrogen'
),
'multiple' => true,
'required' => false
));
;
}
The problem is that after I try to save record in the database I get this exception:
Unknown column type "simplearray" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.
500 Internal Server Error - DBALException
I tried a couple of things to resolve this issue:
I noticed, that the generated type is 'simplearray', but in doctrine this type is 'simple_array'. I thought there was a typo.
Without success I tried to map simplearray to simple_array in config.yml :
doctrine:
dbal:
mapping_types:
simplearray: simple_array
After that I tried to change simplearray to simple_array in the entity. I got this error:
Catchable Fatal Error: Argument 1 passed to Acme\AdminBundle\Entity\Engine::setFuelType() must be an instance of simple_array, array given,
I thought that the admin class was passing array, and the entity was expecting simple_array, so I changed simple_array to array in the entity.
Now the error was this:
Could not convert database value "" to Doctrine Type array 500 Internal Server Error - ConversionException
Any insights about dealing with set columns in Sonata Admin will be greatly appreciated!
Your entity setter & getter are wrong too and should deals with a PHP array as Doctrine is converting it, I think you must change them to:
/**
* Set fuelType
*
* #param array $fuelType
*
* #return NomEngine
*/
public function setFuelType(array $fuelType)
{
$this->fuelType = $fuelType;
return $this;
}
/**
* Get fuelType
*
* #return array
*/
public function getFuelType()
{
return $this->fuelType;
}
It seems that Doctrine doesn't handle well the set syntax in MySQL. I see 2 ways you could solve your issue:
Change your MySQL schema to put in a json array or a varchar instead of your set. Probably the fastest way.
You might not have the luxury to change your schema. In that case, define a custom Doctrine type to suit your needs as described there: http://docs.doctrine-project.org/en/2.0.x/cookbook/mysql-enums.html#solution-2-defining-a-type ; you'll need then to register it to Symfony as explained there: http://symfony.com/doc/current/cookbook/doctrine/dbal.html#registering-custom-mapping-types
Hope it helps!
Form field declaration:
$builder->add('birthday', 'date', array('label' => 'Data urodzenia:', 'years' => range(date('Y'), date('Y')-100)), 'required' => false);
Corresponding document field declaration:
/**
* #ODM\Field(type="date")
*/
protected $birthday;
/**
* Set bitrhday
*
* #param \DateTime $birthday
*/
public function setBirthday(\DateTime $birthday)
{
$this->birthday = $birthday;
}
/**
* Get bitrhday
*
* #return \DateTime $birthday
*/
public function getBirthday()
{
return $this->birthday;
}
When I set data in form value is saved to database correctly, but widget isn`t populated with data form database field when I visit form page after saving.
Other fields in form works as expected.
It`s sf2 bug or my mistake?
First, birthday seems to be misspelled. Setbitrhday & Getbitrhday
Second, ODM does not support the DateTime field type. ODM only supports the field type Date. See ODM Field Type Reference
ORM, on the otherhand does support the DateTime field type. See ORM Field Type Reference