问题描述
我第一次使用Sonata Admin,我在线上阅读了文档,并且我的管理员工作正常,除了尝试通过管理员创建元素时出现错误:
Failed to create object: App\Entity\HomeBlockElement
我有我的HomeBlockElement
<?PHP
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\HomeBlockElementRepository")
* @ORM\Table(name="home_block_element")
*/
class HomeBlockElement
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(name="home_element_id",type="integer")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="home_element_title",type="string",length=255)
*/
protected $title;
/**
* @var string
*
* @ORM\Column(name="home_element_text_link",length=255)
*/
protected $textLink;
/**
* @var string
*
* @ORM\Column(name="home_element_shopping_link",length=255)
*/
protected $shoppingLink;
/**
* @var string
*
* @ORM\Column(name="home_element_marketing_etiquette",length=255)
*/
protected $marketingEtiquette;
/**
* @var string
*
* @ORM\Column(name="home_element_media",type="text")
*/
protected $media;
/**
* @var bool
*
* @ORM\Column(name="home_element_published",type="boolean")
*/
protected $published = false;
public function getId(): ?int
{
return $this->id;
}
/**
* @return string
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string $title
* @return HomeBlockElement
*/
public function setTitle(?string $title): HomeBlockElement
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getTextLink(): ?string
{
return $this->textLink;
}
/**
* @param string $textLink
* @return HomeBlockElement
*/
public function setTextLink(?string $textLink): HomeBlockElement
{
$this->textLink = $textLink;
return $this;
}
/**
* @return string
*/
public function getShoppingLink(): ?string
{
return $this->shoppingLink;
}
/**
* @param string $shoppingLink
* @return HomeBlockElement
*/
public function setShoppingLink(?string $shoppingLink): HomeBlockElement
{
$this->shoppingLink = $shoppingLink;
return $this;
}
/**
* @return string
*/
public function getMarketingEtiquette(): ?string
{
return $this->marketingEtiquette;
}
/**
* @param string $categoryLink
* @return HomeBlockElement
*/
public function setMarketingEtiquette(?string $marketingEtiquette): HomeBlockElement
{
$this->marketingEtiquette = $marketingEtiquette;
return $this;
}
/**
* @return bool
*/
public function isPublished(): bool
{
return $this->published;
}
/**
* @param bool $published
* @return Page
*/
public function setPublished(bool $published): HomeBlockElement
{
$this->published = $published;
return $this;
}
}
还有我的HomeBlockElementAdmin:
<?PHP
namespace App\Admin;
use App\Entity\Page;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Symfony\Component\Form\Extension\Core\Type\CheckBoxType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
final class HomeBlockElementAdmin extends AbstractAdmin
{
protected $datagridValues = array(
'_sort_order' => 'ASC','_sort_by' => 'title',);
/**
* @param $object
* @return string|null
*/
public function toString($object): ?string
{
return $object instanceof Page && $object->getTitle()
? $object->getTitle()
: 'Nouveau bloc élément';
}
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Contenu')
->add('published',CheckBoxType::class,['required' => false,'label' => 'Publier'])
->add('title',TextType::class,['required' => true,'label' => 'Titre'])
->add('marketingEtiquette','label' => 'Etiquette Marketing'])
->add('textLink','label' => 'Texte'])
->add('shoppinglink','label' => 'Lien'])
->end();
}
protected function configureListFields(ListMapper $listMapper)
{
unset($this->listModes['mosaic']);
$listMapper
->addIdentifier('title')
->addIdentifier('marketingEtiquette')
->addIdentifier('textLink')
->addIdentifier('shoppinglink')
->addIdentifier('published')
;
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('title')
->add('marketingEtiquette')
->add('textLink')
->add('shoppinglink')
->add('published');
}
/**
* @param RouteCollection $collection
*/
protected function configureRoutes(RouteCollection $collection)
{
$collection
->remove('delete')
//->remove('create')
->add('move',$this->getRouterIdParameter() . '/move/{position}');
}
}
然后我在我的services.yaml中定义了这个
admin.element:
class: App\Admin\HomeBlockElementAdmin
arguments: [~,App\Entity\HomeBlockElement,~]
tags:
- { name: sonata.admin,manager_type: orm,label: 'Element blocs',group: 'app.admin.group.home' }
我已经使用PHP bin / console doctrine:schema:update更新了数据库,使其与我的代码匹配,并且一切正常,从Internet上看到的错误与sql有关,但是 我看不到错误的来源。 在错误的痕迹中,我可以看到:
PDOException > PDOException > NotNullConstraintViolationException > ModelManagerException
所以错误是因为某些东西在不应该为null时会为null?但是唯一需要的是id,它应该自动生成...我不知道该怎么办
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)