I hope I can be clear in my question.
In my symfony2 project I'm following this tutorial to make a form in which I can press the link to add as many "auteur" as the user wants, so I tried to do it with form collection.
Everything works fine, except the many to one field that always gets "null" value and not the "id".
Here is what I have done.
Soumission entity in which I have the add "auteur" link
....
/**
* #ORM\OneToMany(targetEntity="tuto\BackofficeBundle\Entity\Auteur",
mappedBy="soumission", cascade={"persist"})
*/
protected $auteurs;
.....
/**
* #param Collection $auteurs
* #return $this
*/
public function setAuteurs(Collection $auteurs)
{
$this->auteurs = $auteurs;
return $this;
}
/**
* Add auteur
*
* #param \tuto\BackofficeBundle\Entity\Auteur $auteur
* #return Soumission
*/
public function addAuteur(Auteur $auteur)
{
if ( ! $this->auteurs->contains($auteur) ) {
$auteur->setSoumission($this); // this should set the "soumission-
id" field
$this->auteurs->add($auteur);
}
return $this->auteurs;
}
public function removeAuteur(Auteur $auteur)
{
if ($this->auteurs->contains($auteur)) {
$this->auteurs->removeElement($auteur);
}
return $this->auteurs;
}
/**
* #return mixed
*/
public function getAuteurs()
{
return $this->auteurs;
}
This is the auteur entity:
/**
* #ORM\ManyToOne(targetEntity="Soumission", inversedBy="auteur")
* #ORM\JoinColumn(name="soumission_id", referencedColumnName="id")
*/
protected $Soumission;
/**
* #param $soumission
* #return $this
*/
public function setSoumission(Soumission $soumission)
{
$this->soumission = $soumission;
return $this;}
/**
* #return mixed
*/
public function getSoumission()
{
return $this->soumission;
}
public function __toString() {
return $this->Soumission;
}
the result should be like the second line and not null value
try to do this :
/**
* #ORM\OneToMany(targetEntity="Auteur",mappedBy="soumission", cascade={"persist"})
*/
protected $auteurs;
/**
* Add auteurs
* #return FormationMsk
*/
public function addAuteur(\tuto\BackofficeBundle\Entity\Auteur $auteurs)
{
$this->auteurs[] = $auteurs;
return $this;
}
/**
* Remove auteurs
*/
public function removeAuteur(\tuto\BackofficeBundle\Entity\Auteur $auteurs)
{
$this->auteurs->removeElement($auteurs);
}
/**
* Get auteurs
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getAuteurs()
{
return $this->auteurs;
}
public function __construct()
{
$this-auteurs = new \Doctrine\Common\Collections\ArrayCollection();
}
and for the auteur entity :
/**
* #ORM\ManyToOne(targetEntity="Soumission", inversedBy="auteurs")
* #ORM\JoinColumn(name="soumission_id", referencedColumnName="id")
*/
protected $soumission;
Related
I have problem with my symfony code:
My repository update method
/**
* #param MenuModel $menu
* #return MenuEntity|MenuModel
* #throws RepositoryException
*/
public function updateMenu(MenuModel $menu)
{
try {
$transformedMenu = $this->menuTransformer->transform($menu);
$transformedMenu = $this->getEntityManager()->merge($transformedMenu);
$this->getEntityManager()->flush($transformedMenu);
$this->getEntityManager()->detach($transformedMenu);
return $this->menuTransformer->transform($transformedMenu);
} catch (\Exception $e) {
throw new RepositoryException($e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
}
}
My Element entity:
/**
* Element.
*
* #ORM\HasLifecycleCallbacks
* #ORM\Table(name="element")
* #ORM\ChangeTrackingPolicy("DEFERRED_EXPLICIT")
* #ORM\Entity(repositoryClass="CP\API\Repository\ElementRepository")
*/
class Element extends \CP\RestBundle\Model\Element
{
use Traits\SystemObjectTrait;
use Traits\ChannelsTrait;
use Traits\PropertiesTrait;
use Traits\WorkflowTrait;
/**
* #ORM\ManyToOne(targetEntity="Menu", inversedBy="contents")
* #ORM\JoinColumn(name="menu_id", referencedColumnName="id")
*/
protected $menu;
/**
* Element constructor.
*/
public function __construct()
{
parent::__construct();
}
/**
* #param int $id
*
* #return Element
*/
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
/**
* Update publication by modification when empty
* Update status according to publication, unpublication and archiving
*
* #ORM\PrePersist()
*/
protected function prePersist()
{
$this->updatePublication();
$this->updateStatus();
}
/**
* Update publication by modification when empty
* Update status according to publication, unpublication and archiving
*
* #ORM\PreUpdate()
*/
public function preUpdate()
{
$this->updatePublication();
$this->updateStatus();
}
/**
* Increases object version
*/
public function increaseVersion()
{
++$this->version;
}
/**
* #return mixed
*/
public function getMenu()
{
return $this->menu;
}
/**
* #param mixed $menu
*/
public function setMenu($menu): void
{
$this->menu = $menu;
}
}
My Menu entity
<?php
namespace CP\API\Entity;
use CP\API\Entity\Traits\TimestampableTrait;
use CP\Model\Configuration;
use CP\Model\Content;
use CP\Model\Language;
use CP\Model\MenuTranslation;
use CP\RestBundle\Model\Locator;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\HasLifecycleCallbacks()
* #ORM\Table(name="custom_menu")
* #ORM\Entity(repositoryClass="CP\API\Repository\MenuRepository")
*/
class Menu
{
use TimestampableTrait;
/**
* #var int
*
* #ORM\Id()
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToMany(targetEntity="CP\API\Entity\Element",mappedBy="menu",cascade={"persist"})
*/
protected $contents;
public function __construct(Menu $parent = null)
{
$dateTime = new \DateTime();
$this->creation = $dateTime;
$this->modification === null && $this->setModification($dateTime);
$this->contents = new ArrayCollection();
}
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* #return ArrayCollection
*/
public function getContents(): ArrayCollection
{
return $this->contents;
}
/**
* #param ArrayCollection $contents
*/
public function setContents(ArrayCollection $contents): void
{
$this->contents = $contents;
}
/**
* #param Element $element
* #return $this
*/
public function addContent(Element $element): self
{
if (!$this->contents->contains($element)) {
$this->contents[] = $element;
$element->setMenu($this);
}
return $this;
}
/**
* #param Element $element
* #return $this
*/
public function removeContent(Element $element): self
{
if ($this->contents->contains($element)) {
$this->contents->removeElement($element);
if ($element->getMenu() === $this) {
$element->setMenu(null);
}
}
return $this;
}
}
My menu model
<?php
namespace CP\Model;
use CP\RestBundle\Model\Element;
use CP\RestBundle\Model\Locator;
use CP\RestBundle\Model\Product;
use CP\RestBundle\Model\Traits\TimestampableTrait;
class Menu extends BaseModel
{
/** #var Content[] */
protected $content;
/**
* #return array|null
*/
public function getContent(): ?array
{
return $this->content;
}
/**
* #param Content[] $content
*/
public function setContent(array $content): void
{
$this->content = $content;
}
}
My transformer from model to entity
public function transform($object)
{
if ($object instanceof Menu) {
$menuData = new MenuEntity();
if ($object->getId())
$menuData->setId($object->getId());
if ($object->getContent() instanceof Content) {
$contentEntity = new ContentEntity();
$content = $object->getContent();
$contentEntity->setId($content->getId());
$menuData->addContent($this->attachToEntityManager($contentEntity));
}
return $menuData;
}
private function attachToEntityManager($object)
{
try {
$attachedObject = $this->entityManager->merge($object);
return $attachedObject;
} catch (ORMException $e) {
throw new ModelTransformationException(sprintf('Model transformation error, object could not be attached to Entity Manager: %s in %s',
$e->getMessage(), $e->getFile() . ':' . $e->getLine()));
}
}
When i try saved data then i don't have any error, but on database nothing change i.e. element entity don't have assign menu_id from relation. I don't know what is wrong, maybe i don't know how to use and save OneToMany relation.
Any idea?
Can you give us more context of what your code is doing ? The constructor of MenuEntity is strange, no use of $parent, a boolean comparaison isn't used. Why do you need to use detach and merge methods of entityManager
- Mcsky
Is right, I don't see the point of using detach and/or merge here.
Normally with a OneToMany relationship you use entity manager and persist both relations, flush and you should have an entity with a OneToMany relationship.
I hope this might be helpful, specifically this part: https://symfony.com/doc/current/doctrine/associations.html#saving-related-entities
example:
// relates this product to the category
$product->setCategory($category);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($category);
$entityManager->persist($product);
$entityManager->flush();
I have two Entities
User
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(name="first_name", type="text", nullable=true)
*/
protected $firstName;
/**
* #ORM\Column(name="last_name", type="text", nullable=true)
*/
protected $lastName;
/**
* #ORM\Column(type="bigint", nullable=true)
*/
protected $phone;
/**
* #ORM\Column(name="birth_date", type="date", nullable=true)
*/
protected $birthDate;
/**
* #ORM\Column(type="text", nullable=true)
*/
protected $gender;
/**
* #ORM\Column(name="location_country", type="text", nullable=true)
*/
protected $locationCountry;
/**
* #ORM\Column(name="location_city", type="text", nullable=true)
*/
protected $locationCity;
/**
* #ORM\Column( type="text", nullable=true)
*/
protected $avatar;
/**
* #ORM\Column(name="wall_image", type="text", nullable=true)
*/
protected $wallImage;
/**
* #ORM\Column( type="text", nullable=true)
*/
protected $about;
/**
* #ORM\OneToMany(targetEntity="Follower", mappedBy="user")
*/
protected $followers;
/**
* #ORM\OneToMany(targetEntity="Follower", mappedBy="follower")
*/
protected $followings;
/**
* Is followed. Used when checking is followed by another user.
*/
protected $isFollowed = false;
/**
* #ORM\OneToMany(targetEntity="Photo", mappedBy="user")
* #ORM\OrderBy({"id" = "DESC"})
*/
protected $photos;
public function getFirstName()
{
return $this->firstName;
}
public function getLastName()
{
return $this->lastName;
}
public function getPhone()
{
return $this->phone;
}
public function getBirthDate()
{
return $this->birthDate;
}
public function getGender()
{
return $this->gender;
}
public function getLocationCountry()
{
return $this->locationCountry;
}
public function getLocationCity()
{
return $this->locationCity;
}
public function getAvatar()
{
return $this->avatar;
}
public function getAvatarImage()
{
return $this->getAvatarPath().$this->avatar;
}
public function getWallImage()
{
return $this->wallImage;
}
public function getAbout()
{
return $this->about;
}
public function getAvatarPath()
{
return '/web/uploads/avatars/'.$this->id.'/';
}
public function getWallImagePath()
{
return '/web/uploads/wall/'.$this->id.'/';
}
public function getFollowers()
{
return $this->followers;
}
public function getFollowings()
{
return $this->followings;
}
public function getFollowersCount()
{
//print_r($this->followers->toArray());
}
public function getPhotos()
{
return $this->photos;
}
public function isFollowed()
{
return $this->isFollowed;
}
public function setFirstName($value)
{
$this->firstName = $value;
}
public function setLastName($value)
{
$this->lastName = $value;
}
public function setPhone($value)
{
$this->phone = $value;
}
public function setBirthDate($value)
{
$this->birthDate = $value;
}
public function setGender($value)
{
$this->gender = $value;
}
public function setLocationCountry($value)
{
$this->locationCountry = $value;
}
public function setLocationCity($value)
{
$this->locationCity = $value;
}
public function setAvatar($path)
{
$this->avatar = $path;
}
public function setWallImage($path)
{
$this->wallImage = $path;
}
public function setAbout($about)
{
$this->about = $about;
}
public function setFollowed($isFollowed)
{
$this->isFollowed = $isFollowed;
}
}
Photo
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="photos")
*/
class Photo
{
const
CATEGORY_PHOTOGRAPHY = 1,
CATEGORY_PAINTING = 2,
CATEGORY_3D = 3;
/*
* Flow photos limit
*/
const FLOW_PHOTOS_LIMIT = 15;
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="photos")
*/
protected $user;
/**
* #ORM\Column(type="text", nullable=true)
*/
protected $title;
/**
* #ORM\Column(type="text", nullable=true)
*/
protected $description;
/**
* #ORM\Column(type="text")
*/
protected $name;
/**
* #ORM\ManyToOne(targetEntity="PhotoCategory")
*/
protected $category;
/**
* #ORM\Column(name="creation_date", type="datetime")
*/
protected $creationDate;
/**
* #ORM\Column(name="edit_date", type="datetime", nullable=true)
*/
protected $editDate;
/**
* #ORM\Column(name="is_moderated", type="boolean")
*/
protected $isModerated = false;
/**
* #ORM\Column(name="is_active", type="boolean")
*/
protected $isActive = true;
/**
* #ORM\OneToMany(targetEntity="Comment", mappedBy="photo")
* #ORM\OrderBy({"id" = "DESC"})
*/
protected $comments;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set user
*
* #param User $user
*
* #return Photo
*/
public function setUser($user)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* #return User $user
*/
public function getUser()
{
return $this->user;
}
/**
* Get category
*
* #return Category $category
*/
public function getCategory()
{
return $this->category;
}
/**
* Set title
*
* #param string $title
*
* #return Photo
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Get creationDate
*
* #return \DateTime
*/
public function getCreationDate()
{
return $this->creationDate;
}
/**
* Get editDate
*
* #return \DateTime
*/
public function getEditDate()
{
return $this->editDate;
}
/**
* Get is active
*
* #return integer
*/
public function isActive()
{
return $this->isActive;
}
/**
* Get is moderated
*
* #return integer
*/
public function isModerated()
{
return $this->isModerated;
}
/*
* Get image
*
* #return string
*/
public function getImage()
{
return $this->getWebDirectory().$this->getName();
}
/*
* Get image directory
*
* #return string
*/
public function getDirectory()
{
return __DIR__.'/../../../web/uploads/photos/'.$this->getUser()->getId().'/'.$this->creationDate->format('Y-m-d').'/';
}
/*
* Get image web directory
*
* #return string
*/
public function getWebDirectory()
{
return '/web/uploads/photos/'.$this->getUser()->getId().'/'.$this->creationDate->format('Y-m-d').'/';
}
/*
* Get comments
*/
public function getComments()
{
return $this->comments;
}
/**
* Set description
*
* #param string $description
*
* #return Photo
*/
public function setDescription($description)
{
$this->description = $description;
return $this;
}
/**
* Get description
*
* #return string
*/
public function getDescription()
{
return $this->description;
}
/**
* Set name
*
* #param string $name
*
* #return Photo
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* Set creationDate
*
* #param \DateTime $creationDate
*/
public function setCreationDate(\DateTime $creationDate)
{
$this->creationDate = $creationDate;
}
/**
* Set editDate
*
* #param \DateTime $editDate
*/
public function setEditDate(\DateTime $editDate)
{
$this->editDate = $editDate;
}
/**
* Set active
*/
public function setActive($active)
{
$this->isActive = $active;
}
/**
* Set category
*/
public function setCategory($category)
{
$this->category = $category;
}
/**
* Set moderated
*/
public function setModerated($moderated)
{
$this->isModerated = $moderated;
}
}
As you see, i have isActive in Photo, which is telling is photo deleted or not. So, i get all users photos via User->getPhotos() which is one-to-many. But it return all users photos. How should be done, so it returns all photos which have isActive = true?
thank you
you can try the filter method to query inside entities
$user->getPhotos()->filter(
function($photo) {
return $photo->isActive();
}
);
There are a number of ways you can do this.
Cosmin provided one for you, which will result in Doctrine loading each photo, and then PHP code checking to see whether or not it is active. This will work in the most number of situations, but will potentially be slow inefficient.
Yet another solution, is to use "criteria":
$exp = new \Doctrine\ORM\Query\Expr();
$activePhotos = $user->getPhotos()->matching(
new \Doctrine\Common\Collections\Criteria(
$exp->eq('active', true)
)
);
This will do something similar to the filter that Cosmin suggested, but allow for Doctrine to filter at the database level.
You can create a method inside the User entity, using the code of #Cosmin Ordean :) Something like this
public function getActivePhotos() {
return $this->getPhotos()->filter(
function($photo) {
return $photo->isActive();
}
);
}
I am having problem with LifecycleCallbacks in symfony2 not being executed even though I have #ORM\HasLifecycleCallbacks annotation. I am trying to follow example provided in http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html#using-the-id-as-the-filename. My goal is to save file under the document id.
The below provided code does not result in an error, other than the
The file could not be found. information in reloaded page.
To check what is going on I added die("TEST"); command to upload() function but it seems that it is never executed as the result was only the form page being reloaded with the above mentioned error.
I would like to ask you for advice what may be the reason that the upload() function is not executed?
Controller:
/**
* #Route("app/documents/add/", name="app_documents_add")
*/
public function addAction(Request $request)
{
/**
* This code is aimed at checking if the book is choseen and therefore whether any further works may be carried out
*/
$session = new Session();
if(!$session->get("App_Books_Chosen_Lp")) return new RedirectResponse($this->generateUrl('app_listbooks'));
// Authorization goes here
$documents = new Documents();
$form = $this->createForm(new DocumentsType(), $documents);
$form->add('save', 'submit', array('label' => 'Dodaj dokument'));
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
//$documents->upload();
$book = $em->getReference('AppBundle:Books', $session->get("App_Books_Chosen_Lp"));
if( $book ) $documents->setBook($book);
else die ("CRITICAL ERROR: addAction - Bad book id");
$em->persist($documents);
$em->flush();
}
return $this->render('AppBundle:Documents:adddocuments.html.twig', array('form' => $form->createView()));
}
Documents class:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
* #ORM\Table(name="Documents")
*/
class Documents
{
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="Books", inversedBy="documents")
* #ORM\JoinColumn(name="book_id", referencedColumnName="id")
*/
protected $book;
/**
* #ORM\Column(type="string", length=220)
*/
protected $marker;
/**
* #ORM\Column(type="date", length=220)
*/
protected $document_date;
/**
* #ORM\Column(type="string", length=220)
* #Assert\File(maxSize="6000000")
*/
protected $link;
/**
* #ORM\Column(type="text")
*/
protected $notes;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set marker
*
* #param string $marker
* #return Documents
*/
public function setMarker($marker)
{
$this->marker = $marker;
return $this;
}
/**
* Get marker
*
* #return string
*/
public function getMarker()
{
return $this->marker;
}
/**
* Set document_date
*
* #param \DateTime $documentDate
* #return Documents
*/
public function setDocumentDate($documentDate)
{
$this->document_date = $documentDate;
return $this;
}
/**
* Get document_date
*
* #return \DateTime
*/
public function getDocumentDate()
{
return $this->document_date;
}
/**
* Set link
*
* #param string $link
* #return Documents
*/
public function setLink($link)
{
$this->link = $link;
return $this;
}
/**
* Get link
*
* #return string
*/
public function getLink()
{
return $this->link;
}
/**
* Set notes
*
* #param string $notes
* #return Documents
*/
public function setNotes($notes)
{
$this->notes = $notes;
return $this;
}
/**
* Get notes
*
* #return string
*/
public function getNotes()
{
return $this->notes;
}
/**
* Set book
*
* #param \AppBundle\Entity\Books $book
* #return Documents
*/
public function setBook(\AppBundle\Entity\Books $book = null)
{
$this->book = $book;
return $this;
}
/**
* Get book
*
* #return \AppBundle\Entity\Books
*/
public function getBook()
{
return $this->book;
}
/*
* ### FILE UPLOAD PROCESS ###
*/
/**
* #Assert\File(maxSize="6000000")
*/
private $file;
public function getWebPath()
{
return null === $this->link
? null
: $this->getUploadDir().'/'.$this->link;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'uploads/documents';
}
/**
* Get file.
*
* #return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/*
* Temp fila path
*/
private $temp;
/**
* Sets file.
*
* #param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (is_file($this->getAbsolutePath())) {
// store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
} else {
$this->link = 'initial';
}
}
/**
* #ORM\PrePersist()
* #ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
$this->link = $this->getFile()->guessExtension();
}
}
/**
* #ORM\PostPersist()
* #ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// check if we have an old image
if (isset($this->temp)) {
// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = null;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does
$this->getFile()->move(
$this->getUploadRootDir(),
$this->id.'.'.$this->getFile()->guessExtension()
);
$this->setFile(null);
}
/**
* #ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->temp = $this->getAbsolutePath();
}
/**
* #ORM\PostRemove()
*/
public function removeUpload()
{
if (isset($this->temp)) {
unlink($this->temp);
}
}
public function getAbsolutePath()
{
return null === $this->link
? null
: $this->getUploadRootDir().'/'.$this->id.'.'.$this->link;
}
}
`
The answer why the upload() function was not executed was quite easy - the form is marked as invalid. Now I am looking for help why this happens (The file could not be found while using LifecycleCallbacks)
I have a entity :
/**
* #ORM\Entity
* #ORM\Table(name="organization")
*/
class Organization
{
/*
*#ORM\OneToMany(targetEntity="Link" , mappedBy="organization")
*/
private $links;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set title
*
* #param string $title
* #return Organization
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* #return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set site
*
* #param string $site
* #return Organization
*/
public function setSite($site)
{
$this->site = $site;
return $this;
}
/**
* Get site
*
* #return string
*/
public function getSite()
{
return $this->site;
}
public function __construct()
{
$this->links = new ArrayCollection();
}
}
when run command :
php console doctrine:generate:entities News/VillageNewsBundle/Entity/Organization
when i run this command all the getter and setter
will generated but getter and setter for relation not add to my entity?!
That's because your OneToMany annotation is wrong.
You must use the correct doc synthax, which is:
/**
* [...]
*/
Here, it misses one '*' before your OneToMany annotation. Add it and it will work. :-)
I want to set up a multiselect tags in my sonata admin form like in the picturebelow :
Here is the code of my entity :
class Company{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* #ORM\ManyToMany(targetEntity="City", cascade={"persist"})
*/
protected $city;
/**
* Constructor
*/
public function __construct()
{
$this->city = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add city
*
* #param \Company\AdminBundle\Entity\City $city
* #return Company
*/
public function addVille(\Company\AdminBundle\Entity\City $city)
{
$this->city[] = $city;
return $this;
}
/**
* Remove city
*
* #param \Company\AdminBundle\Entity\City $city
*/
public function removeCity(\Company\AdminBundle\Entity\City $city)
{
$this->city->removeElement($city);
}
/**
* Get city
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getCity()
{
return $this->city;
}
/**
* Set name
*
* #param string $name
* #return Company
*/
public function setName($name)
{
$this->name= $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
public function __toString()
{
return (string) $this->name;
}
}
So I want that the first select tag contains all the cities and in the second , I select any element I want.
how can I make this kind of type?
Sonata admin will automatically give you multiselect for properly configured Many-to-many relationships. You just need to use a jquery multiselect plugin to make the standard more useable, like your picture above.
See: https://github.com/yanickrochon/jquery.uix.multiselect