Symfony实体验证验证外键是否存在

时间:2017-06-29 13:29:04

标签: symfony validation docker entity

我希望能够使用实体验证器约束来验证外键book_id是否有效,请参阅下文:

book.php中

/**
 * Book
 *
 * @ORM\Table("book")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="AppBundle\Repository\BookRepository")
 */
class Book
{
     /**
      * @var integer
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     private $id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string")
     * @Assert\Length(
     *     max = 250,
     *     maxMessage = "Name cannot be longer than {{ limit }} characters",
     *     groups={"create","update"}
     * )
     */
    private $name;

    /**
     * @ORM\OneToOne(targetEntity="Loan", mappedBy="book", fetch="LAZY")
     */
    protected $loan;
}

Loan.php

/**
 * Loan
 *
 * @ORM\Table("loan")
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="AppBundle\Repository\LoanRepository")
 */
class Loan
{
     /**
      * @var integer
      * @ORM\Column(name="id", type="integer")
      * @ORM\Id
      * @ORM\GeneratedValue(strategy="AUTO")
      */
     private $id;

    /**
     * @var integer
     * @ORM\Column(name="book_id", type="integer")
     */
    protected $book_id;

    /**
     * @var string
     * @ORM\Column(name="name", type="string")
     * @Assert\Length(
     *     max = 500,
     *     maxMessage = "Person cannot be longer than {{ limit }} characters",
     *     groups={"create","update"}
     * )
     */
    private $person;

     /**
      * @ORM\OneToOne(targetEntity="Book", inversedBy="loan")
      * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
      */
    protected $book;
}

以下是我目前正在验证贷款实体的方式

    $loan = new Loan();
    $loan->setPerson($person);
    $loan->setBookId($id);

    /** @var ConstraintViolation $error */
    foreach ($this->get('validator')->validate($loan,null,['create'])->getIterator() as $index => $error) {
        $errorMessages[] = $error->getMessage();
    }

我想我可以将这样的自定义验证器添加到贷款实体中:

/**
 * @Assert\IsTrue(message = "The book does not exist")
 * @return bool
 */
public function isBookLegal(BookRepository $bookRepository)
{
    return !$bookRepository->fetchById($this->book_id);
}

但我最终得到了以下例外:

Type error: Too few arguments to function 
AppBundle\Entity\Loan::isBookLegal(), 0 passed and exactly 1 expected

1 个答案:

答案 0 :(得分:3)

首先,您的$book_id实体不应同时拥有$bookLoan。您应该删除$book_id,这足以实现您的实体关系。

然后,您需要做的就是在@Assert\NotBlank()上添加$book

use Symfony\Component\Validator\Constraints as Assert;

...

 /**
  * @ORM\OneToOne(targetEntity="Book", inversedBy="loan")
  * @ORM\JoinColumn(name="book_id", referencedColumnName="id")
  * @Assert\NotBlank()
  */
protected $book;

我不确定你用什么代码来获得所有贷款,但正如埃德温所说,这并不是一个好的形式。你想要更像的东西:

foreach ($loans as $loan) {
    $errors = $this->get('validator')->validate($loan);
    // do something here if there is an error
}

您编写的断言函数无法正常工作,因为您无法将值传递给isBookLegal()函数,也无法在Entity类中使用数据库连接/存储库。< / p>

如果没有更好的背景,我不确定你要完成什么。由于您首先$book注释,因此我们已经开始验证您的@ORM\OneToOne成员。您不必执行任何其他验证。我猜你试图直接将值传递给$book_id,这是不正确的。您应该只通过$book

将已经有效的Loan实体传递到$loan->setBook(Book $book);班级