Ver Mensaje Individual
  #5 (permalink)  
Antiguo 10/07/2012, 10:19
Fastigos
 
Fecha de Ingreso: enero-2012
Mensajes: 5
Antigüedad: 12 años, 3 meses
Puntos: 0
Respuesta: Problemas con la herencia de Symfony2 i doctrine

Solo quiero el primero de la lista, ya que solo habrá uno, pero bueno el problema sería el mismo si los quisiera todos.

Lo de la A lo havia puesto para que fuera mas leible el codigo, en verdad es así:

Esta es la entidad D:
Código PHP:
Ver original
  1. <?php
  2.  
  3. namespace Axon\BackendEstacionsBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\Collection;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
  9. use Axon\BackendBundle\Entity\Pujable;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11.  
  12. /**
  13.  * @ORM\Entity
  14.  * @ORM\Table(name="estacio")
  15.  * @ORM\Entity(repositoryClass="Axon\BackendEstacionsBundle\Entity\EstacioRepository")
  16.  */
  17. class Estacio extends Pujable {
  18.  
  19.     /**
  20.      *  @ORM\Id
  21.      *  @ORM\Column(type="integer")
  22.      *  @ORM\GeneratedValue
  23.      */
  24.     protected $id;
  25.  
  26.     /**
  27.      * @ORM\Column(type="string", length=255)
  28.      * @Assert\NotBlank()
  29.      */
  30.     protected $nombre;
  31.  
  32.     /**
  33.      * @ORM\ManyToOne(targetEntity="\Axon\BackendSectorsBundle\Entity\Sector", inversedBy="estacions")
  34.      * @ORM\JoinColumn(name="sector_id", referencedColumnName="id")
  35.      */
  36.     private $sector;
  37.  
  38.     /**
  39.      * @ORM\OneToMany(targetEntity="\Axon\BackendSmartisBundle\Entity\Smarti", mappedBy="estacio")
  40.      */
  41.     private $smartis;
  42.  
  43.     /**
  44.      * Get id
  45.      *
  46.      * @return integer
  47.      */
  48.     public function getId() {
  49.         return $this->id;
  50.     }
  51.  
  52.     /**
  53.      * Set nom
  54.      *
  55.      * @param string $nombre
  56.      */
  57.     public function setNombre($nom) {
  58.         $this->nombre = $nombre;
  59.     }
  60.  
  61.     /**
  62.      * Get nombre
  63.      *
  64.      * @return string
  65.      */
  66.     public function getNombre() {
  67.         return $this->nombre;
  68.     }
  69.     /**
  70.      * Set sector
  71.      *
  72.      * @param Axon\BackendSectorsBundle\Entity\Sector $sector
  73.      */
  74.     public function setSector(\Axon\BackendSectorsBundle\Entity\Sector $sector) {
  75.         $this->sector = $sector;
  76.     }
  77.  
  78.     /**
  79.      * Get sector
  80.      *
  81.      * @return Axon\BackendSectorsBundle\Entity\Sector
  82.      */
  83.     public function getSector() {
  84.         return $this->sector;
  85.     }
  86.  
  87.     /**
  88.      * Set latitud_sector_sin
  89.      *
  90.      * @param string $latitudSectorSin
  91.      */
  92.     public function setLatitudSectorSin($latitudSectorSin) {
  93.         $this->latitud_sector_sin = $latitudSectorSin;
  94.     }
  95.  
  96.     /**
  97.      * Get latitud_sector_sin
  98.      *
  99.      * @return string
  100.      */
  101.     public function getLatitudSectorSin() {
  102.         return $this->latitud_sector_sin;
  103.     }
  104.  
  105.     /**
  106.      * Set longitud_sector_sin
  107.      *
  108.      * @param string $longitudSectorSin
  109.      */
  110.     public function setLongitudSectorSin($longitudSectorSin) {
  111.         $this->longitud_sector_sin = $longitudSectorSin;
  112.     }
  113.  
  114.     /**
  115.      * Get longitud_sector_sin
  116.      *
  117.      * @return string
  118.      */
  119.     public function getLongitudSectorSin() {
  120.         return $this->longitud_sector_sin;
  121.     }
  122.  
  123.     //gets per les estacions
  124.  
  125.     public function getSmartiByTipus($tipus) {
  126.         $this->smartis->initialize();
  127.         foreach ($this->smartis as $smarti) {
  128.             if ($smarti->getTipo() == $tipus) {
  129.                 return $smarti;
  130.             }
  131.         }
  132.         return null;
  133.     }
  134.  
  135.     public function __construct() {
  136.         $this->smartis = new \Doctrine\Common\Collections\ArrayCollection();
  137.     }
  138.  
  139.     /**
  140.      * Add smartis
  141.      *
  142.      * @param Axon\BackendSmartisBundle\Entity\Smarti $smartis
  143.      */
  144.     public function addSmarti(\Axon\BackendSmartisBundle\Entity\Smarti $smartis) {
  145.         $this->smartis[] = $smartis;
  146.     }
  147.  
  148.     /**
  149.      * Get smartis
  150.      *
  151.      * @return Doctrine\Common\Collections\Collection
  152.      */
  153.     public function getSmartis() {
  154.         return $this->smartis;
  155.     }
  156.  
  157. }

I la entidad A sería la siguiente:
Código PHP:
Ver original
  1. <?php
  2.  
  3. namespace Axon\BackendSmartisBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\Collection;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
  9.  
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11.  
  12. /**
  13.  * @ORM\Entity
  14.  * @ORM\Table(name="smarti")
  15.  * @ORM\InheritanceType("JOINED")
  16.  * @ORM\DiscriminatorColumn(name="tipo", type="integer")
  17.  * @ORM\DiscriminatorMap({"1" = "SmartiControlNeu", "2" = "SmartiControlVent", "3" = "SmartiControlUva"})
  18.  * @ORM\Entity(repositoryClass="Axon\BackendSmartisBundle\Entity\SmartiRepository")
  19.   */
  20.  abstract class Smarti {
  21.  
  22.     /**
  23.      *  @ORM\Id
  24.      *  @ORM\Column(type="integer")
  25.      *  @ORM\GeneratedValue
  26.      */
  27.     private $id;
  28.  
  29.     /**
  30.      * @ORM\Column(type="string", length=255)
  31.      */
  32.     private $nom;
  33.    
  34.    
  35.     /**
  36.      * @ORM\Column(type="boolean")
  37.      */
  38.     private $is_public;
  39.    
  40.     /**
  41.      * @ORM\OneToOne(targetEntity="Axon\BackendEstacionsBundle\Entity\Estacio", inversedBy="smartis")
  42.      * @ORM\JoinColumn(name="estacio_id", referencedColumnName="id")
  43.      */
  44.     private $estacio;
  45.    
  46.  
  47.     /**
  48.      * Get id
  49.      *
  50.      * @return integer
  51.      */
  52.     public function getId()
  53.     {
  54.         return $this->id;
  55.     }
  56.  
  57.     /**
  58.      * Set nom
  59.      *
  60.      * @param string $nom
  61.      */
  62.     public function setNom($nom)
  63.     {
  64.         $this->nom = $nom;
  65.     }
  66.  
  67.     /**
  68.      * Get nom
  69.      *
  70.      * @return string
  71.      */
  72.     public function getNom()
  73.     {
  74.         return $this->nom;
  75.     }
  76.  
  77.     /**
  78.      * Set estacio
  79.      *
  80.      * @param Axon\BackendEstacionsBundle\Entity\Estacio $estacio
  81.      */
  82.     public function setEstacio(\Axon\BackendEstacionsBundle\Entity\Estacio $estacio)
  83.     {
  84.         $this->estacio = $estacio;
  85.     }
  86.  
  87.     /**
  88.      * Get estacio
  89.      *
  90.      * @return Axon\BackendEstacionsBundle\Entity\Estacio
  91.      */
  92.     public function getEstacio()
  93.     {
  94.         return $this->estacio;
  95.     }
  96.  
  97.     /**
  98.      * Set is_public
  99.      *
  100.      * @param boolean $isPublic
  101.      */
  102.     public function setIsPublic($isPublic)
  103.     {
  104.         $this->is_public = $isPublic;
  105.     }
  106.  
  107.     /**
  108.      * Get is_public
  109.      *
  110.      * @return boolean
  111.      */
  112.     public function getIsPublic()
  113.     {
  114.         return $this->is_public;
  115.     }
  116.    
  117.     public abstract function getTipo();
  118. }

I por ejemplo la C sería:

Código PHP:
Ver original
  1. <?php
  2.  
  3. namespace Axon\BackendSmartisBundle\Entity;
  4.  
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\Collection;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;
  9.  
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11.  
  12. /**
  13.  * @ORM\Entity
  14.  * @ORM\Table(name="smarti_control_uva")
  15.  */
  16. class SmartiControlUva extends Smarti {
  17.  
  18.     public function getTipo() {
  19.         return "uva";
  20.     }
  21. }

Luego en el controller hago lo siguiente:
Código PHP:
Ver original
  1. $a = $estacio->getSmartiByTipus("vent");
  2. $b = $estacio->getSmartiByTipus("neu");