0

I have three tables in a PostgreSQL database where I run this queries:

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();

Now when I trying to run a INSERT from Symfony2 controller using this code:

$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())));
}

I get this:

An exception occurred while executing 'INSERT INTO waccount (account_id, account_number, bank_name, balance, created, modified, account_type) VALUES (?, ?, ?, ?, ?, ?, ?)' with params [1, "01234567890121345678", "BankOfAmerica", null, null, null, 6]:

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "balance" violates not-null constraint DETAIL: Failing row contains (1, 01234567890121345678, BankOfAmerica, 6, null, null, null).

Why Symfony or Doctrine doesn't deal with default values? I'm doing something wrong or I miss something here?

Account Entity

This is my account entity:

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;
    }

}

The table waccount exists on DB

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
Reynier
  • 2,420
  • 11
  • 51
  • 91

0 Answers0