>我有一个问题。我安装了奏鸣曲分类包。
但是当我想创建一个新帖子时,我有一个错误:
属性 "collection" 和 "getCollection()"、"collection()"、"isCollection()"、"hasCollection()"、"__get()" 在类 "DN\SiteBundle\Entity\Post" 中都不存在且具有公共访问权限。
这是我在PostAdmin.php
(src/DN/SiteBundle/Admin/PostAdmin.php
)中的代码
namespace DNSiteBundleAdmin;
use SonataAdminBundleAdminAdmin;
use SonataAdminBundleFormFormMapper;
use SonataAdminBundleDatagridDatagridMapper;
use SonataAdminBundleDatagridListMapper;
use SonataAdminBundleShowShowMapper;
use SonataCoreBundleValidatorErrorElement;
use KnpMenuItemInterface 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 DNSiteBundleEntity;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use DoctrineExtensionsTaggableTaggable;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORMEntity
*/
class Post implements Taggable
{
/**
* @var integer
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORMColumn(name="author", type="string", length=255)
*/
private $author;
/**
* @var string
*
* @ORMColumn(name="content", type="text")
*/
private $content;
/**
* @var DateTime
*
* @ORMColumn(name="created_at", type="datetime")
*/
private $created_at;
/**
* @var boolean
*
* @ORMColumn(name="publication", type="boolean")
*/
private $publication;
/**
* @var string
* @GedmoSlug(fields={"title"})
* @ORMColumn(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();
}
}
我猜你的字段名称应该是"标签"而不是"集合"。
我在实体中看不到属性"集合"。
add 方法的第一个参数应该是实体中定义的属性的名称。
namespace DNSiteBundleAdmin;
use SonataAdminBundleAdminAdmin;
use SonataAdminBundleFormFormMapper;
use SonataAdminBundleDatagridDatagridMapper;
use SonataAdminBundleDatagridListMapper;
use SonataAdminBundleShowShowMapper;
use SonataCoreBundleValidatorErrorElement;
use KnpMenuItemInterface 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