Разширението на доктрината Sonata и Gedmo\References не работи (Класът не съществува)

Искам да свържа обекта на doctrine orm и документа на doctrine odm и да ги използвам в sonataAdminBundle в метода configureFormFields на Admin Class.

Установих @Gedmo\References doctrine-extension и то работи добре, когато се опитвам да получа достъп до свързаните модели от контролера.

Но сега трябва да го направя от SonataAdminBundle - да направя потребителски интерфейс за добавяне на свързаните документи към потребителския обект.

Ето го моят обект:

<?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;
}
}

и документ:

<?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;
}
}

Ето моя метод 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 трябва да използвам, за да направя всичко да работи???


person ArFeRR    schedule 24.06.2014    source източник


Отговори (1)


Сблъсках се със същия проблем @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
            ))
        ;
    }

Това ще накара грешката „Класът не съществува“ да изчезне.

person webDEVILopers    schedule 10.09.2014