我在运行此查询的 PostgreSQL 数据库中有三个表:
ALTER TABLE waccount ALTER COLUMN balance SET DEFAULT 0;
ALTER TABLE waccount ALTER COLUMN created SET DEFAULT now();
ALTER TABLE waccount ALTER COLUMN modified SET DEFAULT now();
现在,当我尝试INSERT
使用以下代码从 Symfony2 控制器运行时:
$entity = new Account();
$form = $this->createForm(new AccountType(), $entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('wba_show', array('id' => $entity->getAccountId())));
}
我明白了:
使用参数 [1, "01234567890121345678" 执行 'INSERT INTO waccount (account_id, account_number, bank_name, balance, created, modified, account_type) VALUES (?, ?, ?, ?, ?, ?, ?)' 时发生异常, “美国银行”,空,空,空,6]:
SQLSTATE [23502]:非空违规:7 错误:“余额”列中的空值违反非空约束详细信息:失败行包含 (1, 01234567890121345678, BankOfAmerica, 6, null, null, null)。
为什么 Symfony 或 Doctrine 不处理默认值?我做错了什么或者我错过了什么?
账户实体
这是我的帐户实体:
namespace BankBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Account
*
* @ORM\Entity
* @ORM\Table(name="waccount")
*/
class Account {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*
*/
protected $account_id;
/**
*
* @ORM\Column(type="string", length=20)
*/
protected $account_number;
/**
*
* @ORM\Column(type="string", length=150)
*/
protected $bank_name;
/**
* @ORM\OneToOne(targetEntity="BankBundle\Entity\AccountType")
* @ORM\JoinColumn(name="account_type", referencedColumnName="type_id")
*/
protected $account_type;
/**
*
* @ORM\Column(type="float")
*/
protected $balance;
/**
* @var \DateTime
* @ORM\Column(type="datetime")
*/
protected $created;
/**
* @var \DateTime
* @ORM\Column(type="datetime")
*/
protected $modified;
public function getAccountId() {
return $this->account_id;
}
public function setAccountNumber($account_number) {
$this->account_number = $account_number;
}
public function getAccountNumber() {
return $this->account_number;
}
public function setAccountType($account_type) {
$this->account_type = $account_type;
}
public function setBankName($bank_name) {
$this->bank_name = $bank_name;
}
public function getBankName() {
return $this->bank_name;
}
public function getAccountType() {
return $this->account_type;
}
public function setBalance($balance) {
$this->balance = (float) $balance;
}
public function getBalance() {
return $this->balance;
}
public function setCreated($created) {
$this->created = $created;
}
public function getCreated() {
return $this->created;
}
public function setModified($modified) {
$this->modified = $modified;
}
public function getModified() {
return $this->modified;
}
}
该表waccount
存在于数据库中