Me ha surgido una necesidad de extender una entidad a dos niveles y ando un poco perdido, pues es la primera vez que afronto una necesidad así.
Estoy implementando FOSUserBundle con un paquete de administración (https://github.com/avanzu/AdminThemeBundle) Avanzu AdminThemeBundle.
Bien, quiero unir las entidades de usuario, basandome en la que tiene FOS, me gustaría que la mia extendiese a la de Avanzu y ésta a la de FOS, para asi usar todo el repertorio y poder tener un despliegue completo de opciones con el bundle de admin, ya que si no uso su sistema de usuarios tendría que cambiar prácticamente todo el sistema del bundle y si lo he cogido ha sido para ahorrar tiempo.
Os dejo las clases.
Avanzu userModel.php
Código PHP:
<?php
/**
* UserModel.php
* avanzu-admin
* Date: 23.02.14
*/
namespace AvanzuAdminThemeBundleModel;
use FOSUserBundleModelUser as BaseUser;
class UserModel extends BaseUser implements UserInterface {
/**
* @var string
*/
protected $avatar;
/**
* @var string
*/
protected $username;
/**
* @var string
*/
protected $name;
/**
* @var string
*/
protected $title;
/**
* @var \DateTime
*/
protected $memberSince;
/**
* @var bool
*/
protected $isOnline = false;
function __construct($username='', $avatar = '', $memberSince = null, $isOnline = true, $name='', $title='')
{
$this->avatar = $avatar;
$this->isOnline = $isOnline;
$this->memberSince = $memberSince ?:new DateTime();
$this->username = $username;
$this->name = $name;
$this->title = $title;
}
/**
* @param string $avatar
*
* @return $this
*/
public function setAvatar($avatar)
{
$this->avatar = $avatar;
return $this;
}
/**
* @return string
*/
public function getAvatar()
{
return $this->avatar;
}
/**
* @param boolean $isOnline
*
* @return $this
*/
public function setIsOnline($isOnline)
{
$this->isOnline = $isOnline;
return $this;
}
/**
* @return boolean
*/
public function getIsOnline()
{
return $this->isOnline;
}
/**
* @param \DateTime $memberSince
*
* @return $this
*/
public function setMemberSince(DateTime $memberSince)
{
$this->memberSince = $memberSince;
return $this;
}
/**
* @return \DateTime
*/
public function getMemberSince()
{
return $this->memberSince;
}
/**
* @param string $username
*
* @return $this
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $name
*
* @return $this
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @param string $title
*
* @return $this
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* @return bool
*/
public function isOnline()
{
return $this->getIsOnline();
}
public function getIdentifier() {
return str_replace(' ', '-', $this->getUsername());
}
}
Código PHP:
<?php
namespace AcmeDemoBundleEntity;
use AvanzuAdminThemeBundleModelUserModel as BaseUser;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="empleados")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $firstName;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $lastName;
/**
* @ORM\Column(type="string", nullable=true)
*/
protected $puesto;
/**
* @ORM\OneToMany(targetEntity="Vacaciones", mappedBy="id")
**/
private $vacaciones;
/**
* @ORM\ManyToMany(targetEntity="\Acme\DemoBundle\Entity\Group")
* @ORM\JoinTable(name="fos_user_user_group",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
public function __construct()
{
parent::__construct();
$this->vacaciones = new ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setEmail($email)
{
parent::setEmail($email);
$this->setUsername($email);
}
/**
* Set firstName
*
* @param string $firstName
* @return User
*/
public function setFirstName($firstName)
{
$this->firstName = $firstName;
return $this;
}
/**
* Get firstName
*
* @return string
*/
public function getFirstName()
{
return $this->firstName;
}
/**
* Set lastName
*
* @param string $lastName
* @return User
*/
public function setLastName($lastName)
{
$this->lastName = $lastName;
return $this;
}
/**
* Get lastName
*
* @return string
*/
public function getLastName()
{
return $this->lastName;
}
/**
* Add vacaciones
*
* @param \Acme\DemoBundle\Entity\Vacaciones $vacaciones
* @return User
*/
public function addVacacione(AcmeDemoBundleEntityVacaciones $vacaciones)
{
$this->vacaciones[] = $vacaciones;
return $this;
}
/**
* Remove vacaciones
*
* @param \Acme\DemoBundle\Entity\Vacaciones $vacaciones
*/
public function removeVacacione(AcmeDemoBundleEntityVacaciones $vacaciones)
{
$this->vacaciones->removeElement($vacaciones);
}
/**
* Get vacaciones
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getVacaciones()
{
return $this->vacaciones;
}
/**
* Add groups
*
* @param \Acme\DemoBundle\Entity\Group $groups
* @return User
*/
public function addGroups(AcmeDemoBundleEntityGroup $groups)
{
$this->groups[] = $groups;
return $this;
}
/**
* Remove groups
*
* @param \Acme\DemoBundle\Entity\Group $groups
*/
public function removeGroups(AcmeDemoBundleEntityGroup $groups)
{
$this->groups->removeElement($groups);
}
/**
* Get groups
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getGroups()
{
return $this->groups;
}
/**
* Set puesto
*
* @param string $puesto
* @return User
*/
public function setPuesto($puesto)
{
$this->puesto = $puesto;
return $this;
}
/**
* Get puesto
*
* @return string
*/
public function getPuesto()
{
return $this->puesto;
}
}