我想链接教义表单实体和教义odm文档,并在管理类的configureFormFields方法中的sonataAdminBundle中使用它们。
我已经建立了@Gedmo参考理论扩展,当我试图从控制器访问链接的模型时,它工作得很好。
但是现在我需要从SonataAdminBundle做它-制作一个UI来将链接的文档添加到用户实体。
这是我的实体:
<?php
namespace ApplicationSonataUserBundleEntity;
use DoctrineORMMapping as ORM;
use SonataUserBundleEntityBaseUser;
use FOSUserBundleModelGroupInterface;
use SonataUserBundleModelUserInterface;
use GedmoMappingAnnotation as Gedmo;
use ApplicationSonataUserBundleDocumentAddress;
class User extends BaseUser
{
/**
* @var integer
*/
protected $id;
/**
* @var string
*/
private $phone2;
/**
* @var string
*/
private $phone3;
/**
* @var string
*/
private $photo;
/**
* @var string
*/
private $addressBook;
/**
* @var integer
*/
private $dVal;
/**
* @var integer
*/
private $dValMan;
/**
* @var ArrayCollection
* @GedmoReferenceMany(type="document", class="ApplicationSonataUserBundleDocumentAddress", mappedBy="user")
*/
protected $addresses;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->groups = new DoctrineCommonCollectionsArrayCollection();
$this->addresses = new DoctrineCommonCollectionsArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @var DoctrineCommonCollectionsCollection
*/
protected $groups;
public function setPhone($phone)
{
$this->phone = $phone;
$this->usernameCanonical = $phone;
$this->username = $phone;
}
/**
* Set phone2
*
* @param string $phone2
* @return User
*/
public function setPhone2($phone2)
{
$this->phone2 = $phone2;
return $this;
}
/**
* Get phone2
*
* @return string
*/
public function getPhone2()
{
return $this->phone2;
}
/**
* Set phone3
*
* @param string $phone3
* @return User
*/
public function setPhone3($phone3)
{
$this->phone3 = $phone3;
return $this;
}
/**
* Get phone3
*
* @return string
*/
public function getPhone3()
{
return $this->phone3;
}
/**
* Set photo
*
* @param string $photo
* @return User
*/
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
/**
* Get photo
*
* @return string
*/
public function getPhoto()
{
return $this->photo;
}
/**
* Set addressBook
*
* @param string $addressBook
* @return User
*/
public function setAddressBook($addressBook)
{
$this->addressBook = $addressBook;
return $this;
}
/**
* Get addressBook
*
* @return string
*/
public function getAddressBook()
{
return $this->addressBook;
}
/**
* Set dVal
*
* @param integer $dVal
* @return User
*/
public function setDVal($dVal)
{
$this->dVal = $dVal;
return $this;
}
/**
* Get dVal
*
* @return integer
*/
public function getDVal()
{
return $this->dVal;
}
/**
* Set dValMan
*
* @param integer $dValMan
* @return User
*/
public function setDValMan($dValMan)
{
$this->dValMan = $dValMan;
return $this;
}
/**
* Get dValMan
*
* @return integer
*/
public function getDValMan()
{
return $this->dValMan;
}
/**
* Gets the groups granted to the user.
*
* @return Collection
*/
public function getGroups()
{
return $this->groups ?: $this->groups = new ArrayCollection();
}
public function getGroupNames()
{
$names = array();
foreach ($this->getGroups() as $group) {
$names[] = $group->getName();
}
return $names;
}
public function hasGroup($name)
{
return in_array($name, $this->getGroupNames());
}
public function addGroup(GroupInterface $group)
{
if (!$this->getGroups()->contains($group)) {
$this->getGroups()->add($group);
}
return $this;
}
public function removeGroup(GroupInterface $group)
{
if ($this->getGroups()->contains($group)) {
$this->getGroups()->removeElement($group);
}
return $this;
}
/**
* Returns the gender list
*
* @return array
*/
public static function getGenderList()
{
return array(
UserInterface::GENDER_UNKNOWN => 'Неизвестно',
UserInterface::GENDER_MALE => 'Мужской',
UserInterface::GENDER_FEMALE => 'Женский',
);
}
/**
* @var DoctrineCommonCollectionsCollection
*/
private $children;
/**
* @var ApplicationSonataUserBundleEntityUser
*/
private $parent;
/**
* Add children
*
* @param ApplicationSonataUserBundleEntityUser $children
* @return User
*/
public function addChild(ApplicationSonataUserBundleEntityUser $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* @param ApplicationSonataUserBundleEntityUser $children
*/
public function removeChild(ApplicationSonataUserBundleEntityUser $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* @return DoctrineCommonCollectionsCollection
*/
public function getChildren()
{
return $this->children;
}
/**
* Set parent
*
* @param ApplicationSonataUserBundleEntityUser $parent
* @return User
*/
public function setParent(ApplicationSonataUserBundleEntityUser $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return ApplicationSonataUserBundleEntityUser
*/
public function getParent()
{
return $this->parent;
}
public function setAddresses(DoctrineCommonCollectionsArrayCollection $addresses)
{
$this->addresses = $addresses;
}
public function getAddresses()
{
return $this->addresses;
}
}
和文档:
<?php
namespace ApplicationSonataUserBundleDocument;
use DoctrineCommonCollectionsCollection;
use GedmoMappingAnnotation as Gedmo;
use ApplicationSonataUserBundleEntityUser;
class Address
{
/**
* @var MongoId $id
*/
protected $id;
/**
* @var string $firstname
*/
protected $firstname;
/**
* @var string $lastname
*/
protected $lastname;
/**
* @var string $address
*/
protected $address;
/**
* @var string $postcode
*/
protected $postcode;
/**
* @var string $phone
*/
protected $phone;
/**
* @var string $comment
*/
protected $comment;
/**
* @GedmoReferenceOne(type="entity", class="ApplicationSonataUserBundleEntityUser", inversedBy="addresses", identifier="user_id", mappedBy="user_id")
*/
protected $user;
/**
* @var int $user_id
*/
protected $user_id;
/**
* @var int $delivery_id
*/
protected $delivery_id;
/**
* @var int $city_id
*/
protected $city_id;
/**
* Get id
*
* @return id $id
*/
public function getId()
{
return $this->id;
}
/**
* Set firstname
*
* @param string $firstname
* @return self
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string $firstname
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
* @return self
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string $lastname
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set address
*
* @param string $address
* @return self
*/
public function setAddress($address)
{
$this->address = $address;
return $this;
}
/**
* Get address
*
* @return string $address
*/
public function getAddress()
{
return $this->address;
}
/**
* Set postcode
*
* @param string $postcode
* @return self
*/
public function setPostcode($postcode)
{
$this->postcode = $postcode;
return $this;
}
/**
* Get postcode
*
* @return string $postcode
*/
public function getPostcode()
{
return $this->postcode;
}
/**
* Set phone
*
* @param string $phone
* @return self
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string $phone
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set comment
*
* @param string $comment
* @return self
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* @return string $comment
*/
public function getComment()
{
return $this->comment;
}
/**
* Set userId
*
* @param int $userId
* @return self
*/
public function setUserId($userId)
{
$this->user_id = $userId;
return $this;
}
/**
* Get userId
*
* @return int $userId
*/
public function getUserId()
{
return $this->user_id;
}
/**
* Set deliveryId
*
* @param int $deliveryId
* @return self
*/
public function setDeliveryId($deliveryId)
{
$this->delivery_id = $deliveryId;
return $this;
}
/**
* Get deliveryId
*
* @return int $deliveryId
*/
public function getDeliveryId()
{
return $this->delivery_id;
}
/**
* Set cityId
*
* @param int $cityId
* @return self
*/
public function setCityId($cityId)
{
$this->city_id = $cityId;
return $this;
}
/**
* Get cityId
*
* @return int $cityId
*/
public function getCityId()
{
return $this->city_id;
}
}
这是我的configureFormFields方法:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('addresses', 'sonata_type_model', array(
'required' => false,
'expanded' => true,
'multiple' => true
))
->end();
}
它说:类不存在
我也尝试了sonata_type_collection,但这也是一个错误:
->add('addresses', 'sonata_type_collection', array(
// Prevents the "Delete" option from being displayed
'type_options' => array('delete' => false)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
))
当前字段addresses
没有链接到管理员。请为目标实体创建一个:' '
我应该使用什么sonata_form_type来使一切工作?
我遇到了同样的问题@ArFeRR。我的解决方案是使用'entity'类型并为表单字段添加相关的类名:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
// ->add('branch', 'sonata_type_model', array( // Unknown Document namespace alias 'PlusquamContractBundle'.
->add('branch', 'entity', array(
'class' => 'PlusquamContractBundle:Branch',
'by_reference' => false
))
;
}
这将使"Class does not exist"错误消失