Last active
February 4, 2022 12:48
-
-
Save ornj/5200309 to your computer and use it in GitHub Desktop.
ManyToMany / ManyToOne implemented with SonataAdminBundle and SonataMediaBundle.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\ProjectBundle\Entity; | |
use Doctrine\Common\Collections\ArrayCollection; | |
use Doctrine\ORM\Mapping as ORM; | |
use Acme\Bundle\ProjectBundle\Entity\ProjectHasMedia; | |
/** | |
* Project | |
* | |
* @ORM\Table() | |
* @ORM\Entity(repositoryClass="Acme\Bundle\ProjectBundle\Entity\ProjectRepository") | |
*/ | |
class Project implements Taggable | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var ArrayCollection | |
* | |
* @ORM\OneToMany(targetEntity="Acme\Bundle\ProjectBundle\Entity\ProjectHasMedia", mappedBy="project", cascade={"all"}) | |
* @ORM\JoinTable(name="project_galleries") | |
*/ | |
private $projectMedia; | |
/** | |
* Constructor | |
*/ | |
public function __construct() | |
{ | |
$this->projectMedia = new ArrayCollection(); | |
} | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Add projectMedia | |
* | |
* @param Acme\Bundle\ProjectBundle\Entity\ProjectHasMedia | |
* @return Project | |
*/ | |
public function addProjectMedia(ProjectHasMedia $projectMedia) | |
{ | |
$this->projectMedia[] = $projectMedia; | |
return $this; | |
} | |
/** | |
* Remove projectMedia | |
* | |
* @param Acme\Bundle\ProjectBundle\Entity\ProjectHasMedia | |
*/ | |
public function removeProjectMedia(ProjectHasMedia $projectMedia) | |
{ | |
$this->projectMedia->remove($projectMedia); | |
} | |
/** | |
* Get projectMedia | |
* | |
* @return Acme\Bundle\ProjectBundle\Entity\ProjectHasMedia | |
*/ | |
public function getProjectMedia() | |
{ | |
return $this->projectMedia; | |
} | |
/** | |
* Set projectMedia | |
* | |
* @param array | |
* @return Project | |
*/ | |
public function setProjectMedia($media) | |
{ | |
$this->projectMedia = new ArrayCollection(); | |
foreach ($media as $m) { | |
$m->setProject($this); | |
$this->addProjectMedia($m); | |
} | |
return $this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\ProjectBundle\Admin; | |
use Sonata\AdminBundle\Admin\Admin; | |
use Sonata\AdminBundle\Datagrid\ListMapper; | |
use Sonata\AdminBundle\Datagrid\DatagridMapper; | |
use Sonata\AdminBundle\Form\FormMapper; | |
class ProjectAdmin extends Admin | |
{ | |
protected $context = 'default'; | |
/** | |
* Create form for creating and updating | |
* | |
* @param \Sonata\AdminBundle\Form\FormMapper $formMapper | |
*/ | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$formMapper | |
->add('projectMedia', 'sonata_type_collection', array( | |
'cascade_validation' => true, | |
), array( | |
'edit' => 'inline', | |
'inline' => 'table', | |
'sortable' => 'position', | |
'link_parameters' => array( | |
'context' => $this->context, | |
), | |
)) | |
; | |
} | |
/** | |
* Kinda Hackish methods to fix potential bug with SonataAdminBundle. I have not | |
* confirmed this is necessary but I've seen this implemented more than once. | |
*/ | |
public function prePersist($project) | |
{ | |
$project->setProjectMedia($project->getProjectMedia()); | |
} | |
public function preUpdate($project) | |
{ | |
$project->setProjectMedia($project->getProjectMedia()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\ProjectBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use Application\Sonata\MediaBundle\Entity\Gallery; | |
use Acme\Bundle\ProjectBundle\Entity\Project; | |
/** | |
* @ORM\Table() | |
* @ORM\Entity | |
*/ | |
class ProjectHasMedia { | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @ORM\ManytoOne(targetEntity="Acme\Bundle\ProjectBundle\Entity\Project", inversedBy="projectMedia", cascade={"all"}) | |
*/ | |
protected $project; | |
/** | |
* @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Gallery") | |
* @ORM\JoinTable(name="project_gallery") | |
*/ | |
protected $gallery; | |
public function __construct() | |
{ | |
} | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set project | |
* | |
* @param \Acme\Bundle\ProjectBundle\Entity\Project $project | |
* @return ProjectHasMedia | |
*/ | |
public function setProject(Project $project = null) | |
{ | |
$this->project = $project; | |
return $this; | |
} | |
/** | |
* Get project | |
* | |
* @return \Acme\Bundle\ProjectBundle\Entity\Project | |
*/ | |
public function getProject() | |
{ | |
return $this->project; | |
} | |
/** | |
* Get gallery | |
* | |
* @return Gallery | |
*/ | |
public function getGallery() | |
{ | |
return $this->gallery; | |
} | |
/** | |
* Set gallery | |
* | |
* @param Gallery | |
* @return ProjectHasMedia | |
*/ | |
public function setGallery(Gallery $gallery) | |
{ | |
$this->gallery = $gallery; | |
return $this; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Bundle\ProjectBundle\Admin; | |
use Sonata\AdminBundle\Admin\Admin; | |
use Sonata\AdminBundle\Datagrid\ListMapper; | |
use Sonata\AdminBundle\Datagrid\DatagridMapper; | |
use Sonata\AdminBundle\Form\FormMapper; | |
class ProjectHasMediaAdmin extends Admin | |
{ | |
protected $context = 'default'; | |
protected function configureFormFields(FormMapper $formMapper) | |
{ | |
$formMapper | |
->add('gallery', 'sonata_type_model_list', array( | |
'help' => 'Each individual gallery will be displayed as a carousel.', | |
'required' => false, | |
), array( | |
'admin_code' => 'sonata.media.admin.gallery', | |
)) | |
; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment