2017-05-18 3 views
0

私は次のエンティティ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 /エンティティ/ 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; 
} 

すべてのエンティティは他のエンティティと関連しています。私は、追加の接触のためにと、追加SmsHistoryためconfigureFormFieldsにフィールドを追加私のUSERADMINで

:SmsHistoryAdminで

->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', 
)) 

iは、連絡先を選択するために、連絡先のフィールドを追加しました:

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

I UserAdminからSmsHistoryを追加します。編集中の現在のユーザと関連している連絡先のみを表示したいのですが、すべてのユーザのすべての連絡先が表示されます。

どうすればいいですか?

ありがとうございました!

答えて

0

私は解決策を得ました、私は誰かを助けることを願っています。 SmsHistoryAdminで

、この行を変更:

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

そして今、実体がでrelationated連絡先をフィルタリングしている。これにより

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

をユーザー。

関連する問題