Check if a user already made a submission with Doctrine- Symfony 3 - php

I have 3 entities which are User, Problem and Submission. I'm trying to restrict a user from solving the same question more than once if that user already solved it. I created a method in User entity by checking the Submissions for each user. However I couldn't figure out a way to match the problem_id and user_id fields so I can see if the submission was correct or not.
Here are the entites;
Problem.php
class Problem
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, unique=True)
* #Assert\NotBlank(message="Please enter a valid title")
*/
protected $title;
/**
* #ORM\Column(type="text")
* #Assert\NotBlank(message="Please enter a valid description")
*/
protected $description;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank(message="Please enter a valid value")
*/
protected $points;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank(message="Please enter a valid flag")
*/
protected $flag;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank(message="Please enter a valid value")
*/
protected $category;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="problems")
* #ORM\JoinColumn(name="createdby", referencedColumnName="id")
*/
protected $createdby;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Submission", mappedBy="problem_id")
*/
protected $submissions;
/**
* #Gedmo\Slug(fields={"title"})
* #ORM\Column(type="string", length=255, unique=false,)
*/
protected $slug;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Discussion", mappedBy="problem")
*/
private $discussions;
/**
* #ORM\Column(type="boolean")
*/
protected $isPublished = True;
}
Submission.php
class Submission
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Problem", inversedBy="submissions")
* #ORM\JoinColumn(name="problem_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $problem_id;
/**
* #ORM\Column(type="boolean")
*/
protected $correct = false;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\User", inversedBy="submissions")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user_id;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank(message="Flag cannot be blank")
*/
protected $submission_flag;
/**
* #ORM\Column(type="datetime")
*/
private $createdAt;
/**
* #ORM\Column(type="datetime")
*/
private $updatedAt;
}
User.php
class User implements UserInterface
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, unique=true)
* #Assert\NotBlank(message="Please enter a valid email address")
* #Assert\Email()
*/
private $username;
/**
* #ORM\Column(type="string", length=255, unique=true)
* #Assert\NotBlank(message="Please enter a valid email address")
*/
private $usrname;
/**
* #Assert\NotBlank()
* #Assert\Length(max=4096)
*/
private $plainPassword;
/**
* The below length depends on the "algorithm" you use for encoding
* the password, but this works well with bcrypt.
*
* #ORM\Column(type="string", length=64)
*/
private $password;
/**
* #ORM\Column(type="string", length=255, unique=true)
* #Assert\NotBlank(message="Please enter a valid name")
*/
private $fullname;
/**
* #var array
* #ORM\Column(name="roles", type="json_array")
*/
protected $roles;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Problem", mappedBy="createdby")
*/
protected $problems;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Feed", mappedBy="createdby")
*/
protected $feeds;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Comment", mappedBy="createdby")
*/
protected $comments;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Submission", mappedBy="user_id")
*/
protected $submissions;
}

You may be able to try #UniqueConstraint on your Submission entity. Something like:
/**
* #Entity
* #Table(uniqueConstraints={#UniqueConstraint(name="unique_user_submission", columns={"name", "email"})})
*/
class Submission
{
}
I think that'll make those two fields a composite unique field.
http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/annotations-reference.html#annref-uniqueconstraint

Related

Doctrine Associations in symfony

so I have been working with Symfony for a while but there is one thing that bothers.
It's about Doctrine Associations.
The thing is that I am trying to achieve a user friend invites and relations and there is a page that the user can see the invitations he sent and the ones that are pending.
EDIT: I made it happen using Many-To-One/One-To-Many associations. However
My question is - Are Doctrine Associations the correct way of doing that.
My User Entity
class User implements UserInterface
{
private $id;
/**
* #ORM\Column(name="first_name", type="string", length=30)
*
* #Assert\NotBlank(message="First name cannot be a blank field", groups={"register"})
* #Assert\Length(min="3", max="30", groups={"register"})
*/
/**
* #ORM\Column(type="string", length=50)
*
* #Assert\NotBlank(message="Username cannot be a blank field", groups={"register"})
* #Assert\Length(min="7", max="50", groups={"register"})
*/
private $username;
/**
* #ORM\Column(type="string", length=255)
*
* #Assert\Length(min="7", max="50", groups={"register"})
*/
private $password;
/**
* #ORM\OneToMany(targetEntity="App\Entity\UserInvitation", mappedBy="inviterId", orphanRemoval=true)
*/
private $userInvitations;
/**
* #ORM\OneToMany(targetEntity="App\Entity\UserInvitation", mappedBy="invitedId", orphanRemoval=true)
*/
private $pendingUserInvitations;
//getters and setters
My UserInvitation Entity
class UserInvitation
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userInvitations")
* #ORM\JoinColumn(name="inviter_id", nullable=false)
*/
private $inviterId;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="pendingUserInvitations")
* #ORM\JoinColumn(name="invited_id", nullable=false)
*/
private $invitedId;
/**
* #ORM\Column(type="boolean")
*/
private $status;
This is my database.
The relationships is the right way to do it although on the entity I would use the following:
class UserInvitation
{
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="userInvitations")
* #ORM\JoinColumn(name="inviter_id", nullable=false)
*/
private $inviter;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="pendingUserInvitations")
* #ORM\JoinColumn(name="invited_id", nullable=false)
*/
private $invitee;
/**
* #ORM\Column(type="boolean")
*/
private $status;
Then you would getInviter() or setInviter(). Basically think that you are saving the related object to the entity and not the related field

Doctrine Collection Entities not fully populated

I made a mapping of 4 entities, but when I view the objects in the collection , all objects are populated except the last.
Here Entities
User.php
/**
* User
*
* #ORM\Table(name="`user`")
* #ORM\Entity(repositoryClass="icgo\AdminBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255,nullable=false)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="surname", type="string", length=255,nullable=false)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $surname;
/**
*
* #ORM\OneToMany(targetEntity="Affectation",mappedBy="user",cascade={"persist"})
* #ORM\JoinColumn(nullable=false)
*/
private $affectations;
public function __construct()
{
parent::__construct();
$this->affectations = new \Doctrine\Common\Collections\ArrayCollection();
}
...... getters and setters ....
Affectation.php
/**
* Affectation
*
* #ORM\Table(name="affectation")
* #ORM\Entity(repositoryClass="icgo\AdminBundle\Repository\AffectationRepository")
*/
class Affectation
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="User",inversedBy="affectations",cascade={"persist"})
* #ORM\JoinColumn(name="uid",referencedColumnName="id")
*/
private $user;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="icgo\WorkPlaceBundle\Entity\Workplace",inversedBy="affectations",cascade={"persist"})
* #ORM\JoinColumn(name="wid",referencedColumnName="id")
*/
private $workplace;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Role",inversedBy="affectations",cascade={"persist"})
* #ORM\JoinColumn(name="rid",referencedColumnName="rid")
*/
private $role;
.... getters and setters .....
Workplace.php
/**
* workplace
*
* #ORM\Table(name="workplace")
* #ORM\Entity(repositoryClass="icgo\WorkPlaceBundle\Repository\WorkplaceRepository")
*/
class Workplace
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="wfa", type="string", length=1, nullable=false)
*/
private $famille;
/**
* #var string
*
* #ORM\Column(name="nickname", type="string", length=255)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $nickname;
/**
* #var \DateTime
*
* #ORM\Column(name="date_start", type="date")
* #Assert\Date()
*/
private $dateStart;
/**
* #var \DateTime
*
* #ORM\Column(name="date_end", type="date")
* #Assert\Date()
*/
private $dateEnd;
/**
* #var string
*
* #ORM\Column(name="label_short", type="string", length=255)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $labelShort;
/**
* #var string
*
* #ORM\Column(name="label_long", type="text")
*/
private $labelLong;
/**
*
* #ORM\OneToMany(targetEntity="icgo\AdminBundle\Entity\Affectation",mappedBy="workplace")
* #ORM\JoinColumn(nullable=false)
*/
private $affectations;
.... getters and setters .....
Role.php
/**
* Role
*
* #ORM\Table(name="role")
* #ORM\Entity(repositoryClass="icgo\AdminBundle\Repository\RoleRepository")
*/
class Role
{
/**
* #var int
*
* #ORM\Column(name="rid", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="label_short", type="string", length=255)
* #Assert\NotBlank()
* #Assert\NotNull()
*/
private $labelShort;
/**
* #var string
*
* #ORM\Column(name="label_long", type="text", nullable=false)
*/
private $labelLong;
/**
*
* #ORM\OneToMany(targetEntity="Affectation",mappedBy="role")
* #ORM\JoinColumn(nullable=false)
*/
private $affectations;
.... getters and setters ......
UserRepository.php
class UserRepository extends \Doctrine\ORM\EntityRepository
{
public function getUsers(){
$query = $this->createQueryBuilder('u')
->select('u','a','r')
->addSelect('w')
->leftJoin('u.affectations','a')
->leftJoin('a.workplace','w')
->leftJoin('a.role','r')
->getQuery();
return $query->getResult();
}
}
Result dump
Dump image
As see, for the last object User , the table assignments, workplace and role objects are null. All the previous user are populated
Thanks for help

How to right insert or update on same Doctrine2 object

I have this piece of code:
$entity = $em->getRepository('AppBundle:Representative')->find($soqlObj1['records'][0]['Id']);
if ($entity === null) {
$entity = new Representative();
$em->persist($entity);
}
// we set the values from veeva
$entity->setVeevaRepId($soqlObj1['records'][0]['Id']);
$entity->setEmail($soqlObj1['records'][0]['Email']);
...
$em->flush();
And this is part of the entity Representative:
class Representative
{
/**
* #ORM\Id()
* #ORM\Column(type="string", length=45, nullable=false, unique=true)
* #ORM\GeneratedValue()
* #Expose()
*/
protected $veeva_rep_id;
/**
* #var string
* #ORM\Column(type="string", length=45)
* #Expose()
*/
protected $display_name;
/**
* #var string
* #ORM\Column(type="string", length=255)
* #Expose()
*/
protected $avatar_url = 'https://pdone.s3.amazonaws.com/avatar/default_avatar.png';
/**
* #var string
* #ORM\Column(type="string", length=45)
* #Expose()
*/
protected $rep_type = "VEEVA";
/**
* #var string
* #ORM\Column(type="string", length=45)
* #Expose()
*/
protected $username;
/**
* #var string
* #ORM\Column(type="string", length=45)
* #Expose()
*/
protected $first;
/**
* #var string
* #ORM\Column(type="string", length=45)
* #Expose()
*/
protected $last;
/**
* #var string
* #ORM\Column(type="string", length=45, nullable=true)
* #Expose()
*/
protected $title;
/**
* #var string
* #ORM\Column(type="text", nullable=true)
*/
protected $bio;
/**
* #var string
* #ORM\Column(type="string", length=45, nullable=true)
* #Expose()
*/
protected $phone;
/**
* #var string
* #ORM\Column(type="string", length=45)
* #Expose()
*/
protected $email;
/**
* #var bool
* #ORM\Column(type="boolean")
* #Expose()
*/
protected $inactive = false;
/**
* #var \DateTime
* #ORM\Column(type="datetime", nullable=true)
* #Expose()
*/
protected $lastLoginAt;
/**
* #var \DateTime
* #ORM\Column(type="datetime")
* #Expose()
*/
protected $lastSyncAt;
/**
* #var Territory
* #ORM\ManyToOne(targetEntity="Territory")
* #ORM\JoinColumn(name="territories_id", referencedColumnName="veeva_territory_id")
* #Expose()
*/
protected $territory;
/**
* #var string
* #ORM\Column(type="string", nullable=true, length=150)
*/
protected $repTokenId;
...
}
There are a few others columns that are required at DB level (nullable = false at entity level), my question: if the object doesn't exists at DB it will be created or update based on the code I wrote or I need to move each required field into conditional? Which is the right way to achieve this? I am trying to do a INSERT|UPDATE based on just one query result
If you retrieved object from db, doctrine itself knows if the next operation will be an update or an insert: you don't have to worry about anything
Your answer is a valid one but I would modify it as follows
$entity = $em->getRepository('AppBundle:Representative')->find($soqlObj1['records'][0]['Id']);
if ($entity === null) {
$entity = new Representative();
}
// we set the values from veeva
$entity->setVeevaRepId($soqlObj1['records'][0]['Id']);
$entity->setEmail($soqlObj1['records'][0]['Email']);
$em->persist($entity);
$em->flush();
As I told you, you don't have to worry about insertion or update, doctrine will do it for you. However, if you need to set certain values only if the object is a new one or if it was fetched from db, just add proper code under $entity === null control

Cant convert onetoone to onetomany in doctrine using symfony2

I got hired about 2 weeks in a company, and my first task was to update and implement new features in an existing software written in symfony2-doctrine. But one of the changes I must make is just breaking my back.
The previous model was: one "cliente" (costumer) could have only one "credito" (credit) and one "credito" could have many "venta" (sales)
The new model should be: one "cliente" (costumer) can have many "credito" and one "credito" can have many "venta" (Im keeping the onetomany association for backwards compatibity)
This is how my entities look like this:
cliente.php
class Cliente {
//put your code here
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $nombre;
/**
* #ORM\Column(type="string", unique=true)
* #Assert\NotBlank()
*/
protected $documento;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $direccion;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $telefono;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $celular;
/**
* #ORM\Column(type="string", nullable=true)
* #Assert\Email()
*/
protected $email;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $ciudad;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $departamento;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $referenciaFamiliar;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $referenciaFamiliarTelefono;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $referenciaPersonal;
/**
* #ORM\Column(type="string", nullable=true)
*/
protected $referenciaPersonalTelefono;
/**
* #ORM\OneToMany(targetEntity="Credito", mappedBy="cliente", cascade={"all"})
*/
protected $credito;
/**
* #ORM\Column(type="string", nullable=true, length=255)
*/
protected $photo;
/**
* #Assert\File(maxSize="300k", mimeTypes={"image/jpeg","image/png"},mimeTypesMessage="Debe subir una imagen JPG o PNG",maxSizeMessage="La imagen no puede pesar más de 300 kb.")
*/
protected $file;
credito.php
class Credito {
//put your code here
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\Column(type="string",nullable=true)
* #Assert\NotBlank()
*/
protected $cc;
/**
* #ORM\Column(type="datetime",nullable=false)
* #Assert\DateTime()
*/
protected $fechaRegistro;
/**
* #ORM\ManyToOne(targetEntity="Cliente",cascade={"persist"})
* #ORM\JoinColumn(name="cliente_id",referencedColumnName="id")
*/
protected $cliente;
/**
* #ORM\OneToMany(targetEntity="Venta", mappedBy="credito",cascade={"all"})
*/
protected $ventas;
/**
* #ORM\OneToMany(targetEntity="Abono", mappedBy="credito",cascade={"persist"})
*/
protected $abonos;
/**
* #ORM\Column(type="datetime",nullable=true)
* #Assert\DateTime()
*/
protected $fechaProximoPago;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\NotBlank()
*/
protected $valorProximoPago;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
*/
protected $cuotasTotales;
/**
* #ORM\Column(type="integer")
* #Assert\NotBlank()
*/
protected $cuotasPagadas;
/**
* #ORM\ManyToOne(targetEntity="ModoPagoNom")
* #ORM\JoinColumn(name="modo_pago_id",referencedColumnName="id")
*/
protected $modoPago;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\NotBlank()
*/
protected $valorFinanciado;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\NotBlank()
*/
protected $cupo;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\NotBlank()
*/
protected $cupoUsado;
venta.php
class Venta{
//put your code here
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
*/
protected $fecha;
/**
* #ORM\Column(type="datetime")
* #Assert\DateTime()
*/
protected $fechaPrimerPago;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $numeroFactura;
/**
* #ORM\Column(type="string")
* #Assert\NotBlank()
*/
protected $numeroAutorizo;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\Min(limit="0", message="El valor de la factura debe ser positivo")
*/
protected $valorFactura;
/**
* #ORM\Column(type="integer")
* #Assert\Type("integer")
* #Assert\Min(limit="1", message="Debe especificar al menos una cuota")
*/
protected $numeroCuotas;
/**
* #ORM\Column(type="integer")
* #Assert\Min(0)
*/
protected $cuotasPagadas;
/**
* #ORM\ManyToOne(targetEntity="ModoPagoNom")
* #ORM\JoinColumn(name="modo_pago_id",referencedColumnName="id")
*/
protected $modoPago;
/**
* #ORM\ManyToOne(targetEntity="Credito",cascade={"persist"})
* #ORM\JoinColumn(name="credito_id",referencedColumnName="id")
*/
protected $credito;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\Min(0.0)
*/
protected $valorFinanciado;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\Min(0.0)
*/
protected $valorCuota;
/**
* #ORM\Column(type="decimal",scale=3, precision=23)
* #Assert\Min(0.0)
*/
protected $valorPrimeraCuota;
/**
* #ORM\ManyToOne(targetEntity="Almacen")
* #ORM\JoinColumn(name="almacen_id",referencedColumnName="id")
*/
protected $almacen;
/**
* Get id
*
* #return integer
*/
The problem is: everytime I insert a new "credito", it wont be linked to the existing "cliente" instead of that, it will try to insert a duplicate entry of "cliente".
I've tried many things but none of them had any effect.
I appreciate any help, cause Im stuck with that.
If further information or code is required I will gladly provide it.
The cascade={"persist"} should be the core of your problem:
class Credito {
......
/**
* #ORM\ManyToOne(targetEntity="Cliente",cascade={"persist"})
* #ORM\JoinColumn(name="cliente_id",referencedColumnName="id")
*/
protected $cliente;
It tells doctrine to persist cleinte always when you persist Credito.
See docs for more details http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-associations.html#transitive-persistence-cascade-operations
Always tell Doctrine how to do in both sides of an association :
// credito.php
/**
* #ORM\ManyToOne(targetEntity="Cliente", inversedBy="credito", cascade={"persist"})
* #ORM\JoinColumn(name="cliente_id",referencedColumnName="id")
*/
protected $cliente;
And
// venta.php
/**
* #ORM\ManyToOne(targetEntity="Credito", inversedBy="ventas", cascade={"persist"})
* #ORM\JoinColumn(name="credito_id",referencedColumnName="id")
*/
protected $credito;

Doctrine - ManyToOne with table between

I want to create model which will have table users with reference to table CustomFieldValue nad table CustomField with reference to CustomFieldValue too. CustomFieldValue will have only id, value and two columns, one from users and second from CustomField. I want to have functionality like a dynamic adding a new fields in registration form. Is this good idea? If yes, please help me with this model, because it doesn't work:
User:
/**
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* #ORM\Column(type="string", length=64)
*/
private $password;
/**
* #ORM\ManyToMany(targetEntity="Role", inversedBy="users",cascade={"persist"})
*
*/
private $roles;
/**
* #ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* #ORM\Column(type="string", length=60)
*/
private $sex;
/**
* #ORM\ManyToMany(targetEntity="Region", inversedBy="users")
* #ORM\JoinColumn(name="region", referencedColumnName="id")
*/
private $region;
/**
* #ORM\ManyToMany(targetEntity="Type", inversedBy="users")
* #ORM\JoinColumn(name="type", referencedColumnName="id")
*/
private $type;
/**
* #ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue",inversedBy="id")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $customValues;
CustomFieldValue:
/**
* #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=255)
*/
private $value;
/**
*ORM\OneToMany(targetEntity="User", mapped-by="id")
*#ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
/**
*#ORM\OneToMany(targetEntity="CustomField", mapped-by="id" )
*#ORM\JoinColumn(name="field_id", referencedColumnName="id")
*/
private $field;
CustomField:
/**
* #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=255)
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=255)
*/
private $type;
/**
* #var boolean
*
* #ORM\Column(name="required", type="boolean")
*/
private $required;
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="id")
* #ORM\JoinColumn(name="customfield_id", referencedColumnName="id")
*/
private $customValues;
Your mapping is a little off:
User Entity Should Be:
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="user")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $customValues;
CustomFieldValue Should Be:
/**
*ORM\OneToMany(targetEntity="User", mappedBy="customValues")
*/
private $user;
/**
*#ORM\OneToMany(targetEntity="CustomField", mappedBy="customValues" )
*/
private $field;
CustomField should be:
/**
* #ORM\ManyToOne(targetEntity="CustomFieldValue", inversedBy="field")
* #ORM\JoinColumn(name="customfield_id", referencedColumnName="id")
*/
private $customValues;
You dont need the join columns when you are calling the mappedBy this already tells doctrine to look for the join column declaration on that field. For the mappedBy and inversedBy fields these are the fields that link the 2 together NOT the actual join column name.

Categories