Symfony2 Embedded Forms one to many relationship - php

I have a doubt w.r.t Embedded Forms. I have a "Properties" entity which has 2 fields "id" and "name" and "Amenities" entity which has 2 fields "id" and "value"(id=1,value=wifi,id=2,value=water,id=3,value=food).I have created these 2 entities,but now I want to save the id of "Properties" and "Amenities" into a separate 3rd table.So, I have created a third entity "Properties_Amenities" and its table "properties_amenities".I have created a "one to many" relationship as shown below.I want to embed "Amenities" into "Properties" with the click of an "Add Amenities" button on the "Properties Form".I am not sure as to where to place the "addAmenities()" and "removeAmenities()" functions in the Entity files. Below are 3 entities:-
Properties.php
<?php
namespace EpitaEvents\FormBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Properties
*
* #ORM\Table(name="properties")
* #ORM\Entity
*/
class Properties
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="Properties_Amenities", mappedBy="properties",cascade={"persist"})
**/
private $properties_amenities1;
public function __construct() {
$this->properties_amenities1 = new ArrayCollection();
}
/**
* Get id
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*/
public function getName()
{
return $this->name;
}
/**
* Get properties_amenities1
*/
public function getProperties_Amenities1()
{
return $this->properties_amenities1;
}
/**
* Set properties_amenities1
*
*/
public function setProperties_Amenities1($properties_amenities1)
{
$this->properties_amenities1 = $properties_amenities1;
return $this;
}
/**
* Add Amenities
*
* #return Properties
*/
public function addAmenities(\EpitaEvents\FormBundle\Entity\Properties_Amenities $amenities)
{
$amenities->setProperties($this);
$this->amenities[] = $amenities;
return $this;
}
/**
* Remove Amenities
*
*/
public function removeAmenities(\EpitaEvents\FormBundle\Entity\Properties_Amenities $amenities) {
$this->amenities->removeElement($amenities);
}
}
Amenities.php
<?php
namespace EpitaEvents\FormBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Amenities
*
* #ORM\Table(name="amenities")
* #ORM\Entity
*/
class Amenities
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="value", type="string", length=100)
*/
private $value;
/**
* #ORM\OneToMany(targetEntity="Properties_Amenities", mappedBy="amenities")
**/
private $properties_amenities2;
public function __construct() {
$this->properties_amenities2 = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set value
*
* #param string $value
* #return Amenities
*/
public function setValue($value)
{
$this->value = $value;
return $this;
}
/**
* Get value
*
* #return string
*/
public function getValue()
{
return $this->value;
}
/**
* Set Properties
*
*/
public function setProperties_Amenities2($properties_amenities2)
{
$this->properties_amenities2 = $properties_amenities2;
return $this;
}
/**
* Get Properties
*
* #return EpitaEvents\FormBundle\Entity\Properties
*/
public function getProperties_Amenities2()
{
return $this->properties_amenities2;
}
}
(Also should the __construct() be put in these 2 above files or in "Properties_Amenities.php"(Below)???)
Properties_Amenities.php
<?php
namespace EpitaEvents\FormBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Properties_Amenities
*
* #ORM\Table(name="properties_amenities")
* #ORM\Entity
*/
class Properties_Amenities
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Properties", inversedBy="properties_amenities1")
* #ORM\JoinColumn(name="properties_id", referencedColumnName="id")
**/
private $properties;
/**
* #ORM\ManyToOne(targetEntity="Amenities", inversedBy="properties_amenities2")
* #ORM\JoinColumn(name="amenities_id", referencedColumnName="id")
**/
private $amenities;
function getId() {
return $this->id;
}
function getProperties() {
return $this->properties;
}
function getAmenities() {
return $this->amenities;
}
function setProperties($properties) {
$this->properties = $properties;
}
function setAmenities($amenities) {
$this->amenities = $amenities;
}
}
Can any one suggest as to where to place the "addAmenities()" and "removeAmenities()" functions as I want to save the "id's" of "Properties.php" and "Amenities.php" in a "SEPARATE, THIRD" Entity and table??

Related

WebfactoryPolyglotBundle use causes error "No mapping found for field 'id' on class 'AppBundle\Entity\Film'."

I use the Symfony version 3.4.0 and I try to use this bundle to translate entities : https://github.com/webfactory/WebfactoryPolyglotBundle
So I created two entities ( Film and FilmTranslation ) for the test
Here my Film.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Annotation as Polyglot;
use Webfactory\Bundle\PolyglotBundle\TranslatableInterface;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Film
*
* #ORM\Table(name="film")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmRepository")
* #Polyglot\Locale(primary="fr_FR")
*/
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Polyglot\TranslationCollection
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="Titre", type="string", length=255, unique=true)
* #Polyglot\Translatable
* #var string|TranslatableInterface
*/
private $titre;
/**
* #ORM\OneToMany(targetEntity="FilmTranslation", mappedBy="entity")
* #Polyglot\TranslationCollection
* #var Collection
*/
private $translations;
public function __construct()
{
$this->translations = new ArrayCollection();
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
*
* #return Film
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Add translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*
* #return Film
*/
public function addTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations[] = $translation;
return $this;
}
/**
* Remove translation
*
* #param \AppBundle\Entity\FilmTranslation $translation
*/
public function removeTranslation(\AppBundle\Entity\FilmTranslation $translation)
{
$this->translations->removeElement($translation);
}
/**
* Get translations
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getTranslations()
{
return $this->translations;
}
}
And here my FilmTranslation.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Webfactory\Bundle\PolyglotBundle\Entity\BaseTranslation;
/**
* FilmTranslation
*
* #ORM\Table(name="film_translation")
* #ORM\Entity(repositoryClass="AppBundle\Repository\FilmTranslationRepository")
* #ORM\Table(
* uniqueConstraints = {
* #ORM\UniqueConstraint(columns={"entity_id", "locale"})
* }
* )
*/
class FilmTranslation extends BaseTranslation
{
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255, unique=true)
*/
private $titre;
/**
* #ORM\ManyToOne(targetEntity="Film", inversedBy="translations")
* #var Film
*/
protected $entity;
/**
* Set titre
*
* #param string $titre
*
* #return FilmTranslation
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set locale
*
* #param string $locale
*
* #return FilmTranslation
*/
public function setLocale($locale)
{
$this->locale = $locale;
return $this;
}
/**
* Set entity
*
* #param \AppBundle\Entity\Film $entity
*
* #return FilmTranslation
*/
public function setEntity(\AppBundle\Entity\Film $entity = null)
{
$this->entity = $entity;
return $this;
}
/**
* Get entity
*
* #return \AppBundle\Entity\Film
*/
public function getEntity()
{
return $this->entity;
}
}
I'm able to create a form but when I try to persist and flush I've the following error :
No mapping found for field 'id' on class 'AppBundle\Entity\Film'.
Is something I'm doing wrong ?
So remove #Polyglot\TranslationCollection from $id annotation like this ;)
class Film
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;

Symfony does not return Arraycollection(one to many)

I have a problem with an empty ArrayCollection.
With one-to-many and many-to-one relationships
This is Brand entity:
class Brand
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var Model[]
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Model", mappedBy="brand")
*/
private $models;
/**
* Brand constructor.
*/
public function __construct()
{
$this->models= new ArrayCollection();
}
/**
* #return Model[]|ArrayCollection
*/
public function getModels()
{
return $this->models;
}
/**
* #param Model $model
* #return $this
*/
public function addModel(Model $model)
{
$this->models[] = $model;
return $this;
}
}
And this is the Model entity:
class Model
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="modelName", type="string", length=60)
*/
private $modelName;
/**
* #var Brand
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Brand", inversedBy="models")
* #ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
private $brand;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set modelName
*
* #param string $modelName
*
* #return Model
*/
public function setModelName($modelName)
{
$this->modelName = $modelName;
return $this;
}
/**
* Get modelName
*
* #return string
*/
public function getModelName()
{
return $this->modelName;
}
/**
* #param Brand $brand
* #return $this
*/
public function setBrand(Brand $brand)
{
$this->brand= $brand;
return $this;
}
/**
* #return Brand
*/
public function getBrand()
{
return $this->brand;
}
}
And Controller:
$UserRepo = $this->getDoctrine()->getRepository(Brand::class);
$all = $UserRepo->find($brandId);
Symfony does not return any error.
However, it only downloads the brandName corresponding to $ brandId. Unfortunately, the ArrayCollection is empty.
Can someone tell me where I made a mistake :(

Symfony 2 Entity Repository find() returns empty array

Thank you all for your answers from now. ThatÅ› the question. I have a symfony 2 app with two entities (Tasks and Products). When i tried to find (findBy,findOneBy,findAll) a product it returns an empty array.
Tasks Entity
<?php
namespace pablo\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Task
*
* #ORM\Table(name="tasks")
* #ORM\Entity(repositoryClass="pablo\UserBundle\Repository\TaskRepository")
* #ORM\HasLifecycleCallbacks()
*/
class Task
{
/**
* #ORM\ManyToOne(targetEntity="User", inversedBy="tasks")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $user;
/**
* #ORM\ManyToOne(targetEntity="Product", inversedBy="task")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $product;
public function __construct()
{
$this->tasks = new ArrayCollection();
}
/**
* Set product
*
* #param \pablo\UserBundle\Entity\Product $product
*
* #return Task
*/
public function setProduct(\pablo\UserBundle\Entity\Product $product = null)
{
$this->product = $product;
return $this;
}
/**
* Get product
*
* #return \pablo\UserBundle\Entity\Product
*/
public function getProduct()
{
return $this->product;
}
/**
* #return ArrayCollection
*/
public function getTasks()
{
return $this->tasks;
}
/**
* #param ArrayCollection $tasks
*/
public function setTasks($tasks)
{
$this->tasks = $tasks;
}
And Products Entity
<?php
namespace pablo\UserBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Products
*
* #ORM\Table(name="products")
* #ORM\Entity(repositoryClass="pablo\UserBundle\Repository\ProductsRepository")
* #UniqueEntity("productsName")
*/
class Product
{
/**
* #ORM\OneToMany(targetEntity="Task", mappedBy="product")
*/
protected $task;
/**
* #ORM\OneToMany(targetEntity="Publicaciones", mappedBy="product")
*/
protected $publicaciones;
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="products_name", type="string", length=255)
* #Assert\NotBlank()
*/
private $productsName;
public function __construct()
{
$this->task = new ArrayCollection();
}
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set productsName
*
* #param string $productsName
*
* #return Product
*/
public function setProductsName($productsName)
{
$this->productsName = $productsName;
return $this;
}
/**
* Get productsName
*
* #return string
*/
public function getProductsName()
{
return $this->productsName;
}
public function __toString() {
return $this->productsName;
}
/**
* Get task
*
* #return \Doctrine\Common\Collections\ArrayCollection
*/
public function getTask()
{
return $this->task;
}
/**
* Set task
*
* #param \Doctrine\Common\Collections\ArrayCollection $typeSponsor
*
* #return Task
*/
public function setTask($task)
{
$this->task = $task;
}
/**
* #return mixed
*/
public function getPublicaciones()
{
return $this->publicaciones;
}
/**
* #param mixed $publicaciones
*/
public function setPublicaciones($publicaciones)
{
$this->publicaciones = $publicaciones;
}
}
Now, when i tried to find a product from controller it returns an empty array ({}). I can't see what is wrong with this.
$productId = '18';
$product = $this->get('doctrine.orm.default_entity_manager')->getRepository('pabloUserBundle:Product')->find($productId);
Actually you have a result, it just is an empty object because you have not defined which of the properties should be printed.
The best solution is for your entity to implement JsonSerializable.
class Product implements \JsonSerializable
{
...
public function jsonSerialize()
{
return [
"id"=> $this->getId(),
"name" => $this->getProductsName()
];
}
Now it knows what it should print when converting the class to json object.
If you want the task collection also, implement JsonSerializable for the Task Entity and add in Product Entity:
public function jsonSerialize()
{
return [
"id"=> $this->getId(),
"name" => $this->getProductsName(),
"task" => $this->getTask()->toArray()
];
}
The JsonSerializable interface

Symfony 3.3 Schema Validation Error

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).

OneupUploaderBundle relation between 2 entities

I'm new on stackoverflow and I hope you can help me.
I use OneupUploaderBundle on symfony2 and it's working I can upload multiple images and I can persist them in my database.
But now I would like to upload multiple images for one 'Annonce' for exemple so, I have 2 entities
Annonce
namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Annonce
*
* #ORM\Table(name="annonce")
* #ORM\Entity(repositoryClass="TestBundle\Repository\AnnonceRepository")
*/
class Annonce
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=255)
*/
private $titre;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set titre
*
* #param string $titre
* #return Annonce
*/
public function setTitre($titre)
{
$this->titre = $titre;
return $this;
}
/**
* Get titre
*
* #return string
*/
public function getTitre()
{
return $this->titre;
}
}
And my Entity Image
namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Image
*
* #ORM\Table(name="image")
* #ORM\Entity(repositoryClass="TestBundle\Repository\ImageRepository")
*/
class Image
{
/**
* #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;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* #param string $name
* #return Image
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* #return string
*/
public function getName()
{
return $this->name;
}
/**
* #ORM\ManyToOne(targetEntity="TestBundle\Entity\Annonce")
* #ORM\JoinColumn(nullable=false)
*/
private $annonce;
/**
* Set annonce
*
* #param \TestBundle\Entity\Annonce $annonce
* #return Image
*/
public function setAnnonce(\TestBundle\Entity\Annonce $annonce)
{
$this->annonce = $annonce;
return $this;
}
/**
* Get annonce
*
* #return \TestBundle\Entity\Annonce
*/
public function getAnnonce()
{
return $this->annonce;
}
}
My EventListener
namespace TestBundle\EventListener;
use Doctrine\Common\Persistence\ObjectManager;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use TestBundle\Entity\Annonce;
use TestBundle\Entity\Image;
class UploadListener
{
/**
* #var ObjectManager
*/
private $em;
public function __construct(ObjectManager $em)
{
$this->em = $em;
}
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$annonce = new Annonce();
$image = new Image();
/* TEST */
$annonce->setTitre("I'am a title");
$this->em->persist($annonce);
$image->setName($file->getFilename());
$image->setAnnonce($annonce);
$this->em->persist($image);
$this->em->flush();
$response = $event->getResponse();
$response['files'] = [
'name' => $file->getFilename(),
'size' => $file->getSize()
];
}
The problem is, when I upload many images, I have the same number of row in Annonce as in Image.
Someone can help me please :)

Categories