SonataAdminBundle configureFormFields с двумя связанными объектами шага

У меня есть следующие объекты

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/Entity/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')

Когда я добавляю SmsHistory из UserAdmin, я хочу отображать только контакты, связанные с текущим пользователем, которого я редактирую, но отображаются все контакты всех пользователей.

Как я могу это сделать?

Благодарю вас!


person RL83    schedule 18.05.2017    source источник


Ответы (1)


Я получил решение, я надеюсь помочь кому-то.

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

И теперь объект фильтрует контакт, связанный с пользователем.

person RL83    schedule 18.05.2017