I'm working my way through Symfony trying to learn how it all fits together and I'm working on the admin section.
Right now I'm putting together an admin form for a Show Entity which will reference a section entity (so this show belongs in that section, etc). Every other field in the form saves EXCEPT for the related entity choice field.
This is the ShowAdmin class
<?php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Nelmio\ApiDocBundle\Tests\Fixtures\Form\EntityType;
class ShowAdmin extends AbstractAdmin {
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('title', 'text')
->add('shortname', 'text')
->add('section_id', EntityType::class, array(
'class' => 'AppBundle:SectionEntity',
'choice_label' => 'section_title',
))
->add('logo', 'text')
->add('description', 'textarea')
->add('status', 'integer');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('title');
$datagridMapper->add('shortname');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('title');
$listMapper->add('shortname', 'text');
}
}
This is the ShowEntity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="shows")
*/
class ShowEntity {
function __construct() {
$this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
*/
private $title;
/**
* #ORM\Column(type="string", length=100)
*/
private $shortname;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\SectionEntity")
*/
private $section;
/**
* #ORM\Column(type="string", length=255)
*/
private $logo;
/**
* #ORM\Column(type="text")
*/
private $description;
/**
* #ORM\Column(type="integer")
*/
private $status;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return ShowEntity
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set sectionId
*
* #param integer $sectionId
*
* #return ShowEntity
*/
public function setSectionId($sectionId)
{
$this->section_id = $sectionId;
return $this;
}
/**
* Get sectionId
*
* #return integer
*/
public function getSectionId()
{
return $this->section_id;
}
/**
* Set logo
*
* #param string $logo
*
* #return ShowEntity
*/
public function setLogo($logo)
{
$this->logo = $logo;
return $this;
}
/**
* Get logo
*
* #return string
*/
public function getLogo()
{
return $this->logo;
}
/**
* Set description
*
* #param string $description
*
* #return ShowEntity
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set status
*
* #param integer $status
*
* #return ShowEntity
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* Get status
*
* #return integer
*/
public function getStatus()
{
return $this->status;
}
/**
* Set shortname
*
* #param string $shortname
*
* #return ShowEntity
*/
public function setShortname($shortname)
{
$this->shortname = $shortname;
return $this;
}
/**
* Get shortname
*
* #return string
*/
public function getShortname()
{
return $this->shortname;
}
}
And this is the SectionEntity
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SectionEntity
*
* #ORM\Table(name="section_entity")
* #ORM\Entity(repositoryClass="AppBundle\Repository\SectionEntityRepository")
*/
class SectionEntity
{
protected $section_id;
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="text")
*/
private $section_title;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set sectionTitle
*
* #param string $sectionTitle
*
* #return SectionEntity
*/
public function setSectionTitle($sectionTitle)
{
$this->section_title = $sectionTitle;
return $this;
}
/**
* Get sectionTitle
*
* #return string
*/
public function getSectionTitle()
{
return $this->section_title;
}
/**
* Get string
*/
public function __toString() {
return $this->section_title;
}
function __construct() {
$this->section_id = new \Doctrine\Common\Collections\ArrayCollection();
}
}
Any help would be greatly appreciated, I know it's probably something super simple that I'm just not seeing.
Thanks.
(optional) Rename ShowEntity::$section into ShowEntity::$sections to highlight sections is a collection but not a single entity.
Set ShowEntity __construct method body to:
$this->sections = new \Doctrine\Common\Collections\ArrayCollection();
At ShowAdmin::configureFormFields rename
->add('section_id', EntityType::class, array(
into
->add('section', EntityType::class, array(
You should use direct reference to the relation instead of id.
Remove SectionEntity::__construct method, it has no sense.
Remove protected $section_id; from SectionEntity.
Change public function setSectionId($sectionId) into public function setSection(Section $section).
Perhaps you also need to rename section_title into sectionTitle or simply title, not sure about that.
Related
I have two entities as follows:
<?php
// src/coreBundle/Entity/model.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\brand;
/**
*#ORM\Entity
*#ORM\Table(name="model")
*/
class model
{
/**
* #ORM\ManyToOne(targetEntity="coreBundle\Entity\brand", inversedBy="models")
* #ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
private $brands;
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*#ORM\Column(type="integer")
*/
public $brand_id;
/**
*#ORM\Column(type="string", length=100)
*/
private $name;
/**
*#ORM\Column(type="string", length=100)
*/
private $image_url;
/**
*#ORM\Column(type="string", length=200)
*/
private $comment;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set brandId
*
* #param integer $brandId
*
* #return model
*/
public function setBrandId($brandId)
{
$this->brand_id = $brandId;
return $this;
}
/**
* Get brandId
*
* #return integer
*/
public function getBrandId()
{
return $this->brand_id;
}
/**
* Set name
*
* #param string $name
*
* #return model
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set imageUrl
*
* #param string $imageUrl
*
* #return model
*/
public function setImageUrl($imageUrl)
{
$this->image_url = $imageUrl;
return $this;
}
/**
* Get imageUrl
*
* #return string
*/
public function getImageUrl()
{
return $this->image_url;
}
/**
* Set comment
*
* #param string $comment
*
* #return model
*/
public function setComment($comment)
{
$this->comment = $comment;
return $this;
}
/**
* Get comment
*
* #return string
*/
public function getComment()
{
return $this->comment;
}
/**
* Set brands
*
* #param \coreBundle\Entity\brand $brands
*
* #return model
*/
public function setBrands(\coreBundle\Entity\brand $brands = null)
{
$this->brands = $brands;
return $this;
}
/**
* Get brands
*
* #return \coreBundle\Entity\brand
*/
public function getBrands()
{
return $this->brands;
}
}
And Second one is as follows:
<?php
// src/coreBundle/Entity/brand.php
namespace coreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use coreBundle\Entity\model;
use Doctrine\Common\Collections\ArrayCollection;
/**
*#ORM\Entity
*#ORM\Table(name="brand")
*/
class brand
{
/**
* ORM\OneToMany(targetEntity="coreBundle\Entity\model", mappedBy="brands")
*/
private $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*#ORM\Column(type="string", length=100)
*/
private $name;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return brand
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
}
"model" has a ManyToOne relationship with "brand"
I am having issues of schema validation,
*The association coreBundle\Entity\model#brands refers to the inverse side field coreBundle\Entity\brand#models which does not exist
Can you tell what am I doing wrong, Thanks in advance.
In case your still wondering after 3 hours of agony, your missing the # in #ORM\OneToMany (brand.php).
I am trying to create a little CRUD system with 2 entities and a OneToMany relationship between these 2 entities.
My entities are Location & Job. I created the entities and created the crud controller via doctrine for both.
Location CRUD works great but Job only Read & Delete works. When I try to create a new Job it throws the following exception:
Catchable Fatal Error: Object of class DataBundle\Entity\Location
could not be converted to string
I have no idea what it means or what is causing it.
If anyone could help me out that would be realy awesome!
Many thanks in advance!
Here is my code.
Entities:
Location
<?php
namespace DataBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Location
*
* #ORM\Table(name="location")
* #ORM\Entity(repositoryClass="DataBundle\Repository\LocationRepository")
*/
class Location
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="street", type="string", length=255)
*/
private $street;
/**
* #var string
*
* #ORM\Column(name="number", type="string", length=25)
*/
private $number;
/**
* #var string
*
* #ORM\Column(name="postalcode", type="string", length=4)
*/
private $postalcode;
/**
* #var string
*
* #ORM\Column(name="city", type="string", length=255)
*/
private $city;
/**
* #var Job[] Available jobs for this location.
*
* #ORM\OneToMany(targetEntity="Job", mappedBy="location")
*/
private $jobs;
public function __construct()
{
$this->jobs = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Location
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set street
*
* #param string $street
*
* #return Location
*/
public function setStreet($street)
{
$this->street = $street;
return $this;
}
/**
* Get street
*
* #return string
*/
public function getStreet()
{
return $this->street;
}
/**
* Set number
*
* #param string $number
*
* #return Location
*/
public function setNumber($number)
{
$this->number = $number;
return $this;
}
/**
* Get number
*
* #return string
*/
public function getNumber()
{
return $this->number;
}
/**
* Set postalcode
*
* #param string $postalcode
*
* #return Location
*/
public function setPostalcode($postalcode)
{
$this->postalcode = $postalcode;
return $this;
}
/**
* Get postalcode
*
* #return string
*/
public function getPostalcode()
{
return $this->postalcode;
}
/**
* Set city
*
* #param string $city
*
* #return Location
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Get city
*
* #return string
*/
public function getCity()
{
return $this->city;
}
/**
* Add job
*
* #param \DataBundle\Entity\Job $job
*
* #return Location
*/
public function addJob(\DataBundle\Entity\Job $job)
{
$this->jobs[] = $job;
return $this;
}
/**
* Remove job
*
* #param \DataBundle\Entity\Job $job
*/
public function removeJob(\DataBundle\Entity\Job $job)
{
$this->jobs->removeElement($job);
}
/**
* Get jobs
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getJobs()
{
return $this->jobs;
}
}
Job:
<?php
namespace DataBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Job
*
* #ORM\Table(name="job")
* #ORM\Entity(repositoryClass="DataBundle\Repository\JobRepository")
*/
class Job
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* #var Location
*
* #ORM\ManyToOne(targetEntity="Location", inversedBy="jobs")
* #ORM\JoinColumn(name="location_id", referencedColumnName="id")
*/
private $location;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
*
* #return Job
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set description
*
* #param string $description
*
* #return Job
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set location
*
* #param \DataBundle\Entity\Location $location
*
* #return Job
*/
public function setLocation(\DataBundle\Entity\Location $location = null)
{
$this->location = $location;
return $this;
}
/**
* Get location
*
* #return \DataBundle\Entity\Location
*/
public function getLocation()
{
return $this->location;
}
}
My new action in the JobController:
/**
* Creates a new job entity.
*
*/
public function newAction(Request $request)
{
$job = new Job();
$form = $this->createForm('DataBundle\Form\JobType', $job);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($job);
$em->flush($job);
return $this->redirectToRoute('job_show', array('id' => $job->getId()));
}
return $this->render('job/new.html.twig', array(
'job' => $job,
'form' => $form->createView(),
));
}
DataBundle/Form/JobTypep
<?php
namespace DataBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class JobType extends AbstractType
{
/**
* {#inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title')->add('description')->add('location') ;
}
/**
* {#inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'DataBundle\Entity\Job'
));
}
/**
* {#inheritdoc}
*/
public function getBlockPrefix()
{
return 'databundle_job';
}
}
You can't use location as text field in your form, location is object.
You can fix this error by using form data transformers that will convert location into text on form output and text in location object on form submit.
http://symfony.com/doc/current/form/data_transformers.html
Or by using form type EnityType for location:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title')->add('description');
$builder
->add('location', Symfony\Bridge\Doctrine\Form\Type\EntityType::class, [
'required' => false,
'expanded' => false,
'multiple' => false,
'class' => 'DataBundle\\Entity\\Location',
'empty_data' => '',
'choice_label' => 'name',
'label' => 'Location',
]);
}
http://symfony.com/doc/current/reference/forms/types/entity.html
I have a bit of a problem figuring out the following:
How can I make Symfony insert a new menu when a new coffeeshop has been made with a form? (foreign key in menu is shopid)
Thanks in advance, code below.
Menu Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Menu
*
* #ORM\Table(name="menu")
* #ORM\Entity(repositoryClass="AppBundle\Repository\MenuRepository")
*
*/
class Menu
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToOne(targetEntity="Coffeeshop")
* #ORM\JoinColumn(name="coffeeshop_id", referencedColumnName="id")
*/
private $shopId;
/**
* #var \DateTime $updated
*
* #Gedmo\Timestampable(on="update")
* #ORM\Column(type="datetime")
*/
private $updated;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set shopId
*
* #param integer $shopId
*
* #return Menu
*/
public function setShopId($shopId)
{
$this->shopId = $shopId;
return $this;
}
/**
* Get shopId
*
* #return int
*/
public function getShopId()
{
return $this->shopId;
}
/**
* Set lastUpdated
*
* #param \DateTime $lastUpdated
*
* #return Menu
*/
public function setLastUpdated($lastUpdated)
{
$this->lastUpdated = $lastUpdated;
return $this;
}
/**
* Get lastUpdated
*
* #return \DateTime
*/
public function getLastUpdated()
{
return $this->lastUpdated;
}
/**
* Set updated
*
* #param \DateTime $updated
*
* #return Menu
*/
public function setUpdated($updated)
{
$this->updated = $updated;
return $this;
}
/**
* Get updated
*
* #return \DateTime
*/
public function getUpdated()
{
return $this->updated;
}
}
Coffeeshop Entity:
<?php
/// src/AppBundle/Entity/Product.php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="coffeeshop")
*/
class Coffeeshop
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=100)
*/
private $name;
/**
* #ORM\Column(type="string")
*/
private $phone;
/**
* #ORM\Column(type="string", length=50)
*/
private $streetName;
/**
* #ORM\Column(type="string", length=6)
*/
private $houseNumber;
/**
* #ORM\Column(type="string", length=7)
*/
private $zipcode;
/**
* #ORM\Column(type="text")
*/
private $description;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
*
* #return Coffeeshop
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set phone
*
* #param string $phone
*
* #return Coffeeshop
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* #return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set streetName
*
* #param string $streetName
*
* #return Coffeeshop
*/
public function setStreetName($streetName)
{
$this->streetName = $streetName;
return $this;
}
/**
* Get streetName
*
* #return string
*/
public function getStreetName()
{
return $this->streetName;
}
/**
* Set houseNumber
*
* #param string $houseNumber
*
* #return Coffeeshop
*/
public function setHouseNumber($houseNumber)
{
$this->houseNumber = $houseNumber;
return $this;
}
/**
* Get houseNumber
*
* #return string
*/
public function getHouseNumber()
{
return $this->houseNumber;
}
/**
* Set description
*
* #param string $description
*
* #return Coffeeshop
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set zipcode
*
* #param string $zipcode
*
* #return Coffeeshop
*/
public function setZipcode($zipcode)
{
$this->zipcode = $zipcode;
return $this;
}
/**
* Get zipcode
*
* #return string
*/
public function getZipcode()
{
return $this->zipcode;
}
/**
* Set menu
*
* #param \AppBundle\Entity\Menu $menu
*
* #return Coffeeshop
*/
public function setMenu(\AppBundle\Entity\Menu $menu = null)
{
$this->menu = $menu;
return $this;
}
/**
* Get menu
*
* #return \AppBundle\Entity\Menu
*/
public function getMenu()
{
return $this->menu;
}
/**
* Set coffeeshopmenu
*
* #param \AppBundle\Entity\Menu $coffeeshopmenu
*
* #return Coffeeshop
*/
public function setCoffeeshopmenu(\AppBundle\Entity\Menu $coffeeshopmenu = null)
{
$this->coffeeshopmenu = $coffeeshopmenu;
return $this;
}
/**
* Get coffeeshopmenu
*
* #return \AppBundle\Entity\Menu
*/
public function getCoffeeshopmenu()
{
return $this->coffeeshopmenu;
}
}
Coffeeshop FormBuilder:
<?php
/**
* Created by PhpStorm.
* User:
* Date: 23-9-2016
* Time: 14:20
*/
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
class CoffeeshopType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('phone')
->add('streetName')
->add('houseNumber')
->add('zipcode')
->add('description')
->add('save', SubmitType::class, array('label' => 'Add shop'))
;
}
}
In many ways:
you can define Coffeeshoptypeas a service, then inject ManagerRegistry (#doctrine) to __construct() (or just EntityManager), set an event listener for event FormEvents::POST_SUBMIT. Something like that:
$this->addEventListener(FormEvents::POST_SUBMIT, function(FormEvent $event) {/*...*/});
in controller, where you persist changes from Coffeeshoptype
use an event listener for Doctrine (or create an entity listener, feature from Doctrine). With doctrine events, you can find if entity (Coffeeshop) is persisting or updating and depends of situation, create new menu.
All of above methods can have access to Doctrine (thanks to Dependency Injection), also some of these methods are bad approaches. I suggest to attach EventListener (or EventSubscriber) to one of Doctrine Events and then do persisting for new menu. But if you need to create a new menu only when Coffeeshop is submitted by form, create event listener in form type.
I have a problem. I installed sonata classification bundle.
But when I want to create a new post I have an error:
Neither the property "collection" nor one of the methods "getCollection()", "collection()", "isCollection()", "hasCollection()", "__get()" exist and have public access in class "DN\SiteBundle\Entity\Post".*
This is my code in PostAdmin.php (src/DN/SiteBundle/Admin/PostAdmin.php)
namespace DN\SiteBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\CoreBundle\Validator\ErrorElement;
use Knp\Menu\ItemInterface as MenuItemInterface;
class PostAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array('label' => 'Post Title'))
->add('content')
->add('publication')
->add('author')
->add('collection', 'sonata_type_model_list', array('required' => false));
}
//...
}
?>
This is my code in Post.php (src/DN/SiteBundle/Entity/Post.php)
namespace DN\SiteBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use DoctrineExtensions\Taggable\Taggable;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity
*/
class Post implements Taggable
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* #var boolean
*
* #ORM\Column(name="publication", type="boolean")
*/
private $publication;
/**
* #var string
* #Gedmo\Slug(fields={"title"})
* #ORM\Column(name="slug", type="string", length=255)
*/
private $slug;
private $tags;
public function getTags()
{
$this->tags = $this->tags ?: new ArrayCollection();
return $this->tags;
}
public function getTaggableType()
{
return 'DN_tag';
}
public function getTaggableId()
{
return $this->getId();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Post
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set author
*
* #param string $author
* #return Post
*/
public function setAuthor($author)
{
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor()
{
return $this->author;
}
/**
* Set content
*
* #param string $content
* #return Post
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set publication
*
* #param boolean $publication
* #return Post
*/
public function setPublication($publication)
{
$this->publication = $publication;
return $this;
}
/**
* Get publication
*
* #return boolean
*/
public function getPublication()
{
return $this->publication;
}
/**
* Set slug
*
* #param string $slug
* #return Post
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Get slug
*
* #return string
*/
public function getSlug()
{
return $this->slug;
}
/**
* Set created_at
*
* #param \DateTime $createdAt
* #return Post
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* #return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
public function __construct()
{
$this->created_at = new \DateTime("now");
}
public function __toString()
{
return $this->getTitle();
}
}
The name of your field should be "tags" instead of "collection" I guess.
I don't see a property "collection" in your Entity.
The first parameter of the add method should be the name of your property defined in your Entity.
namespace DN\SiteBundle\Admin;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\CoreBundle\Validator\ErrorElement;
use Knp\Menu\ItemInterface as MenuItemInterface;
class PostAdmin extends Admin
{
// Fields to be shown on create/edit forms
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title', 'text', array('label' => 'Post Title'))
->add('content')
->add('publication')
->add('author')
->add('collection', 'sonata_type_model_list', array('required' => false));
}
//...
}
?>
Documentation on forms : https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html
How to set up fixtures for symfony 2 in for many to many relationship,
Following 2 entities are made by command lines,
And then added some lines for many to many relationship
Entity 1: Blog Class
<?php
namespace Acme\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Blog
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Acme\MainBundle\Entity\BlogRepository")
*/
class Blog {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="tag", inversedBy="blogs")
* #ORM\JoinTable(name="blog_tag",
* joinColumns={#ORM\JoinColumn(name="blog_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
* )
**/
protected $tags;
public function __construct()
{
$this->tags = new ArrayCollection();
}
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="subTitle", type="text")
*/
private $subTitle;
/**
* #var string
*
* #ORM\Column(name="author", type="string", length=255)
*/
private $author;
/**
* #var \DateTime
*
* #ORM\Column(name="createdAt", type="datetime")
*/
private $createdAt;
/**
* #var \DateTime
*
* #ORM\Column(name="updatedAt", type="datetime")
*/
private $updatedAt;
/**
* #var string
*
* #ORM\Column(name="content", type="text")
*/
private $content;
/**
* #var boolean
*
* #ORM\Column(name="isPublished", type="boolean")
*/
private $isPublished;
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Blog
*/
public function setTitle($title) {
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle() {
return $this->title;
}
/**
* Set author
*
* #param string $author
* #return Blog
*/
public function setAuthor($author) {
$this->author = $author;
return $this;
}
/**
* Get author
*
* #return string
*/
public function getAuthor() {
return $this->author;
}
/**
* Set createdAt
*
* #param \DateTime $createdAt
* #return Blog
*/
public function setCreatedAt($createdAt) {
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* #return \DateTime
*/
public function getCreatedAt() {
return $this->createdAt;
}
/**
* Set updatedAt
*
* #param \DateTime $updatedAt
* #return Blog
*/
public function setUpdatedAt($updatedAt) {
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* #return \DateTime
*/
public function getUpdatedAt() {
return $this->updatedAt;
}
/**
* Set content
*
* #param string $content
* #return Blog
*/
public function setContent($content) {
$this->content = $content;
return $this;
}
/**
* Get content
*
* #return string
*/
public function getContent() {
return $this->content;
}
/**
* Set isPublished
*
* #param boolean $isPublished
* #return Blog
*/
public function setIsPublished($isPublished) {
$this->isPublished = $isPublished;
return $this;
}
/**
* Get isPublished
*
* #return boolean
*/
public function getIsPublished() {
return $this->isPublished;
}
public function __toString() {
return strval($this->id);
}
/**
* Set subTitle
*
* #param string $subTitle
* #return Blog
*/
public function setSubTitle($subTitle)
{
$this->subTitle = $subTitle;
return $this;
}
/**
* Get subTitle
*
* #return string
*/
public function getSubTitle()
{
return $this->subTitle;
}
/**
* Add tags
*
* #param \Acme\MainBundle\Entity\tag $tags
* #return Blog
*/
public function addTag(\Acme\MainBundle\Entity\tag $tags)
{
$this->tags[] = $tags;
$tags->addBlog($this);
return $this;
}
/**
* Remove tags
*
* #param \Acme\MainBundle\Entity\tag $tags
*/
public function removeTag(\Acme\MainBundle\Entity\tag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Get tags
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTags()
{
return $this->tags;
}
}
Entity 2: Tag Class
<?php
namespace Acme\MainBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Tag
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Acme\MainBundle\Entity\TagRepository")
*/
class Tag {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="blog", mappedBy="tags")
**/
protected $blogs;
/**
* #var string
*
* #ORM\Column(name="tag", type="string", length=255)
*/
private $tag;
/**
* #var boolean
*
* #ORM\Column(name="isPublished", type="boolean")
*/
private $isPublished;
/**
* Get id
*
* #return integer
*/
public function getId() {
return $this->id;
}
/**
* Set tag
*
* #param string $tag
* #return Tag
*/
public function setTag($tag) {
$this->tag = $tag;
return $this;
}
/**
* Get tag
*
* #return string
*/
public function getTag() {
return $this->tag;
}
/**
* Set isPublished
*
* #param boolean $isPublished
* #return Tag
*/
public function setIsPublished($isPublished) {
$this->isPublished = $isPublished;
return $this;
}
/**
* Get isPublished
*
* #return boolean
*/
public function getIsPublished() {
return $this->isPublished;
}
public function __toString() {
return strval($this->id);
}
/**
* Constructor
*/
public function __construct()
{
$this->blogs = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add blogs
*
* #param \Acme\MainBundle\Entity\blog $blogs
* #return Tag
*/
public function addBlog(\Acme\MainBundle\Entity\blog $blogs)
{
$this->blogs[] = $blogs;
$blogs->addTag($this);
return $this;
}
/**
* Remove blogs
*
* #param \Acme\MainBundle\Entity\blog $blogs
*/
public function removeBlog(\Acme\MainBundle\Entity\blog $blogs)
{
$this->blogs->removeElement($blogs);
}
/**
* Get blogs
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getBlogs()
{
return $this->blogs;
}
}
Attention!!!
You have a circular reference in your code!!!
When you add a tag to a blog, it call add blog to tag and so on, please fix it in like this:
Blog
/**
* Add tags
*
* #param \Acme\MainBundle\Entity\tag $tags
* #return Blog
*/
public function addTag(\Acme\MainBundle\Entity\tag $tags)
{
if (!$this->tags->contains($tags))
{
$this->tags[] = $tags;
$tags->addBlog($this);
}
return $this;
}
Tag
/**
* Add blogs
*
* #param \Acme\MainBundle\Entity\blog $blogs
* #return Tag
*/
public function addBlog(\Acme\MainBundle\Entity\blog $blogs)
{
if (!$this->blogs->contains($blogs))
{
$this->blogs[] = $blogs;
$blogs->addTag($this);
}
return $this;
}
For your question:
First, install and configure the DoctrineFixturesBundle.
Use the ordered fixture features to load first the tag then the blog.
Then I write for you this example fixtures:
TagFixtures
<?php
namespace Acme\MainBundle\DataFixtures\ORM;
use Acme\MainBundle Bundle\Entity\Tag;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class TagFixtures extends AbstractFixture implements OrderedFixtureInterface {
/**
* Load data fixtures with the passed EntityManager
*
* #param \Doctrine\Common\Persistence\ObjectManager $manager
*/
function load(ObjectManager $manager)
{
$tag1 = new Tag();
$tag1->setTag("tag name 1");
$tag1->setIsPublished(true);
$tag2 = new Tag();
$tag2->setTag("tag name 2");
$tag2->setIsPublished(true);
$manager->persist($tag1);
$manager->persist($tag2);
$this->addReference('tag-tag_1', $tag1);
$this->addReference('tag-tag_2', $tag2);
$manager->flush();
}
/**
* Get the order of this fixture
*
* #return integer
*/
function getOrder()
{
return 1;
}
}
BlogFixtures
<?php
namespace Acme\MainBundle\DataFixtures\ORM;
use Acme\MainBundle\Entity\Blog;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class BlogFixtures extends AbstractFixture implements OrderedFixtureInterface {
/**
* Load data fixtures with the passed EntityManager
*
* #param \Doctrine\Common\Persistence\ObjectManager $manager
*/
function load(ObjectManager $manager)
{
$blog = new Blog();
$blog->setTitle('Happy new year');
$blog->setAuthor("me");
$blog->setIsPublished(true);
$blog->setContent("Happy new year");
$blog->setSubTitle("2015");
$blog->setCreatedAt(new \DateTime('now'));
$blog->setUpdatedAt(new \DateTime('now'));
$blog->addTag($this->getReference('tag-tag_1'));
// $blog->addTag($this->getReference('tag-tag_2'));
$manager->persist($blog);
// $this->addReference('blog-blog_1', $blog);
$manager->flush();
}
/**
* Get the order of this fixture
*
* #return integer
*/
function getOrder()
{
return 2;
}
}
Hope this help and happy new year!
For simple cases I create my fixtures like:
Acme/DemoBundle/Entity/Tag.php
public function addBlog(\Acme\MainBundle\Entity\blog $blog)
{
$this->blogs[] = $blog;
return $this;
}
Acme/DemoBundle/Entity/Blog.php
public function addTag(\Acme\MainBundle\Entity\tag $tags)
{
$this->tags[] = $tags;
$tags->addBlog($this);
return $this;
}
Acme/DemoBundle/DataFixtures/ORM/TagBlogFixture.php
class TagBlogFixture extends AbstractFixture implements OrderedFixtureInterface {
function load(ObjectManager $manager)
{
$tag1 = new Tag();
// setters
$tag2 = new Tag();
// setters
$blog1 = new Blog();
// setters
$blog1
->addTag($tag1)
->addTag($tag2)
;
$blog2 = new Blog();
// setters
$blog2->addTag($tag2);
$manager->persist($tag1);
$manager->persist($tag2);
$manager->persist($blog1);
$manager->persist($blog2);
$manager->flush();
}
function getOrder()
{
return 1;
}
}
This doesn't share any references between objects, creates them in single file..