akyayik
akyayik

Reputation: 664

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

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;
}

Upvotes: 0

Views: 35

Answers (1)

Zac Ball
Zac Ball

Reputation: 198

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

Upvotes: 1

Related Questions