2
當我嘗試在數據庫中創建新實體時,出現錯誤「無法使用類型爲Symfony \ Component \ Form \ FormRenderer的對象作爲數組」我的奏鳴曲管理包與Symfony。我之前沒有這個問題,並且我沒有更改管理文件中的任何內容。錯誤500 SonataAdmin:無法使用Symfony Component Form FormRenderer類型的對象作爲數組
環境
Php 7.0, Mysql
索納塔包
$ composer show --latest 'sonata-project/*'
sonata-project/admin-bundle 3.x-dev 51387ad dev-master cd61e55
sonata-project/block-bundle 3.x-dev 033f4e1 dev-master 64a897b
sonata-project/cache 1.x-dev adb8f3e 1.x-dev adb8f3e
sonata-project/core-bundle 3.x-dev 16c7611 dev-master 3ac1797
sonata-project/datagrid-bundle 2.x-dev 66dcb36 2.x-dev 66dcb36
sonata-project/doctrine-extensions 1.x-dev b07f636 dev-master b969484
sonata-project/doctrine-orm-admin-bundle 3.x-dev b06d010 dev-master 66219fe
sonata-project/easy-extends-bundle 2.x-dev 60edcd3 2.x-dev 60edcd3
sonata-project/exporter 1.x-dev 2aa197e 1.x-dev 2aa197e
sonata-project/media-bundle dev-master 504b500 dev-master 504b500
sonata-project/notification-bundle 3.x-dev e27bbc2 dev-master 84e6c0b
Symfony的包
$ composer show --latest 'symfony/*'
symfony/monolog-bundle dev-master c9fb33a dev-master c9fb33a
symfony/phpunit-bridge 3.4.x-dev 5ddca7a dev-master 9c5da0f
symfony/polyfill-apcu dev-master cec3239 dev-master cec3239
symfony/polyfill-intl-icu dev-master 4aa0b65 dev-master 4aa0b65
symfony/polyfill-mbstring dev-master 7c8fae0 dev-master 7c8fae0
symfony/polyfill-php56 dev-master e85ebde dev-master e85ebde
symfony/polyfill-php70 dev-master b6482e6 dev-master b6482e6
symfony/polyfill-util dev-master 67925d1 dev-master 67925d1
symfony/security-acl dev-master ab4dfe2 dev-master ab4dfe2
symfony/swiftmailer-bundle 2.6.x-dev 11555c3 dev-master d31de92
symfony/symfony 3.4.x-dev 30e3b6d 3.4.x-dev 30e3b6d
PHP版本
$ php -v
PHP 7.0.23-1+020170902060934.6+stretch1.gbpa985da (cli) (built: Sep 2 2017 06:09:36) (NTS)
這裏是我的管理文件看起來像:
use Doctrine\ORM\EntityRepository;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
class TeamAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Infos')
->add('name', 'text');
$formMapper->add('manager', 'sonata_type_model_list', array(
'class' => 'UserBundle:User',
'required' => true,
'by_reference' => false,
'required' => false
), array(
'edit' => 'inline',
'inline' => 'table',
))
->end()
->with('General')
->add('members', 'sonata_type_collection', array(
'required'=> false
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position'
))
->add('comexAdmins',
null,
array(
'label' => 'Admin Comex',
'expanded' => true,
'by_reference' => false,
'multiple' => true,
'query_builder' => function(EntityRepository $er)
{
return $er->createQueryBuilder('u')
->where('u.level = 3');
}
))
// ->add('title')
// ->add('abstract')
// ->add('content')
->end();
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('name');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('name');
$listMapper->add('manager');
$listMapper->add('comexAdmins');
$listMapper->add('_action', null, array(
'actions' => array(
'show' => array(),
'edit' => array()
)
));
}
public function prePersist($team)
{
$this->preUpdate($team);
}
public function preUpdate($team)
{
$team->getManager();
}
}
這是球隊實體
class Team
{
/**
* @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, unique=true)
*/
private $name;
/**
* One Team has One Manager.
* @ORM\OneToOne(targetEntity="User", inversedBy ="managerOf", cascade={"persist"})
* @ORM\JoinColumn(name="manager_id", nullable =true)
*/
private $manager;
/**
* @ORM\ManyToMany(targetEntity="UserBundle\Entity\User", inversedBy="comexTeams", cascade={"persist"})
*/
private $comexAdmins;
/**
* One Team has many members.
* @ORM\ManyToMany(targetEntity="User", mappedBy ="teams", cascade={"persist"})
*/
private $members;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Team
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
public function __toString()
{
return $this->name;
}
/**
* Set manager
*
* @param \UserBundle\Entity\User $manager
*
* @return Team
*/
public function setManager(\UserBundle\Entity\User $manager = null)
{
$this->manager = $manager;
return $this;
}
/**
* Get manager
*
* @return \UserBundle\Entity\User
*/
public function getManager()
{
return $this->manager;
}
/**
* Constructor
*/
public function __construct()
{
$this->members = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add member
*
* @param \UserBundle\Entity\Team $member
*
* @return Team
*/
public function addMember(\UserBundle\Entity\User $member)
{
$this->members[] = $member;
return $this;
}
/**
* Remove member
*
* @param \UserBundle\Entity\Team $member
*/
public function removeMember(\UserBundle\Entity\User $member)
{
$this->members->removeElement($member);
}
/**
* Get members
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getMembers()
{
return $this->members;
}
/**
* Add comexAdmin
*
* @param \UserBundle\Entity\User $comexAdmin
*
* @return Team
*/
public function addComexAdmin(\UserBundle\Entity\User $comexAdmin)
{
$this->comexAdmins[] = $comexAdmin;
$comexAdmin->addComexTeam($this);
return $this;
}
/**
* Remove comexAdmin
*
* @param \UserBundle\Entity\User $comexAdmin
*/
public function removeComexAdmin(\UserBundle\Entity\User $comexAdmin)
{
$this->comexAdmins->removeElement($comexAdmin);
}
/**
* Get comexAdmins
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getComexAdmins()
{
return $this->comexAdmins;
}
/**
* Returns Members ids
* @return array
*/
public function getMembersIds()
{
$ids = array();
foreach($this->getMembers() as $member)
{
$ids[] = $member->getId();
}
return $ids;
}
}
Ca你分享團隊實體嗎?它看起來像你的一個字段是一個數組,你試圖以非數組字段類型進行渲染。逐個評論你的每個領域,你會發現哪一個是失敗的。 – albert
¿您能在錯誤日誌或錯誤頁面中檢測錯誤行嗎? – Cuchu
@albert感謝您的幫助,我更新了與團隊實體的問題 – Makhtar