奏鸣曲和Gedmo\Reference原则扩展不起作用(Class不存在) [英] Sonata and Gedmo\References doctrine extension doesn't work ( Class does not exist )

查看:182
本文介绍了奏鸣曲和Gedmo\Reference原则扩展不起作用(Class不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我已经建立了@Gedmo \Reference doctrine-extension,当我尝试从控制器访问链接的模型时,它的工作正常。



但现在我需要从SonataAdminBundle - 创建一个UI,将链接的文档添加到用户实体。



这是我的实体:

 <?php 

命名空间Application\Sonata\UserBundle\Entity;

使用Doctrine\ORM\Mapping作为ORM;
使用Sonata\UserBundle\Entity\BaseUser;
使用FOS\UserBundle\Model\GroupInterface;
使用Sonata\UserBundle\Model\UserInterface;
使用Gedmo\Mapping\Annotation作为Gedmo;
使用Application\Sonata\UserBundle\Document\Address;

class用户扩展BaseUser
{
/ **
* @var整数
* /
protected $ id;

/ **
* @var string
* /
private $ phone2;

/ **
* @var string
* /
private $ phone3;

/ **
* @var string
* /
private $ photo;

/ **
* @var string
* /
private $ addressBook;

/ **
* @var整数
* /
private $ dVal;

/ **
* @var整数
* /
private $ dValMan;

/ **
* @var ArrayCollection
* @ Gedmo\ReferenceMany(type =document,class =\Application\Sonata\UserBundle\ Document \Address,mappedBy =user)
* /
protected $ addresses;

/ **
*构造函数
* /
public function __construct()
{
parent :: __ construct();
$ this-> groups = new \Doctrine\Common\Collections\ArrayCollection();
$ this-> addresses = new \Doctrine\Common\Collections\ArrayCollection();
}



/ **
*获取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;
}


/ **
*设置phone2
*
* @param string $ phone2
* @return用户
* /
public function setPhone2($ phone2)
{
$ this-> phone2 = $ phone2;

return $ this;
}

/ **
*获取phone2
*
* @return string
* /
public function getPhone2 )
{
return $ this-> phone2;
}

/ **
*设置phone3
*
* @param string $ phone3
* @return用户
* /
public function setPhone3($ phone3)
{
$ this-> phone3 = $ phone3;

return $ this;
}

/ **
*获取phone3
*
* @return string
* /
public function getPhone3 )
{
return $ this-> phone3;
}

/ **
*设置照片
*
* @param string $ photo
* @return用户
* /
public function setPhoto($ photo)
{
$ this-> photo = $ photo;

return $ this;
}

/ **
*获取照片
*
* @return string
* /
public function getPhoto )
{
return $ this->照片;
}

/ **
*设置地址簿
*
* @param string $ addressBook
* @return用户
* /
public function setAddressBook($ addressBook)
{
$ this-> addressBook = $ addressBook;

return $ this;
}

/ **
*获取地址簿
*
* @return string
* /
public function getAddressBook )
{
return $ this-> addressBook;
}


/ **
*设置dVal
*
* @param整数$ dVal
* @return用户
* /
public function setDVal($ dVal)
{
$ this-> dVal = $ dVal;

return $ this;
}

/ **
*获取dVal
*
* @return整数
* /
public function getDVal )
{
return $ this-> dVal;
}

/ **
*设置dValMan
*
* @param integer $ dValMan
* @return用户
* /
public function setDValMan($ dValMan)
{
$ this-> dValMan = $ dValMan;

return $ this;
}

/ **
*获取dValMan
*
* @return integer
* /
public function getDValMan )
{
return $ this-> dValMan;
}

/ **
*获取授予用户的组。
*
* @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() - >包含($ group)) {
$ this-> getGroups() - > add($ group);
}

return $ this;
}

public function removeGroup(GroupInterface $ group)
{
if($ this-> getGroups() - > contains($ group)){
$ this-> getGroups() - > removeElement($ group);
}

return $ this;
}

/ **
*返回性别列表
*
* @return array
* /
public static函数getGenderList()
{
返回数组(
UserInterface :: GENDER_UNKNOWN =>'Неизвестно',
UserInterface :: GENDER_MALE =>'Мужской',
UserInterface :: GENDER_FEMALE =>'Женский',
);
}
/ **
* @var \Doctrine\Common\Collections\Collection
* /
private $ children;

/ **
* @var \Application\Sonata\UserBundle\Entity\User
* /
private $ parent;


/ **
*添加子项
*
* @param \Application\Sonata\UserBundle\Entity\User $孩子
* @return用户
* /
public function addChild(\Application\Sonata\UserBundle\Entity\User $ children)
{
$ this-> children [] = $ children;

return $ this;
}

/ **
*删除子项
*
* @param \Application\Sonata\UserBundle\Entity\User $ children
* /
公共功能removeChild(\Application\Sonata\UserBundle\Entity\User $ children)
{
$ this-> children- > removeElement($子女);
}

/ **
*获取孩子
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getChildren()
{
return $ this-> children;
}

/ **
*设置父
*
* @param \Application\Sonata\UserBundle\Entity\User $ parent
* @return用户
* /
public function setParent(\Application\Sonata\UserBundle\Entity\User $ parent = null)
{
$ this-> parent = $ parent;

return $ this;
}

/ **
*获取父
*
* @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 

命名空间Application\Sonata\UserBundle\Document;

使用Doctrine\Common\Collections\Collection;
使用Gedmo\Mapping\Annotation作为Gedmo;
使用Application\Sonata\UserBundle\Entity\User;

class地址
{
/ **
* @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 = address,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;


/ **
*获取id
*
* @return id $ id
* /
public function getId ()
{
return $ this-> id;
}

/ **
*设置名字
*
* @param string $ firstname
* @return self
* /
public function setFirstname($ firstname)
{
$ this-> firstname = $ firstname;
return $ this;
}

/ **
*获取名字
*
* @return string $ firstname
* /
public function getFirstname()
{
return $ this-> firstname;
}

/ **
*设置姓氏
*
* @param string $ lastname
* @return self
* /
public function setLastname($ lastname)
{
$ this-> lastname = $ lastname;
return $ this;
}

/ **
*获取姓氏
*
* @return string $ lastname
* /
public function getLastname()
{
return $ this-> lastname;
}

/ **
*设置地址
*
* @param string $ address
* @return self
* /
public function setAddress($ address)
{
$ this-> address = $ address;
return $ this;
}

/ **
*获取地址
*
* @return string $ address
* /
public function getAddress()
{
return $ this-> address;
}

/ **
*设置邮政编码
*
* @param string $ postcode
* @return self
* /
public function setPostcode($ postcode)
{
$ this-> postcode = $ postcode;
return $ this;
}

/ **
*获取邮政编码
*
* @return string $ postcode
* /
public function getPostcode()
{
return $ this-> postcode;
}

/ **
*设置手机
*
* @param string $ phone
* @return self
* /
public function setPhone($ phone)
{
$ this-> phone = $ phone;
return $ this;
}

/ **
*获取电话
*
* @return string $ phone
* /
public function getPhone()
{
return $ this-> phone;
}

/ **
*设置注释
*
* @param string $ comment
* @return self
* /
public function setComment($ comment)
{
$ this-> comment = $ comment;
return $ this;
}

/ **
*获取评论
*
* @return string $ comment
* /
public function getComment()
{
return $ this-> comment;
}

/ **
*设置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;
}

/ **
*设置deliveryId
*
* @param int $ deliveryId
* @return self
* /
public function setDeliveryId($ deliveryId)
{
$ this-> delivery_id = $ deliveryId;
return $ this;
}

/ **
*获取deliveryId
*
* @return int $ deliveryId
* /
public function getDeliveryId()
{
return $ this-> delivery_id;
}

/ **
*设置cityId
*
* @param int $ cityId
* @return self
* /
public function setCityId($ cityId)
{
$ this-> city_id = $ cityId;
return $ this;
}

/ **
*获取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,但也是一个错误:

   - >添加('addresses','sonata_type_collection',array(
//防止显示Delete选项
'type_options'=> array('delete'=> false)
),array(
'edit'=>'inline',
'inline'=>'table',
'sortable'=>'position',
))

当前字段地址不是链接到管理员。请为目标实体创建一个:``



我应该使用哪些sonata_form_type来使一切正常?

解决方案

我遇到了同样的问题@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
))
;
}

这将使类不存在错误消失。 / p>

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.

这篇关于奏鸣曲和Gedmo\Reference原则扩展不起作用(Class不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆