I have a many to many relation between let's say entityA and entityB and I will use embeeded forms in order to add the attribute of entityA in the form of entityB as follows
$builder ->add('entityAs', 'entity', array(
'class' => 'xxxBundle:EntityA',
'property' => 'name',
'multiple' => false,
));}
When I set 'multiple' to true, everything is OK.
But when I set it to false, I get the following error
Property "entityAs" is not public in class "xxx\Entity\EntityB". Maybe you should create the method "setEntityAs()"?
As usual the property entityAs in your EntityB class is not public (protected or private). So you have to write (or generate) a setter for it: setEntityAs($entityAs)
multiple true may work, as I think (not sure) it uses the addXxx Setter. Proof me, if you have an addEntityAs Method in your EntityB Class?
Buuuuut, if you have a many to many relation, why you would set multiple to false?
Related
I'm using Sonata Admin in my project. I need to render a field that doesn't belong to entity.
Consider entity User with fields username & password. But I also need a extra field as hobby in form but it is not needed in User entity.
$formMapper
->add('username')
->add('password')
->add('hobby');
But I'm getting the symfony error as,
Neither the property "hobby" nor one of the methods "getHobby()", "hobby()", "isHobby()", "hasHobby()", "__get()" exist and have public access in class "App\Entity\User".
How can I solve this? Thanks in advance!!
This answer for Symfony2 should still hold true if I am not mistaken: How to add additional non-entity fields to entity form in Symfony2
In symfony 2.1+, use mapped:
$form = $this->createFormBuilder($promo)
->add('code', 'text')
->add('image', 'file', array(
"mapped" => false,
))
->getForm();
https://symfony.com/doc/current/reference/forms/types/entity.html#mapped
type: boolean default: true
If you wish the field to be ignored when reading or writing to the
object, you can set the mapped option to false.
So for your case it should be something like:
$formMapper
->add('username')
->add('password')
->add('hobby', null, [
'mapped' => false
]);
I am using Sonata Page Bundle to build a set of page with information about items -- one item per page. On each page, I want to include multiple synonyms for the item. I have created a Synonym entity with two fields.
I now want to use an add() method within my configureFormFields definition to add a reference to my new Synonym entity. The following code yields an error saying that The current field 'Synonym' is not linked to an admin. Please create one for the target entity : '':
->add('Synonym', 'sonata_type_collection', array(
'label' => "Synonyms",
'cascade_validation' => true,
'required' => false
), array(
'edit' => 'inline',
'inline' => 'table'
))
... and I have tried modifying the code so it looks like:
'cascade_validation' => true,
'required' => false,
'target_entity' => 'AppBundle\Entity\Synonym'
), array(
... which results in the same error.
How do I tell my admin class that this is a reference to a set of Synonym objects that I want to edit inline?
Edit:
I have also tried
'targetEntity' => 'AppBundle\Entity\Synonym'
... with no luck. The application is still seemingly having trouble figuring out what the entity I'm targeting is.
An at least partial answer was found in adding an "admin_code" definition to my array, as described at
https://symfony.com/doc/master/bundles/SonataAdminBundle/reference/form_types.html
I still get an error, but it is now at least a different error.
I am building a new feature onto a Symfony 2.8 application using a few Sonata bundles.
My Page.orm.xml file now contains the following:
<one-to-many target-entity="AppBundle\Entity\Synonym" field="equivalents" mapped-by="page">
<cascade>
<cascade-all/>
</cascade>
</one-to-many>
... while my Synonym.php entity definition contains the following:
/**
* #var \Application\Sonata\PageBundle\Entity\Page
* #ORM\ManyToOne(targetEntity="Application\Sonata\PageBundle\Entity\Page", inversedBy="equivalents", cascade={"persist"})
* #ORM\JoinTable(name="page_id")
*/
private $page;
... and my PageAdmin.php file contains the following:
->add('equivalents', 'sonata_type_collection', array(
'label' => "Equivalents",
'cascade_validation' => true,
'required' => false,
), array(
'edit' => 'inline',
'inline' => 'table',
'targetEntity' => 'AppBundle\Entity\Synonym',
'admin_code' => 'app.admin.synonym',
))
So far so good. I can load my Page entity's admin screen and add new Synonym entities inline.
The problem comes when I save the Page entity and then reload it. The Synonym objects save, but they are no longer associated with the Page in question, so the Page reverts to having a blank area where the Synonym object listing should be.
How do I make the Synonym objects remain associated with the Page object I am editing?
Extra information in case it's handy: Running doctrine:schema:validate does not output any errors.
Changing my relationships on both the owning and inverse sides to many-to-many "fixed" the problem for me, in that I can now see relationships between records being persisted.
(This also involved creating a couple basic getters and setters that I had neglected earlier.)
I'm currently developping a Symfony application (v2.8) with two main bundles : EasyAdmin and FOSUserBundle.
I have users with a particular role and I'm using a 'findBy' with an array to retrieve those users.
I would like to override the form of my admin controller to fill a select form choices with those users. I know it's possible to do it with an array but I want something dynamic if I add or delete some users.
I don't know how can I call a function from one of my controllers (or do I have to declare it as a service ?) to add choices to select form from a query.
Here is an example of what I would like :
$formBuilder->add('field_to_override', 'choice', array(
'choices' => **my_function**,
'multiple' => true,
'expanded' => true,
));
I hope I'm clear in my explanation.
Thank you in advance !
Consider using an EntityType field, which is a specialized form of Choice field which uses Doctrine entities for the choices.
It is possible to specify a custom query for the choices too, e.g.
$builder->add('users', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
));
I have a model called users and another called permissions. I just generated both with cake bake model all and I got this:
User Entity:
protected $_accessible = [
'email' => true,
'password' => true,
'permissions' => true, // Plural
];
Permission Entity:
protected $_accessible = [
'user_id' => true,
'controller' => true,
'user' => true, // Singular
];
Is this a Bake error or theres some kind of logic behind it?
The answer is explained in the CakePHP 3 docs, under Inflection Rules Updated
BelongsTo and HasOne associations will use the singular form in entity
properties, while HasMany and BelongsToMany (HABTM) will use plural
forms.
In other words, user is singular because it's related by a belongsTo / hasOne association. permissions is plural because it's related by a hasMany or HABTM association.
This can understandably catch folks off-guard if they're migrating from CakePHP 2, but it's actually pretty intuitive. Each permission has one user it's been granted to, but each user might have multiple permissions granted.