SonataAdminBundle configureFormFields with two step relation



我有下一个实体

AppBundle/Entity/User.php

namespace AppBundleEntity;
use SonataUserBundleEntityBaseUser as BaseUser;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
 * @ORMEntity
 * @ORMEntity(repositoryClass="AppBundleRepositoryUserRepository")
 * @ORMTable(name="fos_user_user")
 * 
 */
class User extends BaseUser
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * @ORMOneToMany(targetEntity="SmsHistory", mappedBy="user", cascade={"persist"}, orphanRemoval=true)
 */
private $smsHistory;
public function __construct()
{
    parent::__construct();
    $smsHistory = new ArrayCollection;
}
/**
 * Get id
 *
 * @return int $id
 */
public function getId()
{
    return $this->id;
}
/**
* @param DoctrineCommonCollectionsArrayCollection $smsHistory
*/
public function setSmsHistory($smsHistory){
    if (count($smsHistory) > 0) {
        foreach ($smsHistory as $i) {
            $this->addSmsHistory($i);
        }
    }
    return $this;
}
/**
 * Add smsHistory
 *
 * @param AppBundleEntitySmsHistory $smsHistory
 *
 * @return User
 */
public function addSmsHistory(AppBundleEntitySmsHistory $smsHistory)
{
    $smsHistory->setUser($this);
    $this->smsHistory->add($smsHistory);
}
/**
 * Remove smsHistory
 *
 * @param AppBundleEntitySmsHistory $smsHistory
 */
public function removeSmsHistory(AppBundleEntitySmsHistory $smsHistory)
{
    $this->smsHistory->removeElement($smsHistory);
}
/**
 * Get smsHistory
 *
 * @return DoctrineCommonCollectionsCollection
 */
public function getSmsHistory()
{
    return $this->smsHistory;
}

AppBundle/Entity/SMSSHistory.php

namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
/**
 * SmsHistory
 *
 * @ORMTable(name="sms_history")
 * @ORMEntity(repositoryClass="AppBundleRepositorySmsHistoryRepository")
 */
class SmsHistory
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @ORMManyToOne(targetEntity="User", inversedBy="smsHistory")
 * @ORMJoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;
/**
 * @ORMManyToOne(targetEntity="Contact", inversedBy="smsHistory")
 * @ORMJoinColumn(name="contact_id", referencedColumnName="id")
 */
private $contact;
/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}
/**
 * Set user
 *
 * @param AppBundleEntityUser $user
 *
 * @return SmsHistory
 */
public function setUser(AppBundleEntityUser $user = null)
{
    $this->user = $user;
    return $this;
}
/**
 * Get user
 *
 * @return AppBundleEntityUser
 */
public function getUser()
{
    return $this->user;
}
/**
 * Set contact
 *
 * @param AppBundleEntityContact $contact
 *
 * @return SmsHistory
 */
public function setContact(AppBundleEntityContact $contact = null)
{
    $this->contact = $contact;
    return $this;
}
/**
 * Get contact
 *
 * @return AppBundleEntityContact
 */
public function getContact()
{
    return $this->contact;
}

AppBundle/SMSS历史记录/联系方式.php

namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
 * Contact
 *
 * @ORMTable(name="contact")
 * @ORMEntity(repositoryClass="AppBundleRepositoryContactRepository")
 */
class Contact
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @ORMManyToOne(targetEntity="User", inversedBy="contact")
 * @ORMJoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;
/**
 * @ORMOneToMany(targetEntity="SmsHistory", mappedBy="contact", cascade={"persist"}, orphanRemoval=true)
 */
private $smsHistory;
public function __construct() {
    $smsHistory = new ArrayCollection;
}
/**
 * Get id
 *
 * @return int
 */
public function getId()
{
    return $this->id;
}
/**
 * Set user
 *
 * @param AppBundleEntityUser $user
 *
 * @return Contact
 */
public function setUser(AppBundleEntityUser $user = null)
{
    $this->user = $user;
    return $this;
}
/**
 * Get user
 *
 * @return AppBundleEntityUser
 */
public function getUser()
{
    return $this->user;
}
/**
 * Add smsHistory
 *
 * @param AppBundleEntitySmsHistory $smsHistory
 *
 * @return User
 */
public function addSmsHistory(AppBundleEntitySmsHistory $smsHistory)
{
    $smsHistory->setContact($this);
    $this->smsHistory->add($smsHistory);
}
/**
 * Remove smsHistory
 *
 * @param AppBundleEntitySmsHistory $smsHistory
 */
public function removeSmsHistory(AppBundleEntitySmsHistory $smsHistory)
{
    $this->smsHistory->removeElement($smsHistory);
}
/**
 * Get smsHistory
 *
 * @return DoctrineCommonCollectionsCollection
 */
public function getSmsHistory()
{
    return $this->smsHistory;
}

所有实体都与其他实体有关系。

在我的用户管理员中,我在配置表单字段中添加了用于添加联系人和添加短信历史记录的字段:

->add('contact', 'sonata_type_collection', array(
         'cascade_validation' => true,
         'by_reference' => true,
 ), array(
         'edit' => 'inline',
         'inline' => 'table',
 ))
->add('pushHistory', 'sonata_type_collection', array(
         'cascade_validation' => true,
         'by_reference' => true,
 ), array(
         'edit' => 'inline',
         'inline' => 'table',
 ))

在短信历史记录管理中,我添加了联系人字段,以选择联系人:

->add('contact','sonata_type_model')

当我从UserAdmin添加短信历史记录时,我只想显示与我正在编辑的当前用户有关系的联系人,但显示所有用户的所有联系人。

我该怎么做?

谢谢!

我得到了解决方案,希望对某人有所帮助。

在 SmsHistoryAdmin 中,更改此行:

->add('contact','sonata_type_model', array())

有了这个:

->add('contact', null, [
    'query_builder' => $this->getAllowedContactQueryBuilder(),
])

并添加以下函数:

/**
 * @return DoctrineCommonPersistenceObjectManager|object
 */
protected function getEntityManager()
{
    return $this->getContainer()->get('doctrine')->getManager();
}
/**
 * @return null|SymfonyComponentDependencyInjectionContainerInterface
 */
protected function getContainer()
{
    return $this->getConfigurationPool()->getContainer();
}
/**
 * @return mixed
 */
private function getAllowedContactQueryBuilder()
{
    if (!$this->getSubject()) {
        return null;
    }
    return $this->getContactRepository()
        ->getContactByUserQueryBuilder($this->getSubject()->getUser());
}
/**
 * @return DoctrineCommonPersistenceObjectRepository
 */
public function getContactRepository()
{
    return $this->getEntityManager()->getRepository('AppBundle:Contact');
}

现在,该实体正在过滤与用户的关系。

相关内容

  • 没有找到相关文章

最新更新