1

我想链接学说orm实体和学说odm文档,并在管理类的configureFormFields方法中的sonataAdminBundle中使用它们。

我已经建立了@Gedmo\References 学说扩展,当我尝试从控制器访问链接模型时它工作得很好。

但现在我需要从 SonataAdminBundle 执行此操作 - 制作一个 UI 以将链接的文档添加到用户实体。

这是我的实体:

<?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 来使一切正常工作???

4

1 回答 1

0

我遇到了同样的问题@ArFeRR。我的解决方案是使用“实体”类型并为表单字段添加相关的类名:

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

这将使“类不存在”错误消失。

于 2014-09-10T08:59:15.173 回答