Diferencia entre revisiones de «Como ejecutar sentencia JOIN en un find() en KumbiaPHP Framework»
De KumbiaPHP Framework Wiki
(Adaptando a la beta2) |
|||
(No se muestran 4 ediciones intermedias de 4 usuarios) | |||
Línea 1: | Línea 1: | ||
+ | {{cleanupbox | ||
+ | |image=[[Archivo:Import.png|40px]] | ||
+ | |texto ='''Este Artículo esta siendo revisado por los Kumbieros.'''<br /> | ||
+ | <span style="font-size:90%">El ejemplo presente es completamente funcional, pero su redacción debe mejorar</span> | ||
+ | }} | ||
+ | |||
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. | 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. | ||
Línea 75: | Línea 81: | ||
<?php | <?php | ||
− | class UsuarioController extends | + | class UsuarioController extends AppController{ |
public function index(){ | public function index(){ | ||
− | + | $usuario = Load::model('usuario'); | |
− | + | $this->inner = $usuario->getInnerJoin(); | |
− | + | $this->left = $usuario->getleftJoin(); | |
− | + | $this->right = $usuario->getrightJoin(); | |
} | } | ||
} | } | ||
Línea 100: | Línea 106: | ||
</tr> | </tr> | ||
<?php foreach($inner As $i): ?> | <?php foreach($inner As $i): ?> | ||
− | + | <tr> | |
− | + | <td><?php echo $i->username ?></td> | |
− | + | <td><?php echo $i->nombres ?></td> | |
− | + | <td><?php echo $i->departamento ?></td> | |
+ | </tr> | ||
<?php endforeach; ?> | <?php endforeach; ?> | ||
</table> | </table> | ||
Línea 115: | Línea 122: | ||
</tr> | </tr> | ||
<?php foreach($left As $i): ?> | <?php foreach($left As $i): ?> | ||
− | + | <tr> | |
− | + | <td><?php echo $i->username ?></td> | |
− | + | <td><?php echo $i->nombres ?></td> | |
− | + | <td><?php echo $i->departamento ?></td> | |
+ | </tr> | ||
<?php endforeach; ?> | <?php endforeach; ?> | ||
</table> | </table> | ||
Línea 130: | Línea 138: | ||
</tr> | </tr> | ||
<?php foreach($right As $i): ?> | <?php foreach($right As $i): ?> | ||
− | + | <tr> | |
− | + | <td><?php echo $i->username ?></td> | |
− | + | <td><?php echo $i->nombres ?></td> | |
− | + | <td><?php echo $i->departamento ?></td> | |
+ | </tr> | ||
<?php endforeach; ?> | <?php endforeach; ?> | ||
</table> | </table> | ||
Línea 141: | Línea 150: | ||
== Screenshots == | == Screenshots == | ||
[[Archivo:Screenshots.jpg|800px]] | [[Archivo:Screenshots.jpg|800px]] | ||
+ | [[Categoría:Tutoriales KumbiaPHP]] | ||
+ | |||
+ | Realizado por: Hector Rodriguez (Al3jandro) |
Revisión actual del 02:32 27 may 2012
{{#if:
|}}
{{#if:
|}}
Este Artículo esta siendo revisado por los Kumbieros. El ejemplo presente es completamente funcional, pero su redacción debe mejorar | {{{imageright}}} |
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
Sumario
Creamos Nuestras tablas y le agregamos registros[editar]
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');
Creando nuestros Modelos[editar]
Ahora creamos nuestros modelos [app]/models/username.php:
<?php
class Username extends ActiveRecord
{
}
[app]/models/usuario.php:
<?php
class Usuario extends ActiveRecord
{
public function getInnerJoin(){
return $this->find('columns: usuario.id, nombres, departamento, username',
'join: inner join username on usuario.id = username.usuario_id');
}
public function getLeftJoin(){
return $this->find('columns: usuario.id, nombres, departamento, username',
'join: left outer join username on usuario.id = username.usuario_id');
}
public function getRightJoin(){
return $this->find('columns: usuario.id, nombres, departamento, username',
'join: right outer join username on usuario.id = username.usuario_id');
}
}
?>
Aquí mostramos los distintos métodos como podemos utilizar el find(), para relacionar dos o mas tablas tablas en una consulta.
Creamos nuestro Controller[editar]
[app]/controller/usuario_controller.php:
<?php
class UsuarioController extends AppController{
public function index(){
$usuario = Load::model('usuario');
$this->inner = $usuario->getInnerJoin();
$this->left = $usuario->getleftJoin();
$this->right = $usuario->getrightJoin();
}
}
?>
Creamos nuestra Vista[editar]
[app]/views/usuario/index.phtml:
<h2>Consulta con el Inner Join</h2>
<center>
<table border='1'>
<tr>
<th>Username</th>
<th>Nombre del Usuario</th>
<th>Departamento</th>
</tr>
<?php foreach($inner As $i): ?>
<tr>
<td><?php echo $i->username ?></td>
<td><?php echo $i->nombres ?></td>
<td><?php echo $i->departamento ?></td>
</tr>
<?php endforeach; ?>
</table>
<br />
<h2>Consulta con el Left Outer Join</h2>
<table border='1'>
<tr>
<th>Username</th>
<th>Nombre del Usuario</th>
<th>Departamento</th>
</tr>
<?php foreach($left As $i): ?>
<tr>
<td><?php echo $i->username ?></td>
<td><?php echo $i->nombres ?></td>
<td><?php echo $i->departamento ?></td>
</tr>
<?php endforeach; ?>
</table>
<br />
<h2>Consulta con el Right Outer Join</h2>
<table border='1'>
<tr>
<th>Username</th>
<th>Nombre del Usuario</th>
<th>Departamento</th>
</tr>
<?php foreach($right As $i): ?>
<tr>
<td><?php echo $i->username ?></td>
<td><?php echo $i->nombres ?></td>
<td><?php echo $i->departamento ?></td>
</tr>
<?php endforeach; ?>
</table>
</center>
Screenshots[editar]
Realizado por: Hector Rodriguez (Al3jandro)