多媒体,图库和GalleryMultimedia。
多媒体和画廊都与GalleryMultimedia具有一对多的关系,该画廊拥有多媒体ID,画廊ID和位置。
在MultimediaAdmin中,我添加了画廊列表,如下所示:
->with('Thematic Galleries')
->add('gallery', 'entity', array(
'class' => 'ACME\MyBundle\Entity\Gallery',
'property' => 'name',
'multiple' => true,
'expanded' => true,
'mapped' => false,))
->end()
现在,我坚持将选定的Gallery保留为GalleryMultimedia对象。在我的多媒体模型中,我具有下面的函数,我希望将其传递给GalleryMultimedia对象以进行持久化,但无法弄清楚如何实现。
public function setGalleries($galleries)
{
if (count($galleries) > 0) {
foreach ($galleries as $gallery)
{
$this->addGallery($gallery);
}
}
return $this;
}
出于绝望,我在MultimediaAdmin.php中添加了以下代码
public function prePersist($multimedia)
{
$this->preUpdate($multimedia);
}
public function preUpdate($multimedia)
{
$multimedia->setFiles($multimedia->getFiles());
$this->saveGalleries($multimedia);
}
public function saveGalleries($multimedia)
{
$galleryies = $this->getForm()->get('gallery')->getData();
$container = $this->getConfigurationPool()->getContainer();
$em = $container->get('doctrine')->getManager();
$existing_arr = array();
$existing = $em->getRepository('ACMEMyBundle:GalleryMultimedia')->findBy(array('multimedia' => $multimedia));
$gals = array();
foreach($existing as $exist)
{
$existing_arr[] = $exist->getGallery()->getId();
}
foreach($galleryies as $gallery)
{
if(in_array($gallery->getId(),$existing_arr))
{
continue;
}
else
{
$gm = new \ACME\MyBundle\Entity\GalleryMultimedia();
$gm->setGallery($gallery);
$gals[] = $gm;
$multimedia->setGalleries($gals);
}
}
}
有人可以帮助我可怜的灵魂吗?
最佳答案
我通过像这样将postPersist持久化来解决了这个问题
public function postPersist($multimedia)
{
$this->postUpdate($multimedia);
}
public function postUpdate($multimedia)
{
$this->saveGalleries($multimedia);
}
希望这会帮助某人