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")
*/
Related
I am configuring a series of ApiFilter classes from the Api-platform stack in a Symfony 4 application. I am finding that any filters using a nested property from an entity relationship takes no effect. Please see my example code below, would really appreciate any assistance with why it may not be working.
The entity I am trying to filter on is:
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiProperty;
/**
* PostAuthor
* #ApiResource()
* #ORM\Table(name="POST_AUTHOR", indexes={#ORM\Index(name="IDX_CF3397D94A7000A0", columns={"IDENTITY_ID"})})
* #ORM\Entity(repositoryClass="App\Repository\PostAuthorRepository")
*/
class PostAuthor
{
/**
* #var string
*
* #ORM\Column(name="USER_ID", type="string", length=255, nullable=false)
* #ORM\Id
*/
private $userId;
/**
* #var string
*
* #ORM\Column(name="USERNAME", type="string", length=255, nullable=false)
*/
private $username;
And the collection I am filtering is:
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiFilter;
use Symfony\Component\Serializer\Annotation\Groups;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\NumericFilter;
use Doctrine\ORM\Mapping as ORM;
/**
* PostHeader
* #ORM\Table(name="POST_HEADER", indexes={#ORM\Index(name="IDX_1CEEE7D0B65E5FF8", columns={"EMOTION_REF_ID"}), #ORM\Index(name="IDX_1CEEE7D08AFAAB14", columns={"AUTHOR_ID"})})
* #ORM\Entity(repositoryClass="App\Repository\PostHeaderRepository")
* #ApiResource(
* normalizationContext={"groups"={"postheader:read"}},
* denormalizationContext={"groups"={"postheader:write"}},
* )
* #ApiFilter(SearchFilter::class, properties={"author.userId": "partial", "author.username": "partial"})
*/
class PostHeader
{
/**
* #var PostAuthor
*
* #ORM\ManyToOne(targetEntity="PostAuthor")
* #ORM\JoinColumns({#ORM\JoinColumn(name="AUTHOR_ID", referencedColumnName="USER_ID")})
* #Groups({"postheader:read"})
*/
private $author;
I just test on a new project with the same classes than you and it works. I can filter by author.userId.
Please give us your testing request. And the error message ;).
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 two entities relating to this issue, and I want a join table to be able to cross reference values in each table.
Here is an explanation:
Entity ContainerType.php:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\Containers;
/**
* ContainerType
*
* #ORM\Table(name="container_type")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
*/
class ContainerType
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* #var \DateTime
*
* #ORM\Column(name="date_added", type="datetime")
*/
private $dateAdded;
/**
* #var \DateTime
*
* #ORM\Column(name="date_modified", type="datetime", nullable=true)
*/
private $dateModified;
/**
* #ORM\ManyToMany(targetEntity="Containers", inversedBy="type")
* #ORM\JoinTable(name="container_type_containers")
**/
private $container;
public function __construct()
{
$this->container = new ArrayCollection();
}
And entity Containers.php:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Entity\ContainerType;
/**
* Containers
*
* #ORM\Table(name="containers")
* #ORM\Entity
*/
class Containers
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="number", type="string", length=255)
*/
private $number;
/**
* #ORM\ManyToMany(targetEntity="Containers", mappedBy="container")
*/
private $type;
public function __construct()
{
$this->type = new ArrayCollection();
}
And although the schema update works without problems, when I do a doctrine:schema:validate I get the following fail:
[Mapping] FAIL - The entity-class 'AppBundle\Entity\Containers' mapping is invalid:
* The association AppBundle\Entity\Containers#type refers to the owning side field AppBundle\Entity\Containers#container which does not exist.
But the $container field DOES exist in ContainerType so I do not understand why it's trying to reference a field called container in the Containers entity?
Can anyone shed any light on this?
Thank you
Michael
I think this code should works fine for you :)
ContainerType.php
/**
* #ORM\ManyToMany(targetEntity="Containers", inversedBy="containersType")
* #ORM\JoinTable(name="container_type_containers",
* joinColumns={#ORM\JoinColumn(name="container_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="container_type_id", referencedColumnName="id")})
*/
protected $containers;
Containers.php
/**
* #ORM\ManyToMany(targetEntity="ContainerType", mappedBy="containers")
*/
protected $containersType;
I have just realized my error!
I had used the wrong target Entity in the Containers.php file, I should have used ContainerType as the target, instead I had Containers which is why it was trying to find the field in the wrong table!
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")
I have an error when I am trying to run doctrine:generate:entities:
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] Couldn't find constant ?, property Smartnode\TalkBundle\Entity\Post::$postowner.
This is my post entity class:
namespace Smartnode\TalkBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Post
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Smartnode\TalkBundle\Entity\PostRepository")
*/
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\ManyToOne(targetEntity=“Smartnode\userBundle\Entity\User“)
* #JoinColumn(name="postowner_id", referencedColumnName="id")
*/
protected $postowner;
/**
* #var integer
*
* #ORM\ManyTonOne(targetEntity=“Smartnode\TalkBundle\Entity\Chan“)
*/
private $postchan;
/**
* #var \DateTime
*
* #ORM\Column(name="creationdate", type="datetime")
*/
private $creationdate;
And this is my User entity class:
namespace Smartnode\userBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="Smartnode\userBundle\Entity\UserRepository")
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
The probleme Was the quote :/
with the good quote all is working
Thanks All for help
The answer of the question creator is correct. Anyways it is unclear, what he means by the problem was the quote.
Also ' should be used instead of a " to quote the values of the annotation attributes.
The issue for me was, that a quote was missing.
Wrong code:
/**
* #ORM\OneToMany(targetEntity="UnternehmenBrancheZuordnung", mappedBy=_unternehmen")
* #var ArrayCollection $_branchenZuordnungen
*/
private $_branchenZuordnungen;
Right code:
/**
* #ORM\OneToMany(targetEntity="UnternehmenBrancheZuordnung", mappedBy="_unternehmen")
* #var ArrayCollection $_branchenZuordnungen
*/
private $_branchenZuordnungen;
Here the missing quote is highlighted: mappedBy="_unternehmen"