Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Could not determine access type for property "filters".
<?php
# AppBundle/Entity/Category.php ================================================================================================
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Category
*
* @ORM\Table(name="categories")
* @ORM\Entity(repositoryClass="AppBundle\Repository\CategoryRepository")
*/
class Category
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToMany(targetEntity="Filter")
* @ORM\JoinTable(name="category_filters")
*/
private $filters;
public function __construct()
{
$this->filters = new ArrayCollection();
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
public function addFilter(Filter $filter) {
if ($this->filters->contains($filter)) {
return;
}
$this->filters[] = $filter;
return $this;
}
public function removeFilter(Filter $filter) {
if (!$this->filters->contains($filter)) {
return;
}
$this->filters->removeElement($filter);
return $this;
}
public function getFilters()
{
return $this->filters;
}
}
# AppBundle/Entity/Filter.php ==================================================================================================
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Filter
*
* @ORM\Table(name="filters")
* @ORM\Entity(repositoryClass="AppBundle\Repository\FilterRepository")
*/
class Filter
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="value", type="string", length=255)
*/
private $value;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set value
*
* @param string $value
*
* @return Filter
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* @return string
*/
public function getValue()
{
return $this->value;
}
}
# AppBundle/Form/CategoryForm.php ===============================================================================================
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Doctrine\ORM\EntityRepository;
class CategoryType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('filters', EntityType::class, [
'class' => 'AppBundle:Filter',
'placeholder' => ' ',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('f');
},
'choice_label' => function($filters) {
return $filters->getValue();
},
'multiple' => false,
'expanded' => false,
// 'mapped' => false,
// 'by_reference' => false,
])
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Category'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_category';
}
}
# AppBundle/Form/FilterType.php =================================================================================================
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class FilterType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('value')
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Filter'
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_filter';
}
}
# AppBundle/Controller/DefaultController.php ====================================================================================
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Entity\Category;
use AppBundle\Form\CategoryType;
use AppBundle\Entity\Filter;
use AppBundle\Form\FilterType;
class DefaultController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function indexAction()
{
return $this->render('default/index.html.twig', []);
}
/**
* @Route("/category", name="category_index")
*/
public function categoryIndexAction()
{
$categories = $this->getDoctrine()->getRepository('AppBundle:Category')->findAll();
return $this->render('AppBundle:Category:index.html.twig', ['categories'=>$categories]);
}
/**
* @Route("/category/new", name="category_new")
* @Method({"GET", "POST"})
*/
public function categoryAction(Request $request)
{
$category = new Category();
$form = $this->createForm(CategoryType::class, $category, [
'action'=>$this->generateUrl('category_new'),
'method'=>'POST'
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($category);
$em->flush();
return $this->redirectToRoute('category_index');
}
return $this->render('AppBundle:Category:new.html.twig', ['form'=>$form->createView()]);
}
/**
* @Route("/filter", name="filter_index")
*/
public function filterIndexAction()
{
$filters = $this->getDoctrine()->getRepository('AppBundle:Filter')->findAll();
return $this->render('AppBundle:Filter:index.html.twig', ['filters'=>$filters]);
}
/**
* @Route("/filter/new", name="filter_new")
* @Method({"GET", "POST"})
*/
public function filterAction(Request $request)
{
$filter = new Filter();
$form = $this->createForm(FilterType::class, $filter, [
'action'=>$this->generateUrl('filter_new'),
'method'=>'POST'
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($filter);
$em->flush();
return $this->redirectToRoute('filter_index');
}
return $this->render('AppBundle:Filter:new.html.twig', ['form'=>$form->createView()]);
}
}
?>
<!--app/Resources/views/default/index.html.twig ==============================================================================-->
{% extends 'base.html.twig' %}
{% block body %}
<a href="{{ path('category_index') }}">Category</a> <br>
<a href="{{ path('filter_index') }}">Filter</a>
{% endblock %}
<!--AppBundle/Resources/views/Category/index.html.twig =======================================================================-->
{% extends '::base.html.twig' %}
{% block body %}
<ul>
{% for category in categories %}
<li>{{ category.name }}</li>
{% endfor %}
</ul>
<a href="{{ path('category_new') }}">New Category</a>
{% endblock %}
<!--AppBundle/Resources/views/Category/new.html.twig =========================================================================-->
{% extends '::base.html.twig' %}
{% block body %}
{{ form_start(form) }}
<div>{{ form_row(form.name) }}</div>
<div>{{ form_row(form.filters) }}</div>
<button type="submit">Save</button>
{{ form_end(form) }}
{% endblock %}
<!--AppBundle/Resources/views/Filter/index.html.twig =========================================================================-->
{% extends '::base.html.twig' %}
{% block body %}
<ul>
{% for filter in filters %}
<li>{{ filter.value }}</li>
{% endfor %}
</ul>
<a href="{{ path('filter_new') }}">New Filter</a>
{% endblock %}
<!-- AppBundle/Resources/views/Filter/new.html.twig ==========================================================================-->
{% extends '::base.html.twig' %}
{% block body %}
{{ form_start(form) }}
<div>{{ form_row(form.value) }}</div>
<button type="submit">Save</button>
{{ form_end(form) }}
{% endblock %}
<!--
When I insert a new filter, all things goes all right, but when I try to insert a new category, I get this error:
Error 500: Could not determine access type for property "filters".
Panic Announcement: Help please! :)
-->
@kermorgant

This comment has been minimized.

Copy link

commented Mar 3, 2017

Hello !

Did you find what the problem was ?

@Jir4

This comment has been minimized.

Copy link

commented Mar 13, 2017

Looks related to this : https://knpuniversity.com/screencast/doctrine-relations/create-genus-note ( in the comments section )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.