我在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();
现在,当我尝试使用此代码从Symfony2控制器运行INSERT
时:
$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())));
}
我明白了:
执行'INSERT INTO waccount时发生异常 (account_id,account_number,bank_name,余额,创建,修改, account_type)VALUES(?,?,?,?,?,?,?)'与params [1, “01234567890121345678”,“BankOfAmerica”,null,null,null,6]:
SQLSTATE [23502]:非空违规:7错误:列中的空值 “balance”违反了非空约束DETAIL:失败的行包含 (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