PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 18, 2022

[FIXED] How to make combination of two columns unique in Symfony 4 using Doctrine?

 October 18, 2022     doctrine, doctrine-orm, php, symfony     No comments   

Issue

I have a table named 'student_assignment' in which I have multiple columns from which I am showing 2 of them below:

Both of these columns are also foreign keys.

StudentId   assignmentId
    10           7           -> allowed
    10           8           -> allowed
    11           7           -> allowed
    11           7           -> not allowed, the combination of 11 7 already exists in table

I have tried this in my entity file, but it does not work.

/**
 * Webkul\CampusConnect\Entity\StudentAssignment
 *
 * @Table(name="student_assignment", 
 *    uniqueConstraints={
 *        @UniqueConstraint(name="assignment_unique", 
 *            columns={"student", "assignment"})
 *    }
 * )
 * @Entity
 */

Please how to implement this using ORM in symfony 4.

I have a link which does ther same but in Mysql. I want the solution for Symfony ORM. enter link description here

Error:

[Semantical Error] The annotation "@Table" in class Webkul\CampusConnect\En tity\StudentAssignment was never imported. Did you maybe forget to add a "u se" statement for this annotation?

Entity:

namespace Webkul\CampusConnect\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Table;

/**
 * Webkul\CampusConnect\Entity\StudentAssignment
 *
 * @ORM\Table(name="student_assignment", 
 *    uniqueConstraints={
 *        @UniqueConstraint(name="assignment_unique", 
 *            columns={"student", "assignment"})
 *    }
 * )
 * @Entity
 */
class StudentAssignment
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Webkul\CampusConnect\Entity\Student", inversedBy="studentAssignments")
     * @ORM\JoinColumn(onDelete="CASCADE")
     */
    private $student;

Solution

You've edited, but you weren't using ORM as an imported alias, that was number 1 (see comments).

Then you missed adding ORM to the inner configuration, e.g. @ORM\UniqueConstraint instead of @UniqueConstraint. Also, the configuration of UniqueConstraint requires the use of the column names, not property.

You've not provided both sides of the join table'esque OtM - MtO relation, but I'll assume it exists. You should have:

namespace Webkul\CampusConnect\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(
 *    name="student_assignment", 
 *    uniqueConstraints={
 *        @ORM\UniqueConstraint(name="assignment_unique", columns={"student_id", "assignment_id"})
 *    }
 * )
 * @Entity
 */
class StudentAssignment
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(name="id", type="integer")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Webkul\CampusConnect\Entity\Student", inversedBy="studentAssignments")
     * @ORM\JoinColumn(name="student_id", onDelete="CASCADE")
     */
    private $student;

    /**
     * @ORM\ManyToOne(targetEntity="Webkul\CampusConnect\Entity\Assignment", inversedBy="studentAssignments")
     * @ORM\JoinColumn(name="assignment_id", onDelete="CASCADE")
     */
    private $assignment;

    // ...
}


Answered By - rkeet
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing