I want to link the doctrine orm entity and doctrine odm document and use them in sonataAdminBundle in configureFormFields Method of the Admin Class.
I've established the @Gedmo\References doctrine-extension and it works just fine when i'm trying to access the linked models from controller.
But now I need to do it from SonataAdminBundle - to make an UI to add the linked documents to the user entity.
Here is my entity:
<?php
namespace Application\Sonata\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Sonata\UserBundle\Entity\BaseUser;
use FOS\UserBundle\Model\GroupInterface;
use Sonata\UserBundle\Model\UserInterface;
use Gedmo\Mapping\Annotation as Gedmo;
use Application\Sonata\UserBundle\Document\Address;
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
* @Gedmo\ReferenceMany(type="document", class="\Application\Sonata\UserBundle\Document\Address", mappedBy="user")
*/
protected $addresses;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
$this->addresses = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
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 \Doctrine\Common\Collections\Collection
*/
private $children;
/**
* @var \Application\Sonata\UserBundle\Entity\User
*/
private $parent;
/**
* Add children
*
* @param \Application\Sonata\UserBundle\Entity\User $children
* @return User
*/
public function addChild(\Application\Sonata\UserBundle\Entity\User $children)
{
$this->children[] = $children;
return $this;
}
/**
* Remove children
*
* @param \Application\Sonata\UserBundle\Entity\User $children
*/
public function removeChild(\Application\Sonata\UserBundle\Entity\User $children)
{
$this->children->removeElement($children);
}
/**
* Get children
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getChildren()
{
return $this->children;
}
/**
* Set parent
*
* @param \Application\Sonata\UserBundle\Entity\User $parent
* @return User
*/
public function setParent(\Application\Sonata\UserBundle\Entity\User $parent = null)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return \Application\Sonata\UserBundle\Entity\User
*/
public function getParent()
{
return $this->parent;
}
public function setAddresses(\Doctrine\Common\Collections\ArrayCollection $addresses)
{
$this->addresses = $addresses;
}
public function getAddresses()
{
return $this->addresses;
}
}
and document:
<?php
namespace Application\Sonata\UserBundle\Document;
use Doctrine\Common\Collections\Collection;
use Gedmo\Mapping\Annotation as Gedmo;
use Application\Sonata\UserBundle\Entity\User;
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;
/**
* @Gedmo\ReferenceOne(type="entity", class="\Application\Sonata\UserBundle\Entity\User", 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;
}
}
Here is my configureFormFields Method:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('General')
->add('addresses', 'sonata_type_model', array(
'required' => false,
'expanded' => true,
'multiple' => true
))
->end();
}
it says: Class does not exist
i tried the sonata_type_collection too, but it's an error too:
->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',
))
The current field addresses
is not linked to an admin. Please create one for the target entity : ``
What sonata_form_type should I use to make everything work???
I ran into the same issue @ArFeRR. My solution was to use the 'entity' type and add the related class name for the form field:
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
))
;
}
This will make the "Class does not exist" error disappear.
来源:https://stackoverflow.com/questions/24391982/sonata-and-gedmo-references-doctrine-extension-doesnt-work-class-does-not-exi