Version 2, last updated by phillipo at Aug 31 19:04 2007 UTC

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

  1. Dim nit As String = ‘808111827-2’
  2. Dim Query As String = "select count(*) from clientes where nit = ‘" & nit & “’”
  3. Dim command As New System.Data.OleDb.OleDbCommand(Query, con)
  4. Dim cnt As Int32 = 0
  5. Try
  6. con.Open()
  7. Dim cursor As OleDb.OleDbDataReader = command.ExecuteReader()
  8. cursor.Read()
  9. If cursor.GetInt32() = 0 Then
  10. new System.Data.OleDb.OleDbCommand(“insert into clientes values(‘” & nit & ”’,
  11. ‘EMPRESA DE TELECOMUNICACIONES ETB’)”)
  12. Else
  13. new System.Data.OleDb.OleDbCommand(“update clientes set razon_social
  14. = ‘EMPRESA DE TELECOMUNICACIONES ETB’ where nit = ‘” & nit & ”’”)
  15. End If
  16. cursor.Close()
  17. Catch dbError As OleDBException
  18. Dim i As Integer
  19. For i = 0 To dbError.Errors.Count - 1
  20. MessageBox.Show(dbError.Errors(i).Message + ControlChars.Cr)
  21. Next i
  22. End Try

Ejemplo

  1. ?php
  2. $nit = ‘808111827-2’ ;
  3. $q = mysql_query(“select count(*) from clientes where nit = ‘$nit”);
  4. $fila = mysql_fetch_array($q);
  5. if($fila[0]==0){
  6. mysql_query(“insert into clientes values (‘$nit’, ‘EMPRESA DE TELECOMUNICACIONES ETB’”);
  7. 7. } else {
  8. 8. mysql_query (“update clientes set razon_social = ‘EMPRESA DE TELECOMUNICACIONES ETB’
  9. 9. where nit = ‘$nit’”);
  10. 10. }
  11. 11. ?>

El mapeo Objeto-Relacional soluciona esto y proporciona un alto potencial para hacer las aplicaciones más fáciles de mantener.



Regresar al indice