Symfony entity field : manyToMany with multiple = false - field not populated correctly
If you want to set the multiple
option to false
when adding to a ManyToMany
collection, you can use a "fake" property on the entity by creating a couple of new getters and setters, and updating your form-building code.
(Interestingly, I saw this problem on my project only after upgrading to Symfony 2.7, which is what forced me to devise this solution.)
Here's an example using your entities. The example assumes you want validation (as that's slightly complicated, so makes this answer hopefully more useful to others!)
Add the following to your Post
class:
public function setSingleCategory(PostCategory $category = null)
{
// When binding invalid data, this may be null
// But it'll be caught later by the constraint set up in the form builder
// So that's okay!
if (!$category) {
return;
}
$this->postCategories->add($category);
}
// Which one should it use for pre-filling the form's default data?
// That's defined by this getter. I think you probably just want the first?
public function getSingleCategory()
{
return $this->postCategories->first();
}
And now change this line in your form:
->add('postCategories', 'entity', array(
to be
->add('singleCategory', 'entity', array(
'constraints' => [
new NotNull(),
],
i.e. we've changed the field it references, and also added some inline validation - you can't set up validation via annotations as there is no property called singleCategory
on your class, only some methods using that phrase.
set multiple='false' in a form in a many to many relation symfony2
In EntityA, you have something like this, right?
public function setEntitiesB($data)
{
$this->entitiesB = $data ;
}
Now because you can also receive single value instead of array of values, you need something like this:
public function setEntitiesB($data)
{
if ( is_array($data) ) {
$this->entitiesB = $data ;
} else {
$this->entitiesB->clear() ;
$this->entitiesB->add($data) ;
}
}
Customize options in form select multiple [Symfony2]
You have to enable the expanded
option in order to display checkboxes:
expanded
type:
boolean
default:false
If set to true, radio buttons or checkboxes will be rendered
(depending on the multiple value). If false, a select element will be
rendered.
So you should use something like this:
->add('projNats', EntityType::class, array(
'class' => 'ITBundle:Nature',
'label' => 'nature',
'choice_label' => 'lib',
'multiple' => true,
'expanded' => true,
'required' => false))
Symfony2 - Collection of Forms - Update -ManytoMany-Relation
You should have in your user entity :
/**
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
* @ORM\JoinTable(name="role_assigned_to_user",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
* )
*/
private $roles;
In your role entity:
/**
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
*/
private $users;
in your role constructor:
public function __construct() {
$this->users = new \Doctrine\Common\Collections\ArrayCollection();
}
Instead of collection it will multi select:
->add('roles', 'entity', array(
'class' => 'YourBundle:Role',
'property' => 'name',
))
Laravel - Form Input - Multiple select for a one to many relationship
I agree with user3158900, and I only differ slightly in the way I use it:
{{Form::label('sports', 'Sports')}}
{{Form::select('sports',$aSports,null,array('multiple'=>'multiple','name'=>'sports[]'))}}
However, in my experience the 3rd parameter of the select is a string only, so for repopulating data for a multi-select I have had to do something like this:
<select multiple="multiple" name="sports[]" id="sports">
@foreach($aSports as $aKey => $aSport)
@foreach($aItem->sports as $aItemKey => $aItemSport)
<option value="{{$aKey}}" @if($aKey == $aItemKey)selected="selected"@endif>{{$aSport}}</option>
@endforeach
@endforeach
</select>
symfony2 form with a many to many entity
This is the solution working for me now with 'multiple' => false':
i've added this function in the other entity in relation with Manifestation, to consider manifestations as an array
public function setManifestations($manifestations){
if(!is_array($manifestations)){
$manifestations = array($manifestations);
}
$this->manifestations = $manifestations;
}
Related Topics
Alternative for $_Server['Http_Referer'] PHP Variable in Msie
Destination Path for Move_Uploaded_File in PHP
JSON_Decode() Returning Error "Notice: Trying to Get Property of Non-Object"
Replace the Last Comma in a String Using Regular Expression
How to Launch a PHP Script at Apache Startup
Best Debug Tool to Debug Ajax Request in PHP
How to Get Woocommerce Variation Id
PHP - How to Know If Server Allows Shell_Exec
Replace Multiple New Lines with a Single Newline
Codeigniter Activerecord: Join Backticking
Using PHP, How to Insert Text Without Overwriting to the Beginning of a Text File
From the String Name of a Class, How to Get a Static Variable
JavaScript String Length Differs from PHP Mb_Strlen
How to Detect If a User Uploaded a File Larger Than Post_Max_Size
How to Start Local Server with Symfony 5 or Downgrade Version to 4.4
How to Password Protect Streaming Videos with PHP
Symfony & Guard: "The Security Token Was Removed Due to an Accountstatusexception"