Error sonata classificiation bundle

151 views Asked by At

I have a problem. I installed sonata classification bundle.

But when I want to create a new post I have an error:

Neither the property "collection" nor one of the methods "getCollection()", "collection()", "isCollection()", "hasCollection()", "__get()" exist and have public access in class "DN\SiteBundle\Entity\Post".*

This is my code in PostAdmin.php (src/DN/SiteBundle/Admin/PostAdmin.php)

namespace DN\SiteBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\CoreBundle\Validator\ErrorElement;
use Knp\Menu\ItemInterface as MenuItemInterface;

class PostAdmin extends Admin
{
 // Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('title', 'text', array('label' => 'Post Title'))
        ->add('content')
 ->add('publication')
->add('author')
    ->add('collection', 'sonata_type_model_list', array('required' =>  false));

}
//...
}
?>

This is my code in Post.php (src/DN/SiteBundle/Entity/Post.php)

 namespace DN\SiteBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Gedmo\Mapping\Annotation as Gedmo;
    use DoctrineExtensions\Taggable\Taggable;
    use Doctrine\Common\Collections\ArrayCollection;


    /**
     * @ORM\Entity
     */
    class Post implements Taggable
    {
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="author", type="string", length=255)
     */
    private $author;

    /**
     * @var string
     *
     * @ORM\Column(name="content", type="text")
     */
    private $content;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $created_at;

    /**
     * @var boolean
     *
     * @ORM\Column(name="publication", type="boolean")
     */
    private $publication;

    /**
     * @var string   
     * @Gedmo\Slug(fields={"title"})
     * @ORM\Column(name="slug", type="string", length=255)
     */
     private $slug;

      private $tags;

      public function getTags()
    {
        $this->tags = $this->tags ?: new ArrayCollection();

        return $this->tags;
    }

    public function getTaggableType()
    {
        return 'DN_tag';
    }

    public function getTaggableId()
    {
        return $this->getId();
    }




    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set title
     *
     * @param string $title
     * @return Post
     */
    public function setTitle($title)
    {
        $this->title = $title;

        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set author
     *
     * @param string $author
     * @return Post
     */
    public function setAuthor($author)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return string 
     */
    public function getAuthor()
    {
        return $this->author;
    }

    /**
     * Set content
     *
     * @param string $content
     * @return Post
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return string 
     */
    public function getContent()
    {
        return $this->content;
    }



    /**
     * Set publication
     *
     * @param boolean $publication
     * @return Post
     */
    public function setPublication($publication)
    {
        $this->publication = $publication;

        return $this;
    }

    /**
     * Get publication
     *
     * @return boolean 
     */
    public function getPublication()
    {
        return $this->publication;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Post
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * Get slug
     *
     * @return string 
     */
    public function getSlug()
    {
        return $this->slug;
    }




    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return Post
     */
    public function setCreatedAt($createdAt)
    {
        $this->created_at = $createdAt;

        return $this;
    }

    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

            public function __construct()
    {
        $this->created_at = new \DateTime("now");
    }

    public function __toString()
    {
        return $this->getTitle();
    }
    }
1

There are 1 answers

0
HypeR On

The name of your field should be "tags" instead of "collection" I guess.

I don't see a property "collection" in your Entity.

The first parameter of the add method should be the name of your property defined in your Entity.

namespace DN\SiteBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\CoreBundle\Validator\ErrorElement;
use Knp\Menu\ItemInterface as MenuItemInterface;

class PostAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title', 'text', array('label' => 'Post Title'))
            ->add('content')
            ->add('publication')
            ->add('author')
            ->add('collection', 'sonata_type_model_list', array('required' =>  false));
    }
//...
}
?>

Documentation on forms : https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html