SonataAdminBundle configureFormFields包含两个步骤关联实体

时间:2017-05-18 08:07:14

标签: sonata-admin sonata symfony-2.8

我有下一个实体

的appbundle /实体/ user.php的

namespace AppBundle\Entity;

use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @ORM\Table(name="fos_user_user")
 * 
 */
class User extends BaseUser
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\OneToMany(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 \Doctrine\Common\Collections\ArrayCollection $smsHistory
*/
public function setSmsHistory($smsHistory){
    if (count($smsHistory) > 0) {
        foreach ($smsHistory as $i) {
            $this->addSmsHistory($i);
        }
    }
    return $this;
}

/**
 * Add smsHistory
 *
 * @param \AppBundle\Entity\SmsHistory $smsHistory
 *
 * @return User
 */
public function addSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
    $smsHistory->setUser($this);
    $this->smsHistory->add($smsHistory);
}

/**
 * Remove smsHistory
 *
 * @param \AppBundle\Entity\SmsHistory $smsHistory
 */
public function removeSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
    $this->smsHistory->removeElement($smsHistory);
}

/**
 * Get smsHistory
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getSmsHistory()
{
    return $this->smsHistory;
}

的appbundle /实体/ SmsHistory.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * SmsHistory
 *
 * @ORM\Table(name="sms_history")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\SmsHistoryRepository")
 */
class SmsHistory
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="smsHistory")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

/**
 * @ORM\ManyToOne(targetEntity="Contact", inversedBy="smsHistory")
 * @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
 */
private $contact;

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

/**
 * Set user
 *
 * @param \AppBundle\Entity\User $user
 *
 * @return SmsHistory
 */
public function setUser(\AppBundle\Entity\User $user = null)
{
    $this->user = $user;

    return $this;
}

/**
 * Get user
 *
 * @return \AppBundle\Entity\User
 */
public function getUser()
{
    return $this->user;
}

/**
 * Set contact
 *
 * @param \AppBundle\Entity\Contact $contact
 *
 * @return SmsHistory
 */
public function setContact(\AppBundle\Entity\Contact $contact = null)
{
    $this->contact = $contact;

    return $this;
}

/**
 * Get contact
 *
 * @return \AppBundle\Entity\Contact
 */
public function getContact()
{
    return $this->contact;
}

的appbundle / SmsHistory / Contact.php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Contact
 *
 * @ORM\Table(name="contact")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ContactRepository")
 */
class Contact
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="contact")
 * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
 */
private $user;

/**
 * @ORM\OneToMany(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 \AppBundle\Entity\User $user
 *
 * @return Contact
 */
public function setUser(\AppBundle\Entity\User $user = null)
{
    $this->user = $user;

    return $this;
}

/**
 * Get user
 *
 * @return \AppBundle\Entity\User
 */
public function getUser()
{
    return $this->user;
}

/**
 * Add smsHistory
 *
 * @param \AppBundle\Entity\SmsHistory $smsHistory
 *
 * @return User
 */
public function addSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
    $smsHistory->setContact($this);
    $this->smsHistory->add($smsHistory);
}

/**
 * Remove smsHistory
 *
 * @param \AppBundle\Entity\SmsHistory $smsHistory
 */
public function removeSmsHistory(\AppBundle\Entity\SmsHistory $smsHistory)
{
    $this->smsHistory->removeElement($smsHistory);
}

/**
 * Get smsHistory
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getSmsHistory()
{
    return $this->smsHistory;
}

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

在我的UserAdmin中,我在configureFormFields中添加了添加联系人的字段,并添加了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',
 ))

在SmsHistoryAdmin中,我添加了联系人字段,以选择联系人:

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

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

我怎么能这样做?

谢谢!

1 个答案:

答案 0 :(得分:0)

我得到了解决方案,希望能帮助别人。

在SmsHistoryAdmin中,更改以下行:

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

有了这个:

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

并添加此功能:

/**
 * @return \Doctrine\Common\Persistence\ObjectManager|object
 */
protected function getEntityManager()
{
    return $this->getContainer()->get('doctrine')->getManager();
}

/**
 * @return null|\Symfony\Component\DependencyInjection\ContainerInterface
 */
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 \Doctrine\Common\Persistence\ObjectRepository
 */
public function getContactRepository()
{
    return $this->getEntityManager()->getRepository('AppBundle:Contact');
}

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