Bueno puedes hacerlo de la siguiente manera antes persistir la entidad user para hacer el flush puedes poner $user->setUser_type(xxxxxxx) le pasas el user_type que desees y listo después pones algo como:
$em->persist($user);
$em->flush();
Saludos
Osay González Fuentes.
Centro Nacional de Calidad de Software.
La Habana, Cuba.
From: symfo...@googlegroups.com [mailto:symfo...@googlegroups.com] On Behalf Of reiziel
Sent: viernes, 07 de septiembre de 2012 13:32
To: symfo...@googlegroups.com
Subject: [symfony-es] Cómo definir un valor por defecto al crear una entidad
Hola amigos,
Tengo una duda en symfony 2.1 y solicito su ayuda. Tengo dos entidades, una es User y la otra es UserType y necesito que cuando se cree un User tenga por defecto un UserType. Los tipos de usuario ya están creados, por lo que básicamente necesito que cuando se cree un objeto del tipo User para insertarlo en la bd tenga por defecto un UserType de los que ya están creados en la BD. Agradezco su ayuda.
Mi entidad User es:
namespace zzz\zzzBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use zzz\zzzBundle\Entity\UserType;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
....
/**
* @ORM\OneToOne(targetEntity="UserType")
* @ORM\JoinColumn(name="user_type_id", referencedColumnName="id")
*/
protected $user_type;
}
Y la entidad UserType:
namespace zzz\zzzBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users_type")
*/
class UserType
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="type", type="string")
*/
protected $type;
}
--
Has recibido este mensaje porque estás suscrito al grupo "symfony-es" de Google Groups.
Para publicar en este grupo, envía un email a symfo...@googlegroups.com
Para darte de baja, envía un email a symfony-es+...@googlegroups.com
El resto de opciones puedes encontrarlas en http://groups.google.com/group/symfony-es?hl=es
Database default values are not "portably" supported. The only way to use database default values is through the columnDefinition mapping attribute where you specify the SQL snippet (DEFAULT cause inclusive) for the column the field is mapped to.
You can use:
<?php
/**
* @Entity
*/
class myEntity {
/**
* @var string
*
* @Column(name="myColumn", type="string", length="50")
*/
private $myColumn = 'myDefaultValue';
...
}http://stackoverflow.com/questions/3376881/default-value-in-doctrine
You should have a service for creating new YourEntity instances. The service will know of to retrieve the default group, or how to create a reference to the default group, and will take care of passing it to the entity's constructor.
For example:
http://stackoverflow.com/questions/7301813/doctrine-entity-as-default-value-in-another-entityclass YourEntityService
{
...
public function createNewYourEntity()
{
$defaultGroup = $this->em->getReference('Usergroup', 1);
return new YourEntity($defaultGroup);
}
...
}
Doctrine does not support to set the default values in columns through the “DEFAULT” keyword in SQL. This is not necessary however, you can just use your class properties as default values. These are then used upon insert:
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/faq.html
Esto también: ayuda https://www.google.com.py/search?q=doctrine+deafault+value+for+entity&sugexp=chrome,mod=13&sourceid=chrome&ie=UTF-8#hl=es&pwst=1&sa=X&ei=WU5KUJL1IYPq9ATaxoHADA&ved=0CB0QvwUoAQ&q=doctrine+default+value+for+entity&spell=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.&fp=6d4c6ca7f9647072&biw=1366&bih=624
El 7 de septiembre de 2012 16:47, Leonardo Fernandez <leod...@gmail.com> escribió:
Buscando un poco encontre estoDatabase default values are not "portably" supported. The only way to use database default values is through the columnDefinition mapping attribute where you specify the SQL snippet (DEFAULT cause inclusive) for the column the field is mapped to.
You can use:
<?php /** * @Entity */ class myEntity { /** * @var string * * @Column(name="myColumn", type="string", length="50") */ private $myColumn = 'myDefaultValue'; ... }http://stackoverflow.com/questions/3376881/default-value-in-doctrineYou should have a service for creating new YourEntity instances. The service will know of to retrieve the default group, or how to create a reference to the default group, and will take care of passing it to the entity's constructor.
For example:
class