I want to link the doctrine orm entity and the odma doctrine document and use them in the sonataAdminBundle in the configureFormFields file of the admin class.
I installed the @Gedmo \ References doctrine extension, and it works great when I try to access related models from a controller.
But now I need to do this from SonataAdminBundle - to make a user interface for adding related documents to a user object.
Here is my essence:
<?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
{
protected $id;
private $phone2;
private $phone3;
private $photo;
private $addressBook;
private $dVal;
private $dValMan;
protected $addresses;
public function __construct()
{
parent::__construct();
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
$this->addresses = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getId()
{
return $this->id;
}
protected $groups;
public function setPhone($phone)
{
$this->phone = $phone;
$this->usernameCanonical = $phone;
$this->username = $phone;
}
public function setPhone2($phone2)
{
$this->phone2 = $phone2;
return $this;
}
public function getPhone2()
{
return $this->phone2;
}
public function setPhone3($phone3)
{
$this->phone3 = $phone3;
return $this;
}
public function getPhone3()
{
return $this->phone3;
}
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
public function getPhoto()
{
return $this->photo;
}
public function setAddressBook($addressBook)
{
$this->addressBook = $addressBook;
return $this;
}
public function getAddressBook()
{
return $this->addressBook;
}
public function setDVal($dVal)
{
$this->dVal = $dVal;
return $this;
}
public function getDVal()
{
return $this->dVal;
}
public function setDValMan($dValMan)
{
$this->dValMan = $dValMan;
return $this;
}
public function getDValMan()
{
return $this->dValMan;
}
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;
}
public static function getGenderList()
{
return array(
UserInterface::GENDER_UNKNOWN => '',
UserInterface::GENDER_MALE => '',
UserInterface::GENDER_FEMALE => '',
);
}
private $children;
private $parent;
public function addChild(\Application\Sonata\UserBundle\Entity\User $children)
{
$this->children[] = $children;
return $this;
}
public function removeChild(\Application\Sonata\UserBundle\Entity\User $children)
{
$this->children->removeElement($children);
}
public function getChildren()
{
return $this->children;
}
public function setParent(\Application\Sonata\UserBundle\Entity\User $parent = null)
{
$this->parent = $parent;
return $this;
}
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
{
protected $id;
protected $firstname;
protected $lastname;
protected $address;
protected $postcode;
protected $phone;
protected $comment;
protected $user;
protected $user_id;
protected $delivery_id;
protected $city_id;
public function getId()
{
return $this->id;
}
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
public function getFirstname()
{
return $this->firstname;
}
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
public function getLastname()
{
return $this->lastname;
}
public function setAddress($address)
{
$this->address = $address;
return $this;
}
public function getAddress()
{
return $this->address;
}
public function setPostcode($postcode)
{
$this->postcode = $postcode;
return $this;
}
public function getPostcode()
{
return $this->postcode;
}
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
public function getPhone()
{
return $this->phone;
}
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
public function getComment()
{
return $this->comment;
}
public function setUserId($userId)
{
$this->user_id = $userId;
return $this;
}
public function getUserId()
{
return $this->user_id;
}
public function setDeliveryId($deliveryId)
{
$this->delivery_id = $deliveryId;
return $this;
}
public function getDeliveryId()
{
return $this->delivery_id;
}
public function setCityId($cityId)
{
$this->city_id = $cityId;
return $this;
}
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();
}
he says: class does not exist
I also tried sonata_type_collection, but this is also an error:
->add('addresses', 'sonata_type_collection', array(
'type_options' => array('delete' => false)
), array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
))
The current field is addressesnot associated with an administrator. Create one for the target: ``
sonata_form_type , ?