我有3个实体User
,Problem
和Submission
。我试图限制用户多次解决同一个问题,如果该用户已经解决了它。我通过检查每个用户的User
在Submissions
实体中创建了一个方法。但是我找不到匹配problem_id和user_id字段的方法,所以我可以看到提交是否正确。
以下是有意思的; 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实现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;
}
答案 0 :(得分:1)
您可以在Submission实体上尝试@UniqueConstraint。类似的东西:
/**
* @Entity
* @Table(uniqueConstraints={@UniqueConstraint(name="unique_user_submission", columns={"name", "email"})})
*/
class Submission
{
}
我认为这会使这两个领域成为一个复合的独特领域。