我有一张发票表。在此表中,我有:
1- invoice_id
列,即PRIMARY KEY
和auto_increment
。
2- invoice_number
列,在数据库中是唯一的。
在我的实体中,我有此代码
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Invoices
*
* @ORM\Table(name="invoices", uniqueConstraints={@ORM\UniqueConstraint(name="invoice_number", columns={"invoice_number"})})
* @ORM\Entity
* @ORM\HasLifecycleCallbacks
*
* @UniqueEntity(fields="invoiceNumber", message="invoice number is already exist")
*/
class Invoices
{
/**
* @var integer
*
* @ORM\Column(name="invoice_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $invoiceId;
/**
* @var string
*
* @ORM\Column(name="invoice_number", type="string", length=50, nullable=true, unique=true)
*/
private $invoiceNumber;
在控制器中,我像这样使用$form->isValid()
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
}
我通过此代码渲染字段
{{ form_label(form.salesInvoice.invoice.invoiceNumber, null, {'label_attr':{'class':'black'}}) }} <span class="required">*</span>
{{ form_widget(form.salesInvoice.invoice.invoiceNumber) }}
<div class="required"> {{ form_errors(form.salesInvoice.invoice.invoiceNumber) }}</div>
当我尝试使用重复的发票编号保存发票时,出现此错误
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1230008' for key 'invoice_number'
它不应尝试保存,但显示验证消息
发票号码已经存在
答案 0 :(得分:0)
就我而言,fields
批注中的属性UniqueEntity
应该是一个数组,所以:
@UniqueEntity(fields={"invoiceNumber"}, message="invoice number is already exist")
代替:
@UniqueEntity(fields="invoiceNumber", message="invoice number is already exist")
那应该做。