Ver Mensaje Individual
  #4 (permalink)  
Antiguo 16/03/2011, 15:11
Avatar de GatorV
GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 17 años, 11 meses
Puntos: 2135
Respuesta: BD Relacional y objetos

Pues te digo depende mucho ya de tu sistema, lo ideal es que tuvieras un ORM que manejara esas cosas por ti, que fuera algo así:
Código PHP:
Ver original
  1. class Noticia
  2. {
  3.     private $_idNoticia;
  4.     private $_Categoria;
  5.    
  6.     public function __construct($nIdNoticia = 0, Categoria $Categoria = null)
  7.     {
  8.         $this->_idNoticia = $nIdNoticia;
  9.         $this->_Categoria = $Categoria;
  10.     }
  11.    
  12.     public function setCategoria(Categoria $Categoria)
  13.     {
  14.         $this->_Categoria = $Categoria;
  15.        
  16.         return $this;
  17.     }
  18.    
  19.     public function getCategoria()
  20.     {
  21.         return $this->_Categoria;
  22.     }
  23.    
  24.     public function setIdNoticia($nIdNoticia)
  25.     {
  26.         $this->_idNoticia = $nIdNoticia;
  27.        
  28.         return $this;
  29.     }
  30.    
  31.     public function getIdNoticia($nIdNoticia)
  32.     {
  33.         return $this->_idNoticia;
  34.     }
  35. }
  36.  
  37. class Categoria
  38. {
  39.     private $_idCategoria;
  40.     private $_categoria;
  41.    
  42.     public function __construct($nIdCategoria = 0, $sCategoria) {
  43.         $this->_idCategoria = $nIdCategoria;
  44.         $this->_categoria = $sCategoria;
  45.     }
  46.    
  47.     public function setCategoria($sCategoria)
  48.     {
  49.         $this->_categoria = $sCategoria;
  50.        
  51.         return $this;
  52.     }
  53.    
  54.     public function getCategoria()
  55.     {
  56.         return $this->_categoria;
  57.     }
  58.    
  59.     public function setIdCategoria($nIdCategoria)
  60.     {
  61.         $this->_idCategoria = $nIdCategoria;
  62.        
  63.         return $this;
  64.     }
  65.    
  66.     public function getIdCategoria()
  67.     {
  68.         return $this->_idCategoria;
  69.     }
  70.    
  71.     public function __toString()
  72.     {
  73.         return $this->getCategoria();
  74.     }
  75. }

Lo ideal es que tu capa de persistencia a la hora de hacer una consulta te traiga tu objeto listo, es decir internamente hace un JOIN con la tabla y trae la categoría relacionada y la inyecta a tu objeto, y ya en tu objeto solo haces un getCategoria() para obtener la categoría, así lo mantienes independiente y no metes código de proceso/persistencia en tus objetos.

Un ejemplo es algo así:
Código PHP:
Ver original
  1. public function getNoticiaById($nIdNoticia)
  2. {
  3.     $DB = $this->getDb();
  4.     $sQuery = "SELECT * FROM noticias AS n INNER JOIN categorias AS c ON n.id_categoria = c.id_categoria WHERE n.id_noticia=:id_noticia";
  5.     $Stmt = $DB->prepare($sQuery);
  6.     $Stmt->execute(array(':id_noticia' => $nIdNoticia));
  7.    
  8.     $aData = $Stmt->fetch(PDO::FETCH_ASSOC);
  9.     $Noticia = new Noticia();
  10.     $Noticia->setIdNoticia($aData['id_noticia'])
  11.             ->setCategoria(new Categoria($aData['id_categoria'], $aData['categoria']));
  12.            
  13.     return $Noticia;
  14. }

Claro todo eso lo puedes automatizar aún más y de eso se encarga un ORM de mapear tus objetos simples, y persistirlos de una manera automatizada usando un mapeo objeto relacional.

Espero con este ejemplo tengas una idea más clara de como hacerlo.

Saludos.