Version 2, last updated by phillipo at Aug 31 19:04 2007 UTC
22-La-Vida-sin-ActiveRecord
22. La Vida sin ActiveRecord
Anteriormente las aplicaciones eran bolas de código, mezclaban SQL, CSS, HTML, PHP, JavaScript, etc. Al menos los que buscábamos un orden mínimo conseguíamos separar algunas cosas pero el SQL y el PHP siempre iban ligados así que hacer cambios en el modelo relacional podría ser fatal ya que dañaba muchas partes de la aplicación que ya estaban probadas.
Este no es un problema exclusivo de PHP, también lo tienen otros lenguajes ó estilos de programación que no reparan en esto. Por ejemplo en Visual Basic .NET y en PHP tradicional:
Ejemplo
- Dim nit As String = ‘808111827-2’
- Dim Query As String = "select count(*) from clientes where nit = ‘" & nit & “’”
- Dim command As New System.Data.OleDb.OleDbCommand(Query, con)
- Dim cnt As Int32 = 0
- Try
- con.Open()
- Dim cursor As OleDb.OleDbDataReader = command.ExecuteReader()
- cursor.Read()
- If cursor.GetInt32() = 0 Then
- new System.Data.OleDb.OleDbCommand(“insert into clientes values(‘” & nit & ”’,
- ‘EMPRESA DE TELECOMUNICACIONES ETB’)”)
- Else
- new System.Data.OleDb.OleDbCommand(“update clientes set razon_social
- = ‘EMPRESA DE TELECOMUNICACIONES ETB’ where nit = ‘” & nit & ”’”)
- End If
- cursor.Close()
- Catch dbError As OleDBException
- Dim i As Integer
- For i = 0 To dbError.Errors.Count - 1
- MessageBox.Show(dbError.Errors(i).Message + ControlChars.Cr)
- Next i
- End Try
Ejemplo
- ?php
- $nit = ‘808111827-2’ ;
- $q = mysql_query(“select count(*) from clientes where nit = ‘$nit”);
- $fila = mysql_fetch_array($q);
- if($fila[0]==0){
- mysql_query(“insert into clientes values (‘$nit’, ‘EMPRESA DE TELECOMUNICACIONES ETB’”);
- 7. } else {
- 8. mysql_query (“update clientes set razon_social = ‘EMPRESA DE TELECOMUNICACIONES ETB’
- 9. where nit = ‘$nit’”);
- 10. }
- 11. ?>
El mapeo Objeto-Relacional soluciona esto y proporciona un alto potencial para hacer las aplicaciones más fáciles de mantener.
