I try to put in place a system of categories . I love that my users can tag with categories . So I put a Array Collection and a " ManyToMany " but it does not work and whatever I do I get this error " Catchable Fatal Error : Object of class Shootngo \ CoreBundle \ Entity \ UserCategory couldn't be converted to string".
If someone could help me, i think i will not have hairs if i continue to search a solution...
$userCategories = new ArrayCollection();
foreach ($form->getCategories()->getLibCategory() as $category) {
$userCategories->add($category);
}
$user->addCategory($userCategories);
My FormType :
->add('category', CollectionType::class, array(
'entry_type' => UserCategoryType::class,
'allow_add' => true,
))
My View :
<div class="form-group">
<label class="col-sm-4 control-label">Catégories<span class="text-danger">*</span></label>
<div class="col-sm-6">
{{ form_errors(form.category) }}
<ul id="category-list" data-prototype="{{ form_widget(form.category.vars.prototype)|e }}">
{% for cat in form.category %}
{{ form_errors(cat) }}
<div class="form-group">
{{ form_widget(cat,{'attr' : {'class' : 'form-control', 'placeholder' : "", 'data-parsley-required' : 'data-parsley-required'}}) }}
</div>
{% endfor %}
</ul>
<a href="#" id="add-another-category">Add another category</a>
</div>
thank in advance ! Christophe
you need to add a method to
Shootngo\CoreBundle\Entity\UserCategory
entity class...
public function __toString()
{
return 'My string version of UserCategory'; // if you have a name property you can do $this->getName();
}
that way when the select options are being generated php automatically uses the __toString()
method to convert the Entity Object into text...