Como ejecutar sentencia JOIN en un find() en KumbiaPHP Framework

De KumbiaPHP Framework Wiki


La sentencia JOIN en SQL permite combinar registros de dos o más tablas en una base de datos relacional. En el Lenguaje de Consultas Estructurado (SQL), hay tres tipo de JOIN: interno, externo, y cruzado.

Debemos crear las dos tablas para realizar con el find unas consultas con el JOIN

Contenido

[editar] Creamos Nuestras tablas y le agregamos registros

CREATE TABLE username(
  id int(11) NOT NULL AUTO_INCREMENT,
  username varchar(50) NOT NULL,
  usuario_id int(11) NOT NULL,
  PRIMARY KEY (id)
)
 
-- Volcar la base de datos para la tabla `username`
 
INSERT INTO username(`id`, `username`, `usuario_id`) VALUES
(1, 'lmarcano', 3),
(2, 'eperez', 1),
(3, 'jlinares', 0);
 
 
CREATE TABLE usuario(
  id int(11) NOT NULL AUTO_INCREMENT,
  nombres varchar(100) NOT NULL,
  departamento varchar(100) NOT NULL,
  PRIMARY KEY (id)
)
 
-- Volcar la base de datos para la tabla `usuario`
 
INSERT INTO usuario (`id`, `nombres`, `departamento`) VALUES
(1, 'Elena Perez', 'Administracion'),
(2, 'Carlos Beltran', 'Presidencia'),
(3, 'Luis Marcano', 'Auditoria');

[editar] Creando nuestros Modelos

Ahora creamos nuestros modelos [app]/models/username.php:

  1. <?php
  2. class Username extends ActiveRecord
  3. {
  4. }

[app]/models/usuario.php:

  1. <?php
  2. class Usuario extends ActiveRecord
  3. {    
  4.     public function getInnerJoin(){
  5.         return $this->find('columns: usuario.id, nombres, departamento, username',
  6.                            'join: inner join username on usuario.id = username.usuario_id');
  7.     }
  8.  
  9.      public function getLeftJoin(){
  10.         return $this->find('columns: usuario.id, nombres, departamento, username',
  11.                            'join: left outer join username on usuario.id = username.usuario_id');
  12.     }   
  13.  
  14.      public function getRightJoin(){
  15.         return $this->find('columns: usuario.id, nombres, departamento, username',
  16.                            'join: right outer join username on usuario.id = username.usuario_id');
  17.     }
  18. }
  19. ?>

Aquí mostramos los distintos métodos como podemos utilizar el find(), para relacionar dos o mas tablas tablas en una consulta.

[editar] Creamos nuestro Controller

[app]/controller/usuario_controller.php:

  1. <?php
  2.  
  3. class UsuarioController extends ApplicationController{
  4.  
  5.     public function index(){
  6.  
  7.     $this->inner = $this->Usuario->getInnerJoin();
  8.     $this->left = $this->Usuario->getleftJoin();
  9.     $this->right = $this->Usuario->getrightJoin();
  10.     }
  11. }
  12.  
  13. ?>

[editar] Creamos nuestra Vista

[app]/views/usuario/index.phtml:

  1. <h2>Consulta con el Inner Join</h2>
  2. <center>
  3. <table border='1'>
  4.     <tr>
  5.         <th>Username</th>
  6.         <th>Nombre del Usuario</th>
  7.         <th>Departamento</th>
  8.     </tr>
  9.     <?php foreach($inner As $i): ?>
  10.     <?php echo tr_color('#ff0000', '#fff')?>
  11.     <td><?php echo $i->username ?></td>
  12.     <td><?php echo $i->nombres ?></td>
  13.     <td><?php echo $i->departamento ?></td>
  14.     </tr>
  15.     <?php endforeach; ?>
  16. </table>
  17. <br />
  18. <h2>Consulta con el Left Outer Join</h2>
  19. <table border='1'>
  20.     <tr>
  21.         <th>Username</th>
  22.         <th>Nombre del Usuario</th>
  23.         <th>Departamento</th>
  24.     </tr>
  25.     <?php foreach($left As $i): ?>
  26.     <?php echo tr_color('#ff0000', '#fff')?>
  27.     <td><?php echo $i->username ?></td>
  28.     <td><?php echo $i->nombres ?></td>
  29.     <td><?php echo $i->departamento ?></td>
  30.     </tr>
  31.     <?php endforeach; ?>
  32. </table>
  33. <br />
  34. <h2>Consulta con el Right Outer Join</h2>
  35. <table border='1'>
  36.     <tr>
  37.         <th>Username</th>
  38.         <th>Nombre del Usuario</th>
  39.         <th>Departamento</th>
  40.     </tr>
  41.     <?php foreach($right As $i): ?>
  42.     <?php echo tr_color('#ff0000', '#fff')?>
  43.     <td><?php echo $i->username ?></td>
  44.     <td><?php echo $i->nombres ?></td>
  45.     <td><?php echo $i->departamento ?></td>
  46.     </tr>
  47.     <?php endforeach; ?>
  48. </table>
  49. </center>

[editar] Screenshots

Realizado por: Hector Rodriguez (Al3jandro)

Herramientas personales
Manual 1.0