I'm trying to creating an API REST with the bundle FOSRestBundle (SF5).
I've an entity "Categorie" which can have an parent "Categorie".
Here is the entity :
<?php
namespace App\Entity\Main;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\ExclusionPolicy;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity(repositoryClass="App\Repository\CategorieRepository")
* #ORM\Table(name="categorie")
* #ExclusionPolicy("all")
*/
class Categorie
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #Expose
* #ORM\Column(type="integer")
*/
private $id;
/**
* #var string
* #Assert\NotBlank()
* #Expose
* #ORM\Column(type="string", length=255)
*/
private $libelle;
/**
* #var string
* #Assert\NotBlank()
* #Expose
* #ORM\Column(type="string", length=255)
*/
private $icone;
/**
* #var Categorie
* #ORM\ManyToOne(targetEntity="App\Entity\Main\Categorie", inversedBy="categories", cascade={"all"}, fetch="EAGER")
* #ORM\JoinColumn(name="categorie_parent_id", referencedColumnName="id", nullable=true)
*/
private $categorieParent;
/**
* #var ArrayCollection
* #ORM\OneToMany(targetEntity="App\Entity\Main\Categorie", mappedBy="categorieParent")
*/
private $categories;
/**
* #var ArrayCollection
* #ORM\OneToMany(targetEntity="App\Entity\Main\Produit", mappedBy="categorie")
*/
private $produits;
public function __construct()
{
$this->produits = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getLibelle(): ?string
{
return $this->libelle;
}
public function setLibelle(string $libelle): self
{
$this->libelle = $libelle;
return $this;
}
public function getIcone(): ?string
{
return $this->icone;
}
public function setIcone(string $icone): self
{
$this->icone = $icone;
return $this;
}
public function setCategorieParent(Categorie $categorieParent): self
{
$this->categorieParent = $categorieParent;
return $this;
}
public function getCategorieParent(Categorie $categorieParent)
{
return $this->categorieParent;
}
}
Here is my action in controller :
/**
* #Rest\View(statusCode=Response::HTTP_CREATED)
* #Rest\Post("/api/{_locale}/categorie/create", name="api_categorie_create")
* #ParamConverter("categorie", converter="fos_rest.request_body")
* #IsGranted("ROLE_SUPER_ADMIN")
* #return Categorie|View
*/
public function create(Categorie $categorie, ConstraintViolationList $violations)
{
if (count($violations)) {
return $this->view($violations, Response::HTTP_BAD_REQUEST);
}
$em = $this->getDoctrine()->getManager('main');
$em->persist($categorie);
$em->flush();
return $categorie;
}
When I use postman to insert data with this content :
{
"libelle":"Blonde",
"icone":"blonde.png",
"categorieParent.id": 1
}
"libelle" and "icone" are inserted but "categorieParent" wasn't set.
I've try :
{
"libelle":"Blonde",
"icone":"blonde.png",
"categorieParent": 1
}
{
"libelle":"Blonde",
"icone":"blonde.png",
"categorieParent": {
"id": 1
}
}
For each try, I set id with number and string.
And anything doesn't work.
Thx for help :) !
CategorieParent will only accept a Categorie entity; libelle and icone work because they are simple strings. You should use the passed integer to fetch the Entity, then save the values.
Related
I have two entities Sport and Tarif (translated by price) linked by ManyToOne relation.
I would like to have just one admin form (in Sonata Admin Bundle) to create or delete a Sport with three fields :
libelle ( = name)
valeurDeBase ( = price value) which is a number attribute of Tarif entity
conditionDeReduction ( = condition to have a discount) which is a text attribute of Tarif entity
I'm searching a way to do that and I found the use of CollectionType (https://sonata-project.org/bundles/doctrine-orm-admin/master/doc/reference/form_field_definition.html) to embed the Tarif fields in SportAdmin form but that's not working as you can see below :
Here are the entities :
Sport.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Sport
*
* #ORM\Table(name="sport")
* #ORM\Entity(repositoryClass="AppBundle\Repository\SportRepository")
*/
class Sport
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="libelle", type="string", length=255, unique=true)
*/
private $libelle;
/**
* Un Sport est lié à 1 et 1 seul Tarif
* #ORM\ManyToOne(targetEntity="Tarif")
* #ORM\JoinColumn(nullable=false)
*/
private $tarif;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set libelle
*
* #param string $libelle
* #return Sport
*/
public function setLibelle($libelle)
{
$this->libelle = $libelle;
return $this;
}
/**
* Get libelle
*
* #return string
*/
public function getLibelle()
{
return $this->libelle;
}
/**
* Constructor
*/
public function __construct()
{
$this->licences = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add licence
*
* #param \AppBundle\Entity\Licence $licence
*
* #return Sport
*/
public function addLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences[] = $licence;
return $this;
}
/**
* Remove licence
*
* #param \AppBundle\Entity\Licence $licence
*/
public function removeLicence(\AppBundle\Entity\Licence $licence)
{
$this->licences->removeElement($licence);
}
/**
* Get licences
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getLicences()
{
return $this->licences;
}
/**
* Set tarif
*
* #param \AppBundle\Entity\Tarif $tarif
*
* #return Sport
*/
public function setTarif(\AppBundle\Entity\Tarif $tarif)
{
$this->tarif = $tarif;
return $this;
}
/**
* Get tarif
*
* #return \AppBundle\Entity\Tarif
*/
public function getTarif()
{
return $this->tarif;
}
}
Tarif.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tarif
*
* #ORM\Table(name="tarif")
* #ORM\Entity(repositoryClass="AppBundle\Repository\TarifRepository")
*/
class Tarif
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="valeurDeBase", type="decimal", precision=10, scale=2)
*/
private $valeurDeBase;
/**
* #var string
*
* #ORM\Column(name="conditionReduction", type="text", nullable=true)
*/
private $conditionReduction;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set valeurDeBase
*
* #param string $valeurDeBase
* #return Tarif
*/
public function setValeurDeBase($valeurDeBase)
{
$this->valeurDeBase = $valeurDeBase;
return $this;
}
/**
* Get valeurDeBase
*
* #return string
*/
public function getValeurDeBase()
{
return $this->valeurDeBase;
}
/**
* Set conditionReduction
*
* #param string $conditionReduction
* #return Tarif
*/
public function setConditionReduction($conditionReduction)
{
$this->conditionReduction = $conditionReduction;
return $this;
}
/**
* Get conditionReduction
*
* #return string
*/
public function getConditionReduction()
{
return $this->conditionReduction;
}
}
SportAdmin.php
<?php
// src/AppBundle/Admin/SportAdmin.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 Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', CollectionType::class, array(
'by_reference' => false
),
array(
'edit' => 'inline',
'inline' => 'table',
'sortable' => 'position',
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
TarifAdmin.php
<?php
// src/AppBundle/Admin/TarifAdmin.php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
class TarifAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('valeurDeBase', 'number');
$formMapper->add('conditionReduction', 'text');
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
}
protected function configureListFields(ListMapper $listMapper)
{
}
public function toString($object)
{
return $object instanceof Tarif
? $object->getTitle()
: 'Tarif'; // shown in the breadcrumb on the create view
}
}
Thank you for your help.
Finally I have created an admin block for Tarif and embed it in SportAdmin with sonata_type_admin. It works perfectly.
Here the SportAdmin.php
<?php
// src/AppBundle/Admin/SportAdmin.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 Sonata\CoreBundle\Form\Type\CollectionType;
class SportAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('libelle', 'text');
$formMapper->add('tarif', 'sonata_type_admin', array(), array(
'admin_code' => 'admin.tarif'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('libelle');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('id');
$listMapper->add('libelle');
$listMapper->add('tarif.valeurDeBase');
$listMapper->add('tarif.conditionReduction');
}
public function toString($object)
{
return $object instanceof Sport
? $object->getTitle()
: 'Sport'; // shown in the breadcrumb on the create view
}
}
I'm trying to get INSERT values from a form into DB, one of my form fields is brand_id which is returning NULL upon submission.
There are two entities having Many to One relationship
Brand
id
name
Model
id
brand_id (FK)
name
image_url
comment
All fields return a value but the brand_id returns NULL
Code and Entitys are as follows:
brand Entity:
<?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
{
/**
* #var int
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
*#ORM\Column(type="string", length=100)
*/
private $name;
/**
*#ORM\OneToMany(targetEntity="model", mappedBy="brands")
*/
protected $models;
public function __construct()
{
$this->models = new ArrayCollection();
}
/**
* 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;
}
/**
* Add model
*
* #param \coreBundle\Entity\model $model
*
* #return brand
*/
public function addModel(\coreBundle\Entity\model $model)
{
$this->models[] = $model;
return $this;
}
/**
* Remove model
*
* #param \coreBundle\Entity\model $model
*/
public function removeModel(\coreBundle\Entity\model $model)
{
$this->models->removeElement($model);
}
/**
* Get models
*
* #return \Doctrine\Common\Collections\Collection
*/
public function getModels()
{
return $this->models;
}
}
model Entity :
<?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
{
/**
* #var int
* #ORM\Id
* #ORM\Column(name="id", type="integer")
* #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;
/**
*#ORM\ManyToOne(targetEntity="brand", inversedBy="models")
*#ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brands;
/**
* #ORM\OneToOne(targetEntity="model_item", mappedBy="models")
*/
private $model_items;
/**
* 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;
}
}
My Controller Code:
public function newModelAction(Request $request)
{
$product = $this->getDoctrine()
->getRepository('coreBundle:brand')
->findAll();
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$productId
);
}
$model = new model();
$form = $this->createFormBuilder($model)
->add('brand_id',TextType::class,array('label'=>'Brand Id'))
->add('name',TextType::class,array('label'=>'Model Name'))
->add('comment',TextType::class,array('label'=>'Comments'))
->add('image_url',TextType::class,array('label'=>'Image URL'))
->add('save',SubmitType::class, array('label'=>'Add Model'))
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($model);
$em->flush();
return $this->render('coreBundle:layouts:newItem.html.twig',
array('form'=>$form->createView(),));
}
// ... do something, like pass the $product object into a template
return $this->render('coreBundle:layouts:newModel.html.twig',
array('form'=>$form->createView(),));
}
Try to remove this:
*#ORM\Column(type="integer")
*/
public $brand_id;
Because you have already the field for brands in this point:
/**
*#ORM\ManyToOne(targetEntity="brand", inversedBy="models")
*#ORM\JoinColumn(name="brand_id", referencedColumnName="id")
*/
protected $brands;
You need also to remove getter and seeter of brandId and remove brand_id from the form and adding this for example:
->add('brand', EntityType::class, array(
// query choices from this entity
'class' => 'AppBundle:Brand',
))
Try this
/**
*#ORM\ManyToOne(targetEntity="brand")
*#ORM\JoinColumn(nullable=false)
*/
protected $brands;
I have a trouble and I need your assistance. Now I create two entities :
Partner
PartnerMedia
Partner entity contains all information about my partners and the other entity, have relationship with Partner and Sonata Media Entity.
There is what contains my partner entity :
<?php
namespace AppBundle\Entity;
use AppBundle\Entity\CMS\Block;
use AppBundle\Entity\SuperClass\SortableTranslatableEntity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Sonata\MediaBundle\Model\Media;
use Doctrine\Common\Collections\ArrayCollection;
use Sonata\TranslationBundle\Model\Gedmo\TranslatableInterface;
/**
* Partner.
*
* #ORM\Table("partner")
* #ORM\Entity
*/
class Partner extends SortableTranslatableEntity implements TranslatableInterface
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="libelle", type="string", length=60)
* #Gedmo\Translatable
*/
private $libelle;
/**
* #var string
*
* #ORM\Column(name="mea", type="boolean")
*/
private $mea;
/**
* #ORM\OneToMany(targetEntity="\AppBundle\Entity\Media\PartnerMedia", mappedBy="partner", cascade={"persist"}, orphanRemoval=true)
* #ORM\OrderBy({"position" = "ASC"})
*/
private $medias;
/**
* #ORM\Column(type="string", name="url", nullable=true)
* #Gedmo\Translatable
*/
private $url;
/**
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\CMS\Block", inversedBy="block")
*/
private $block;
public function __construct()
{
$this->medias = new ArrayCollection();
}
public function setTranslatableLocale($locale)
{
$this->locale = $locale;
}
public function getContextName()
{
return 'partner';
}
public function __toString()
{
return $this->getId() ? (string) $this->getLibelle() : '-';
}
public function firstPhoto()
{
if ($this->getMedias() && $this->getMedias()->count()) {
return $this->getMedias()->first()->getMedia();
}
}
public function getId()
{
return $this->id;
}
public function getLibelle()
{
return $this->libelle;
}
/**
* Add medias.
*
* #param PartnerMedia $media
*
* #return PartnerMedia
*/
public function addMedias(PartnerMedia $media)
{
$media->setPartner($this);
$this->medias[] = $media;
return $this;
}
/**
* Remove medias.
*
* #param PartnerMedia $media
*/
public function removeMedia(PartnerMedia $media)
{
$this->medias->removeElement($media);
}
public function getMedias()
{
return $this->medias;
}
public function getUrl()
{
return $this->url;
}
/**
* Get page.
*
* #return \AppBundle\Entity\CMS\Block
*/
public function getBlock()
{
return $this->block;
}
public function setId($id)
{
$this->id = $id;
}
public function setLibelle($libelle)
{
$this->libelle = $libelle;
}
public function setUrl($url)
{
$this->url = $url;
}
/**
* Set page.
*
* #param \AppBundle\Entity\CMS\Block $block
* #return Partner
*/
public function setBlock(Block $block = null)
{
$this->block = $block;
return $this;
}
/**
* Set mea
*
* #param boolean $mea
* #return Partner
*/
public function setMea($mea)
{
$this->mea = $mea;
return $this;
}
/**
* Get mea
*
* #return boolean
*/
public function getMea()
{
return $this->mea;
}
/**
* #param mixed $medias
*/
public function setMedias($medias)
{
$this->medias = $medias;
}
}
and what contains PartnerMedia entity :
<?php
/**
* PartnerMedia
*
* #ORM\Table()
* #ORM\Entity
*/
class PartnerMedia extends SortableEntity
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="\Application\Sonata\MediaBundle\Entity\Media")
*/
private $medias;
/**
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\Partner", inversedBy="partner")
*
* #Gedmo\SortableGroup
*/
private $partner;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set medias
*
* #param Media $medias
* #return PartnerMedia
*/
public function setMedias(Media $medias = null)
{
$this->medias = $medias;
return $this;
}
/**
* Get medias
*
* #return Media
*/
public function getMedias()
{
return $this->medias;
}
/**
* Set Partner
*
* #param Partner $partner
* #return PartnerMedia
*/
public function setPartner(Partner $partner = null)
{
$this->partner = $partner;
return $this;
}
/**
* Get Partner
*
* #return Partner
*/
public function getPartner()
{
return $this->partner;
}
public function getContextName()
{
return 'partner_media';
}
public function __construct()
{
$this->medias = new ArrayCollection();
}
}
Now when I tried to create a new partner, I receive that exception :
The current field medias is not linked to an admin. Please create one for the target entity : AppBundle\Entity\Media\PartnerMedia
I need your assistance please and thanks
The error message you describe sounds more like a Sonata Admin error.
One of the things about sonata admin is that when you are creating links between entities, you need to have admin classes built for both entity classes.
More than likely, you're trying to test your code before completely implementing the admins necessary to do so.
I'm working with FOSUserBundle and I need to build Users Profile. This is what I did:
Create the User class and extends from BaseUser as FOSUser docs said
namespace Sunahip\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToOne(targetEntity="Profile", mappedBy="user")
*/
protected $profile;
/**
* #ORM\ManyToMany(targetEntity="Sunahip\UserBundle\Entity\Group")
* #ORM\JoinTable(name="fos_user_user_group",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
}
Create a Profile entity
namespace Sunahip\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* #ORM\Entity
* #ORM\Table(name="profile")
*/
class Profile extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Id
* #ORM\OneToOne(targetEntity="User", inversedBy="profile")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* #ORM\Column(name="register_type", type="smallint", length=1)
*/
protected $register_type;
/**
* #ORM\Column(name="rif", type="string", length=25)
*/
protected $rif;
/**
* #ORM\Column(name="ci", type="string", length=25)
*/
protected $ci;
/**
* #ORM\Column(name="firstname", type="string", length=25)
*/
protected $firstname;
/**
* #ORM\Column(name="lastname", type="string", length=25)
*/
protected $lastname;
/**
* #ORM\Column(name="state", type="string", length=150)
*/
protected $state;
/**
* #ORM\Column(name="city", type="string", length=150)
*/
protected $city;
/**
* #ORM\Column(name="town", type="string", length=150)
*/
protected $town;
/**
* #ORM\Column(name="urbanization", type="string", length=150)
*/
protected $urbanization;
/**
* #ORM\Column(name="urbanization", type="string", length=150)
*/
protected $street;
/**
* #ORM\Column(name="aparment", type="string", length=150)
*/
protected $aparment;
/**
* #ORM\Column(name="aparment_no", type="string", length=150)
*/
protected $aparment_no;
/**
* #ORM\Column(name="reference", type="string", length=250)
*/
protected $reference;
/**
* #ORM\Column(name="zipcode", type="string", length=250)
*/
protected $zipcode;
/**
* #ORM\Column(name="fax", type="string", length=250)
*/
protected $fax;
/**
* #ORM\Column(name="local_phone", type="string", length=250)
*/
protected $local_phone;
/**
* #ORM\Column(name="movil_phone", type="string", length=250)
*/
protected $movil_phone;
/**
* #ORM\Column(name="alt_email", type="string", length=250)
*/
protected $alt_email;
/**
* #ORM\Column(name="alt_email", type="string", length=250)
*/
protected $website;
public function getId()
{
return $this->id;
}
public function setUser(User $user)
{
$this->user = $user;
}
public function getUser()
{
return $this->user;
}
public function setRegisterType($register_type)
{
$this->register_type = $register_type;
}
public function getRegisterType()
{
return $this->register_type;
}
public function setRif($rif)
{
$this->rif = $rif;
}
public function getRif()
{
return $this->rif;
}
public function setCI($ci)
{
$this->ci = $ci;
}
public function getCI()
{
return $this->ci;
}
public function setFirstname($firstname)
{
$this->firstname = $firstname;
}
public function getFirstname()
{
return $this->firstname;
}
public function setLastname($lastname)
{
$this->lastname = $lastname;
}
public function getLastname()
{
return $this->lastname;
}
public function setState($state)
{
$this->state = $state;
}
public function getState()
{
return $this->state;
}
public function setCity($city)
{
$this->city = $city;
}
public function getCity()
{
return $this->city;
}
public function setTown($town)
{
$this->town = $town;
}
public function getTown()
{
return $this->town;
}
public function setUrbanization($urbanization)
{
$this->urbanization = $urbanization;
}
public function getUrbanization()
{
return $this->urbanization;
}
public function setStreet($street)
{
$this->street = $street;
}
public function getStreet()
{
return $this->street;
}
public function setAparment($aparment)
{
$this->aparment = $aparment;
}
public function getAparment()
{
return $this->aparment;
}
public function setAparmentNo($aparment_no)
{
$this->aparment_no = $aparment_no;
}
public function getAparmentNo()
{
return $this->aparment_no;
}
public function setReference($reference)
{
$this->reference = $reference;
}
public function getReference()
{
return $this->reference;
}
public function setZipcode($zipcode)
{
$this->zipcode = $zipcode;
}
public function getZipcode()
{
return $this->zipcode;
}
public function setFax($fax)
{
$this->fax = $fax;
}
public function getFax()
{
return $this->fax;
}
public function setLocalPhone($local_phone)
{
$this->local_phone = $local_phone;
}
public function getLocalPhone()
{
return $this->local_phone;
}
public function setMovilPhone($movil_phone)
{
$this->movil_phone = $movil_phone;
}
public function getMovilPhone()
{
return $this->movil_phone;
}
public function setAltEmail($alt_email)
{
$this->alt_email = $alt_email;
}
public function getAltEmail()
{
return $this->alt_email;
}
public function setWebsite($website)
{
$this->website = $website;
}
public function getWebsite()
{
return $this->website;
}
}
Now, I'm trying to validate that entities by running the command doctrine:schema:validate and I get this error:
[Doctrine\ORM\Mapping\MappingException] Duplicate definition of
column 'urbanization' on entity 'Sunahip\UserBundle\Entity\Profile' in
a field or discriminator column mapping.
My questions:
I don't know what is wrong and also don't know what the error means is the first time I got this error.
I don't know if I'm building users profiles in the right way I mean if I should extends from BaseUser or from User
Can I give some help here? Advices? Ideas?
You have (had) basically two probles here:
Duplicated urbanization column name somewhere there which needs to be removed. Only one column with the same name is allowed
Duplicated #ORM\Id annotation in your Profile entity. Remove one from $user because it is not your Id
I have a rather strange problem. I'm using Doctrine 2 under Zend Framework 1.11. I have a database called "Sessions", which are training sessions for students. Each session has an associated note, called a SOAPE note. Edit: I am now including both of the entities in question.
Sessions:
use Doctrine\ORM\Mapping as ORM;
/**
* #Entity(repositoryClass="Repositories\Sessions")
* #Table(name="Sessions")
*/
class Sessions
{
/**
* #var integer Id
*
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer $schId
*
* #Column(name="schId", type="integer", nullable=false)
*/
protected $schId;
/**
* #var integer $stdId
*
* #Column(name="stdId", type="integer", nullable=false)
*/
protected $stdId;
/**
* #var integer $trainerPsnId
*
* #Column(name="trainerPsnId", type="integer", nullable=false)
*/
protected $trainerPsnId;
/**
* #var boolean $isLegacy
*
* #Column(name="isLegacy", type="boolean", nullable=false)
*/
protected $isLegacy;
/**
* #var float $charge
*
* #Column(name="charge", type="float", nullable=false)
*/
protected $charge;
/**
* #var float $trainerPay
*
* #Column(name="trainerPay", type="float", nullable=false)
*/
protected $trainerPay;
/**
* #var integer $modeId
*
* #Column(name="modeId", type="integer", nullable=false)
*/
protected $modeId;
/**
* #var text $notes
*
* #Column(name="notes", type="text", nullable=true)
*/
protected $notes;
/**
* #var string $twitterNote
*
* #Column(name="twitterNote", type="string", length=20, nullable=true)
*/
protected $twitterNote;
// ASSOCIATIONS
/**
* #OneToOne(targetEntity="Schedule", inversedBy="session")
* #JoinColumn(name="schId", referencedColumnName="id")
*/
protected $schedule;
/**
* #OneToOne(targetEntity="SnSoapeNotes", mappedBy="session")
* #JoinColumn(name="id", referencedColumnName="snId")
*/
protected $soapeNote;
/**
* #ManyToOne(targetEntity="Students")
* #JoinColumn(name="stdId", referencedColumnName="id")
*/
protected $student;
/**
* #ManyToOne(targetEntity="Personnel", inversedBy="sessions")
* #JoinColumn(name="trainerPsnId", referencedColumnName="id")
*/
protected $trainer;
// Getters and Setters
public function getId()
{
return $this->id;
}
public function getSchId()
{
return $this->schId;
}
public function setSchId($schId)
{
$this->schId = $schId;
}
public function getStdId()
{
return $this->stdId;
}
public function setStdId($stdId)
{
$this->stdId = $stdId;
}
public function getTrainerPsnId()
{
return $this->trainerPsnId;
}
public function setTrainerPsnId($trainerPsnId)
{
$this->stdId = $trainerPsnId;
}
public function getIsLegacy()
{
return $this->isLegacy;
}
public function setIsLegacy($isLegacy)
{
$this->isLegacy = $isLegacy;
}
public function getCharge()
{
return $this->charge;
}
public function setCharge($charge)
{
$this->charge = $charge;
}
public function getTrainerPay()
{
return $this->trainerPay;
}
public function setTrainerPay($trainerPay)
{
$this->trainerPay = $trainerPay;
}
public function getModeId()
{
return $this->modeId;
}
public function setModeId($modeId)
{
$this->modeId = $modeId;
}
public function getNotes()
{
return $this->notes;
}
public function setNotes($notes)
{
$this->notes = $notes;
}
public function getTwitterNote()
{
return $this->twitterNote;
}
public function setTwitterNote($twitterNote)
{
$this->twitterNote = $twitterNote;
}
// Foreign Data
public function getSchedule()
{
return $this->schedule;
}
public function getStudent()
{
return $this->student;
}
public function getTrainer()
{
return $this->trainer;
}
public function getSoapeNote()
{
return $this->soapeNote;
}
}
SnSoapeNotes:
namespace Entities;
use Doctrine\Mapping as ORM;
/**
* SnSoapeNotes
*
* #Table(name="SnSoapeNotes")
* #Entity(repositoryClass="Repositories\SnSoapeNotes")
*/
class SnSoapeNotes
{
/**
* #var integer Id
*
* #Id #Column(type="integer")
* #GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var integer $mental
*
* #Column(name="mental", type="integer", nullable=false)
*/
private $mental;
/**
* #var integer $physical
*
* #Column(name="physical", type="integer", nullable=false)
*/
private $physical;
/**
* #var text $subjective
*
* #Column(name="subjective", type="text", nullable=false)
*/
private $subjective;
/**
* #var text $objective
*
* #Column(name="objective", type="text", nullable=false)
*/
private $objective;
/**
* #var text $plan
*
* #Column(name="plan", type="text", nullable=false)
*/
private $plan;
/**
* #var text $action
*
* #Column(name="action", type="text", nullable=false)
*/
private $action;
/**
* #var text $education
*
* #Column(name="education", type="text", nullable=false)
*/
private $education;
/**
* #var text $warning
*
* #Column(name="warning", type="text", nullable=true)
*/
private $warning;
/**
* #var text $incident
*
* #Column(name="incident", type="text", nullable=true)
*/
private $incident;
/**
* #var text $technical
*
* #Column(name="technical", type="text", nullable=true)
*/
private $technical;
// ASSOCIATIONS
/**
* #Var Sessions $sessions
*
* #Column(name="snId", type="integer", nullable=false)
* #OneToOne(targetEntity="Sessions", inversedBy="soapeNote")
* #JoinColumn(name="snId", referencedColumnName="id")
*/
protected $sessions;
// Getters and Setters
public function getSnId()
{
return $this->snId;
}
public function setSnId($snId)
{
$this->snId = $snId;
}
public function getMental()
{
return $this->mental;
}
public function setMental($mental)
{
$this->mental = $mental;
}
public function getPhysical()
{
return $this->physical;
}
public function setPhysical($physical)
{
$this->physical = $physical;
}
public function getSubjective()
{
return $this->subjective;
}
public function setSubjective($subjective)
{
$this->subjective = $subjective;
}
public function getObjective()
{
return $this->objective;
}
public function setObjective($objective)
{
$this->objective = $objective;
}
public function getPlan()
{
return $this->plan;
}
public function setPlan($plan)
{
$this->plan = $plan;
}
public function getAction()
{
return $this->action;
}
public function setAction($action)
{
$this->action = $action;
}
public function getEducation()
{
return $this->education;
}
public function setEducation($education)
{
$this->education = $education;
}
public function getWarning()
{
return $this->warning;
}
public function setWarning($warning)
{
$this->warning = $warning;
}
public function getIncident()
{
return $this->incident;
}
public function setIncident($incident)
{
$this->incident = $incident;
}
public function getTechnical()
{
return $this->technical;
}
public function setTechnical($technical)
{
$this->technical = $technical;
}
public function getSession()
{
return $this->session;
}
// A quick way to make sure the soape note has been completed.
// Note that objective is left out here because it can be
// filled out beforehand
public function getIsComplete()
{
return !empty($this->subjective)
&& !empty($this->action)
&& !empty($this->plan)
&& !empty($this->education);
}
}
When calling $em->getRepository('Entities\Sessions')->findOneBy('id'), everything works fine--I get the session and its accompanying SOAPE note. Ditto for other associated tables' data.
But now I am trying to write a custom repository to get the notes prior to this session. The function is as follows:
<?php
namespace Repositories;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\DBAL\Types\Type;
/**
* Sessions
*/
class Sessions extends EntityRepository
{
public function getPastSoapeNotes($params) {
$studentId = $params['studentId'];
$latestSession = $params['snDatetime'] ?: date('Y-m-d H:i');
$qb = $this->_em->createQueryBuilder();
$qb->select('n.subjective, n.objective, n.plan, n.action, n.education')
->from('Entities\Sessions', 'sn')
->innerJoin('sn.Entities\SnSoapeNotes', 'n');
return $qb->getQuery()->getResult();
}
}
When I call this, I get the following error:
[Semantical Error] line 0, col 126 near 'n': Error: Class Entities\Sessions has no association named Entities\SnSoapeNotes
I have also tried using the "mappedBy" and "inersedBy" annotations in every possible combination, but to no avail; Doctrine can't seem to find the association. I am at a complete loss as to what is going on.
I figured out what I did wrong. In the join statement, I used 'sn.Entities\SnSoapeNotes' when I should have used just 'soapeNote', which is the property in the Sessions class, not the table name itself.