*Please, help me. *

*I have used the official documentation example. But I can't find a 
solution for this problem.*


Doctrine\DBAL\Exception\NotNullConstraintViolationException

An exception occurred while executing 'INSERT INTO teniluser_perfil (nome, 
sobrenome, apelido, is_gravatar, id_foto, id_user, id_tratamento) VALUES 
(?, ?, ?, ?, ?, ?, ?)' with params ["Roberto", "Santos", null, "0", null, 
null, null]:


SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id_user' 
cannot be null


*User.php*
<?php

namespace TenilUser\Entity;

use Doctrine\ORM\Mapping as ORM;
use Zend\Math\Rand;
use Zend\Crypt\Key\Derivation\Pbkdf2;
/**
 * User
 *
 * @ORM\Entity(repositoryClass="TenilUser\Entity\UserRepository")
 * @ORM\Table(name="teniluser_user")
 * @ORM\HasLifecycleCallbacks
 */
class User
{

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\OneToOne(targetEntity="Perfil", mappedBy="user", 
cascade={"persist"})
 **/
protected $perfil;

/**
 * @var string
 *
 * @ORM\Column(name="email", type="string", length=255, nullable=false, 
unique=true)
 */
protected $email;

/**
 * @var string
 *
 * @ORM\Column(name="password", type="string", length=255, nullable=false)
 */
protected $password;

/**
 * @var string
 *
 * @ORM\Column(name="salt", type="string", length=255, nullable=false)
 */
protected $salt;

/**
 * @var boolean
 *
 * @ORM\Column(name="active", type="boolean", nullable=false)
 */
protected $active = '0';

/**
 * @var string
 *
 * @ORM\Column(name="activation_key", type="string", length=255, 
nullable=false)
 */
protected $activationKey;

/**
 * @var string
 *
 * @ORM\Column(name="password_reset_key", type="string", length=255, 
nullable=false)
 */
protected $passwordResetKey;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="updated_at", type="datetime", nullable=false)
 */
protected $updatedAt;

/**
 * @var \DateTime
 *
 * @ORM\Column(name="created_at", type="datetime", nullable=false)
 */
protected $createdAt;

public function __construct(array $options = array())
{
/*
 * Executado no momento da criação do objeto.
 * 
 * IMPORTANTE: Verificar por que algumas coisas são atribuidas no construtor
 * e outras nos setters. Ex. $password
 */
$this->createdAt = new \DateTime("now");
$this->updatedAt = new \DateTime("now");
// Atribuindo o valor para o salt.
// Rand::getBytes(8, TRUE) Gera uma string de 8 caracteres.
// base64_encode converte para a base 64.
$this->salt = base64_encode(Rand::getBytes(8, TRUE));

// Atribuindo o valor da chave de ativação.
$this->activationKey = md5($this->email . $this->salt);

// Getters e Setters automáticos (subistitui o configurator.php)
/*
 * $hydrator = new Hydrator\ClassMethods;
 * $hydrator->hydrate($options, $this);        
 */
// Somente php >= 5.4
}

public function encryptPassword($password)
{

$hash = 'sha256';
$salt = $this->salt;
$iterations = 10000;
$length = 32;
$data = Pbkdf2::calc($hash, $password, $salt, $iterations, $length);

return base64_encode($data);
}

public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
return $this;
}

public function getEmail()
{
return $this->email;
}

public function setEmail($email)
{
$this->email = strtolower($email);
return $this;
}

public function getPassword()
{
return $this->password;
}

public function setPassword($password)
{
$this->password = $this->encryptPassword($password);
return $this;
}

public function getSalt()
{
return $this->salt;
}

public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}

public function getActive()
{
return $this->active;
}

public function setActive($active)
{
$this->active = $active;
return $this;
}

public function getActivationKey()
{
return $this->activationKey;
}

public function setActivationKey($activationKey)
{
$this->activationKey = $activationKey;
return $this;
}

public function getPasswordResetKey()
{
return $this->passwordResetKey;
}

public function setPasswordResetKey($passwordResetKey = NULL)
{
// se for passado um valor TRUE, então será gerada a chave.
// caso contrario, a chave será nula
$this->passwordResetKey = $passwordResetKey ? Rand::getString(64, 
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', TRUE) : 
NULL;
return $this;
}

public function getUpdatedAt()
{
return $this->updatedAt;
}

/**
 * prePersist: Antes de gravar as informações no banco, ele executa o 
método.
 * @return User
 * @internal param \DateTime $updatedAt
 * @ORM\PreUpdate
 */
public function setUpdatedAt()
{
$this->updatedAt = new \DateTime("now");
return $this;
}

public function getCreatedAt()
{
return $this->createdAt;
}

public function setCreatedAt(\DateTime $createdAt)
{
$this->createdAt = $createdAt;
return $this;
}

public function __toString()
{
return $this->email;
}

public function getPerfil()
{
return $this->perfil;
}

/**
 * Allow null to remove association
 *
 * @param Perfil $perfil
 * @return User $this
 */
public function setPerfil(Perfil $perfil = null)
{
$this->perfil = $perfil;
return $this;
}
}


*Pefil.php*
<?php

namespace TenilUser\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

/**
 * Perfil
 *
 * @ORM\Entity(repositoryClass="PerfilRepository")
 * @ORM\Table(name="teniluser_perfil")
 */
class Perfil
{

/**
 * @var integer $id
 * @ORM\Id
 * @ORM\Column(name="id", type="integer", nullable=false)
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\OneToOne(targetEntity="User", inversedBy="perfil")
 * @ORM\JoinColumn(name="id_user", referencedColumnName="id", 
nullable=false)
 */
protected $user;

/**
 * @ORM\OneToMany(targetEntity="Telefone", mappedBy="perfil", 
cascade={"persist"})
 */
protected $telefones;

/**
 * @ORM\OneToMany(targetEntity="Endereco", mappedBy="perfil", 
cascade={"persist"})
 */
protected $enderecos;

/**
 * @var string
 * @ORM\Column(name="nome", type="string", length=255, nullable=true)
 */
protected $nome;

/**
 * @var string
 * @ORM\Column(name="sobrenome", type="string", length=255, nullable=true)
 */
protected $sobrenome;

/**
 * @var string
 * @ORM\Column(name="apelido", type="string", length=255, nullable=true)
 */
protected $apelido;

/**
 * @ORM\ManyToOne(targetEntity="PerfilTratamento")
 * @ORM\JoinColumn(name="id_tratamento", referencedColumnName="id")
 */
protected $tratamento;

/**
 * @var boolean
 * @ORM\Column(name="is_gravatar", type="boolean", nullable=true)
 */
protected $isGravatar = '0';

/**
 * @var string
 * @ORM\Column(name="id_foto", type="string", length=45, nullable=true)
 */
protected $foto;

/**
 * Nunca esquecer de inicializar todas as coleções!
 */
public function __construct()
{
$this->telefones = new ArrayCollection();
$this->enderecos = new ArrayCollection();
}

/**
 * @return mixed
 */
function getId()
{
return $this->id;
}

/**
 * @return string
 */
function getNome()
{
return $this->nome;
}

/**
 * @return string
 */
function getSobrenome()
{
return $this->sobrenome;
}

/**
 * @return string
 */
function getApelido()
{
return $this->apelido;
}

function getTratamento()
{
return $this->tratamento;
}

/**
 * @return bool
 */
function getIsGravatar()
{
return $this->isGravatar;
}

/**
 * @return string
 */
function getFoto()
{
return $this->foto;
}

/**
 * @param $id
 * @return $this
 */
function setId($id)
{
$this->id = $id;
return $this;
}

/**
 * @param $nome
 * @return $this
 */
function setNome($nome)
{
$this->nome = $nome;
return $this;
}

/**
 * @param $sobrenome
 * @return $this
 */
function setSobrenome($sobrenome)
{
$this->sobrenome = $sobrenome;
return $this;
}

/**
 * @param $apelido
 * @return $this
 */
function setApelido($apelido)
{
$this->apelido = $apelido;
return $this;
}

/**
 * @param $isGravatar
 * @return $this
 */
function setIsGravatar($isGravatar)
{
$this->isGravatar = $isGravatar;
return $this;
}

/**
 * @param $foto
 * @return $this
 */
function setFoto($foto)
{
$this->foto = $foto;
return $this;
}

/**
 * @param Collection $telefones
 */
public function addTelefones(Collection $telefones)
{
foreach ($telefones as $telefone) {
$telefone->setPerfil($this);
$this->telefones->add($telefone);
}
}

/**
 * @param Collection $telefones
 */
public function removeTelefones(Collection $telefones)
{
foreach ($telefones as $telefone) {
$telefone->setPerfil(null);
$this->telefones->removeElement($telefone);
}
}

/**
 * @return Collection
 */
public function getTelefones()
{
return $this->telefones;
}

/**
 * @param Collection $enderecos
 */
public function addEnderecos(Collection $enderecos)
{
foreach ($enderecos as $endereco) {
$endereco->setPerfil($this);
$this->enderecos->add($endereco);
}
}

/**
 * @param Collection $enderecos
 */
public function removeEnderecos(Collection $enderecos)
{
foreach ($enderecos as $endereco) {
$endereco->setPerfil(null);
$this->enderecos->removeElement($endereco);
}
}

/**
 * @return Collection
 */
public function getEnderecos()
{
return $this->enderecos;
}

/**
 * @return string
 */
public function __toString()
{
return $this->getNome();
}

/**
 * Allow null to remove association
 *
 * @param User $user
 * @return Perfil $this
 */
public function setUser(User $user = null)
{
$this->user = $user;
return $this;
}

/**
 * @return User
 */
public function getUser()
{
return $this->user;
}

}

*indexController.php*
.
.
.

public function doctrineAction(){

$entityManager = 
$this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
$hydrator = new DoctrineHydrator($entityManager);
$user = new User();
$perfil = new Perfil();
$perfil->setNome('Roberto');
$perfil->setSobrenome('Santos');

$data = array(
'email' => '[email protected]',
'password' => 'p@$$w0rd',
'perfil'  => $perfil
);

$user = $hydrator->hydrate($data, $user);

$entityManager->persist($user);
$entityManager->flush();

echo $user->getEmail(); // prints "[email protected]"
echo $user->getPerfil(); // prints "Roberto"

die;
}

.
.
.


-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to