0

我有个问题。我安装了奏鸣曲分类包。

但是当我想创建一个新帖子时,我遇到了一个错误:

属性“collection”和方法之一“getCollection()”、“collection()”、“isCollection()”、“hasCollection()”、“__get()”都不存在并且在类“DN\”中具有公共访问权限SiteBundle\Entity\Post".*

这是我在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));

}
//...
}
?>

这是我在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();
    }
    }
4

1 回答 1

0

我猜你的字段名称应该是“tags”而不是“collection”。

我在您的实体中没有看到属性“集合”。

add 方法的第一个参数应该是在实体中定义的属性的名称。

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));
    }
//...
}
?>

表格文档:https ://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html

于 2015-06-18T13:35:10.577 回答