I have a ManyToMany relation with a composite key on the reverse side.
When I use the console command doctrine:schema:update I have the following error:
[Doctrine\ORM\ORMException]
Column name `keyword` referenced for relation from Map\MapBundle\Entity\
Student towards Map\MapBundle\Entity\SkillType does not exist.
I have an entity student (unique key) with a ManyToMany relation with an entity skill (composite key) which has a ManyToOne relation with skillType (unique key).
Here is the different class mapping I have:
Class Student
<?php
namespace Map\MapBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Student
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Map\MapBundle\Entity\StudentRepository")
*/
class Student {
/**
*
* #var integer #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="Map\MapBundle\Entity\SkillType")
* #ORM\JoinTable(name="students_skills",
* joinColumns={
* #ORM\JoinColumn(name="keyword", referencedColumnName="keyword"),
* #ORM\JoinColumn(name="attribut", referencedColumnName="attribut")
* })
*/
private $skills;
}
Class skill
<?php
namespace Map\MapBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Skill
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Map\MapBundle\Entity\SkillRepository")
*/
class Skill {
/**
* #ORM\ManyToOne(targetEntity="Map\MapBundle\Entity\skillType")
* #ORM\JoinColumn(name="keyword", referencedColumnName="keyword")
* #ORM\Id
*/
private $keyword;
}
Classe skillType
<?php
namespace Map\MapBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* SkillType
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Map\MapBundle\Entity\SkillTypeRepository")
*/
class SkillType {
/**
* #var string
*
* #ORM\Column(name="keyword", type="string", length=255)
* #ORM\Id
*/
private $keyword;
}
I tried to exchange the keyword and attribut #joinColumn lines, but I have the same error message with attribut instead of keyword.
I can't see what's wrong with my mapping. The table skill exists and has columns named keyword and attribut.
I hope that somebody will see where I made a mistake (probably a typo error like a missing character or a case mistake).
Thank you for your answer. It helped me a lot and i succeded doing the schema update.
Here is the code I finaly used
/**
* #ORM\ManyToMany(targetEntity="Carte\CarteBundle\Entity\Skill")
* #ORM\JoinTable(name="students_skills",
* joinColumns={#ORM\JoinColumn(name="student_id", referencedColumnName="id")},
* inverseJoinColumns={
* #ORM\JoinColumn(name="keyword", referencedColumnName="keyword"),
* #ORM\JoinColumn(name="attribut", referencedColumnName="attribut")
* })
*/
private $skills;
You write that you want Student to have the many-to-many relation with Skill, but you connected it with SkillType instead. And you're missing the inverseJoinColumns property and you didn't referenced Student properly.
Try the following annotation (untested and after looking at the documentation):
/**
* #ORM\ManyToMany(targetEntity="Map\MapBundle\Entity\Skill")
* #ORM\JoinTable(name="students_skills",
* joinColumns={#ORM\JoinColumn(name="student_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="skill_keyword", referencedColumnName="keyword")}
* )
*/
private $skills;
Related
I am getting error of Case mismatch between loaded and declared class names: "Doctrine\ORM\Mapping\ManytoOne" vs "Doctrine\ORM\Mapping\ManyToOne" with User Entity only. Same Code with other entities is working fine.
Here is my Comp Entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Comp
*
* #ORM\Table(name="comp")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
*/
class Comp
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
* #ORM\ManytoOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id",referencedColumnName="id", nullable=true)
*/
private $user;
My User Entity is
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
and I am getting error of
Case mismatch between loaded and declared class names: "Doctrine\ORM\Mapping\ManytoOne" vs "Doctrine\ORM\Mapping\ManyToOne"
while updating schema
The same code is working file with other entities. I manage to make foreign keys. Cannot understand what is going wrong with User field only.
try to change this:
* #ORM\ManytoOne(targetEntity="User")
to this:
* #ORM\ManyToOne(targetEntity="User")
Annotations are case sensitive for this reason you retrieve an error
I have an entity called Note , for every Note ther's etudiant_id and evaluation_id .
I want that the combination of etudiant_id and evaluation_id should be unique .
For exmple when i give him a Note object1(etudiant_id=1,evaluation_id=1) and Note2 object2(etudiant_id=1,evaluation_id=1) : this should throw an exception. but
in case of :
Note object1(etudiant_id=1,evaluation_id=2) and Note2 object2(etudiant_id=1,evaluation_id=1) : this will not throw anything .
By the way i tried :* #UniqueEntity(fields={"evaluation","etudiant"}) but this didnt work correcte.
Here's my Note Entity :
<?php
namespace EcoleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Note
* #ORM\Entity
* #UniqueEntity(fields={"evaluation","etudiant"})
* #ORM\Table(name="note")
* #ORM\Entity(repositoryClass="EcoleBundle\Repository\NoteRepository")
*/
class Note
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var float
*
* #ORM\Column(name="valeur", type="float", nullable=true)
*/
private $valeur;
/**
* #ORM\ManyToOne(targetEntity="EcoleBundle\Entity\Evaluation", inversedBy="notes")
*/
private $evaluation;
/**
* #ORM\ManyToOne(targetEntity="EcoleBundle\Entity\Etudiant", inversedBy="notes")
*/
private $etudiant;
You should have only one #Entity annotation. Try to substitute with this and clean the cache
/**
* Note
* #ORM\Entity(repositoryClass="EcoleBundle\Repository\NoteRepository")
* #UniqueEntity(fields={"evaluation","etudiant"})
* #ORM\Table(name="note")
*/
I have a "Company" entity with three fields : id, name and slug. The name is unique.
When I attempt to create a new Company with a name already used I get the following error :
An exception occurred while executing 'INSERT INTO companies (name, slug) VALUES (?, ?)' with params ["test", "test-1"]:
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'test' for key 'UNIQ_8244AA3A5E237E06'
I don't know why, but the #UniqueEntity does not show my error message. I also tried to put this line : #UniqueEntity(fields={"name"}, groups={"company"})
Here is the code of the class :
<?php
namespace Project\UserBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Company
*
* #ORM\Table(name="companies")
* #UniqueEntity(fields={"name"}, message="form.error.name.unique")
* #ORM\Entity
*/
class Company
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=50, unique=true)
* #Assert\NotBlank(message="form.error.name.not_blank");
* #Assert\Length(min="4", max="50", minMessage="form.error.name.length.min {{ limit }}", maxMessage="form.error.name.length.max {{ limit }}");
*/
private $name;
/**
* #var string
*
* #ORM\Column(length=170, unique=true)
* #Gedmo\Slug(fields={"name"})
*/
private $slug;
/**
* #ORM\OneToMany(targetEntity="Project\UserBundle\Entity\User", mappedBy="company")
* #Assert\Valid
*/
private $users;
}
Could you test this 3 possibilities and try to get the default message? (But it seems that you already tried the third one. Maybe luck with the others)
(Btw, you can not set a groups option in the UniqueEntity annotation)
/**
* User
*
* #UniqueEntity("name")
* #UniqueEntity(fields="name")
* #UniqueEntity(fields={"name"})
*/
class User
{
/**
* #var string
*
* #ORM\Column(name="name", type="string", unique=true)
*
* #Assert\NotBlank
*/
private $name;
}
This syntax works for me:
#UniqueEntity("name", message="form.error.name.unique")
My problem is quite simple but i search for an hour without succeed, i would like to add some constraint to the UserEntity. For example, i would like to limit the length of a username
I think the best way is to not touche FOS in Vendor. I have create my own userBundle with my own layout.html etc ... But i cannot override attribut who is already existing in FosuserBundle (it's working for the layout overriding btw my userBundle is a child of FOS)
the funny thing is "id" has no problem for the overriding
My User entity :
<?php
namespace Diane\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Diane\UserBundle\Entity\UserRepository")
*/
class User extends BaseUser
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
* #ORM\Column(name="username", type="string", length=10)
*/
protected $username;
}
Fos User model :
<?php
namespace FOS\UserBundle\Model;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
abstract class User implements UserInterface, GroupableInterface
{
protected $id;
/**
* #var string
*/
protected $username;
...
}
i have already try to remove :
/**
* #var string
*/
Error message :
Doctrine\ORM\Mapping\MappingException] Duplicate definition of column
'username' on entity 'Diane\UserBundle\Entity\User' in a field or
discriminator column mapping.
Thanks for any idea you could give me
Try AttributeOverrides Doctrine annotation:
/**
* User
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Diane\UserBundle\Entity\UserRepository") / class User extends
*
* #ORM\AttributeOverrides({
* #ORM\AttributeOverride(name="username",
* column=#ORM\Column(
* name = "username",
* type = "string",
* length = 10
* )
* )
* })
*
*/
class User extends BaseUser
{
I'm new to Symfony2 + Doctrine and I´m looking for a way to validate the uniqueness in an Arraycollection. May be it is already answered question but I can´t figure how resolve it.
I`ve a Relevamientosserviciosprestador class with a Callback:
namespace Prestadores\PrincipalBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\ExecutionContext;
/**
* Prestadores\PrincipalBundle\Entity\Relevamientosserviciosprestador
*
* #ORM\Table(name="relevServiciosPrestador")
* #ORM\Entity(repositoryClass="Prestadores\PrincipalBundle\Repository\RelevamientosserviciosprestadorRepository")*
* #Assert\Callback(methods={"sonUnicosLosTiposDeReclamoq"})
*/
class Relevamientosserviciosprestador
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
....
....
/**
* #ORM\OneToMany(targetEntity="Atencionusuarioreclamo", mappedBy="relevamiento_id", cascade={"persist"})
* #Assert\Valid
*/
private $reclamos;
....
....
public function __construct()
{
$this->personal = new ArrayCollection();
$this->reclamos = new ArrayCollection();
}
....
....
/*Acá intentaremos validar si los tipos de reclamo que se están cargando son únicos para ese relevamiento*/
public function sonUnicosLosTiposDeReclamoq(ExecutionContext $context)
{
foreach ($this->reclamos as $reclamo){
/*Here, I get all entities, not only those related to a Relevamientosserviciosprestador*/
var_dump($reclamo->gettiporeclamo()->getnombre());
}
}
}
And the Atencionusuarioreclamo entity:
namespace Prestadores\PrincipalBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Prestadores\PrincipalBundle\Entity\Atencionusuarioreclamo
*
* #ORM\Table(name="atencionUsuarioReclamo")
* #ORM\Entity
*/
class Atencionusuarioreclamo
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var Atencionusuariosede
*
* #ORM\ManyToOne(targetEntity="Atencionusuariosede")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="nroSede", referencedColumnName="id")
* })
*/
private $nrosede;
/**
* #var relevamiento_id
*
* #ORM\ManyToOne(targetEntity="Relevamientosserviciosprestador")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="relevamiento_id", referencedColumnName="id")
* })
*/
private $relevamiento_id;
/**
* #var Prmreclamotipo
*
* #ORM\ManyToOne(targetEntity="Prmreclamotipo")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="tipoReclamo", referencedColumnName="id")
* })
* #Assert\NotBlank()
*/
private $tiporeclamo;
....
....
....
....
}
I want to chek uniqueness of tiporeclamo in a given sede and relevamiento_id
I create or edit a Relevamientosserviciosprestador using a Form which has a sub form collection for "Atencionusuarioreclamo" entities. On submit, the callback for Relevamientosserviciosprestador executes but $this->reclamos has all saved entities not only those related to the Relevamientosserviciosprestador what I´m editing.
Is this the expected behauvoir or I`m missing something?
I´ve also tested the approach mentioned in How to validate unique entities in an entity collection in symfony2
but, again, it checks all entities.
I´ve also read Doctrine2 ArrayCollection but I cant´t understand if it resolve the problem.
Please, can you tell me how do you manage uniqueness in your ArrayCollection before persist it?
I´m sorry for my poor english
Thanks in advance
Ivan
php's array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
combined with ArrayCollection->toArray() and ArrayCollection->__construct()
In your case:
$Atencionusuarioreclamo =
new ArrayCollection(array_unique($Atencionusuarioreclamo->toArray());