Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Novas APIs no Drupal 7     José San Martin
Database Layer
Banco de dados• “Query builders” paraconsultas INSERT,  UPDATE, DELETE, MERGE, e SELECT.• Suporteparareplicação master/sla...
Database layer• Camada de abstração• Permiteouso com diversosservidores de  BD• Baseadonabiblioteca PDO do PHP• Funçõesesp...
select com db_queryDrupal 6:$query = db_query(SELECT title FROM {node});while ($n = db_fetch_object($query)) {  $titles[] ...
select com db_queryDrupal 7:$results = db_query(SELECT title FROM  {node});foreach($results as $n) {  $titles[] = $n->titl...
select com db_queryDrupal 6:$query = db_query(   SELECT title FROM {node}   WHERE uid = %d, 1);
select com db_queryDrupal 7:$results = db_query(   SELECT title FROM {node}    WHERE uid = :uid’,array(:uid => 1));foreach...
select com db_queryDrupal 7:$results = db_query(   SELECT title FROM {node}    WHERE title = :title,array(:title => Foz));...
select com db_queryDrupal 7:$results = db_query(   SELECT title FROM {node}    WHERE title LIKE :title,array(:title => Foz...
query objects• abstraçãoparadescreverumaconsultaaoba  nco de dados• Passos:  – Crie a query (db_select, db_insert, db_dele...
insertDrupal 6:$query = db_query(   "INSERT INTO {role}    (name, weight)    VALUES (%s, %d)",   Admin, -10);
insertDrupal 7:$fields = array(   name => Admin’,   weight => 0,);db_insert(role)->fields($fields)->execute();            ...
ouusando a API do Drupal…Drupal 6 eDrupal 7:$fields = array(   name => Admin’,   weight => 0,);drupal_write_record(role, $...
multi-insert$values = array(   array(      title => Example,      uid => 1,      created => REQUEST_TIME,   ),   array(   ...
multi-insert$query = db_insert(node)->fields(array(title, uid, created));foreach ($values as $record) {  $query->values($r...
db_delete$num_deleted = db_delete(node)  ->condition(nid, 5)  ->execute();           O método execute() vairetornar a quan...
db_select$results = db_select(node, n)  ->fields(n, array(title))  ->condition(n.uid, 0, <>)  ->execute();foreach ($result...
conditions->condition(n.uid, 0, =)->condition(n.uid, 0) //igual a linhaacima->condition(n.uid, 0, <>)->condition(n.nid, 30...
db_select: joins$query = db_select(node, n)  ->fields(n, array(title))  ->condition(n.uid, 0, <>);$alias = $query->join(us...
db_selectoudb_query?• db_queryémaiseficiente• db_selectémaisabstratoepermitequery_alt  er
hook_query_alter• Permitealteraruma query  definidaporoutromódulo• Baseia-se na tag da $query
hook_query_alter$query->addTag(node_access);function  mymodule_query_alter(QueryAlterableInterface$  query) {    if ($quer...
Files API
Novidades• Arquivospassam a ser cidadãos de  primeiraclasse, ouseja, entidades• Stream wrappers• Managed Files vs. Unmanag...
Stream Wrappers• ProvidospeloDrupal  – public://arquivo.txt  – private://outro-arquivo.txt  – temporary://mais-um-arquivo....
copiar um arquivo$file = file_load(1);   // fidif (file_prepare_directory("public://pasta") {  $copia =file_copy($file, pu...
exibir a url$file = file_load(1);print $file->uri; // public://file.png$url = file_create_url($file->uri);print $url;//   ...
maisinformações: drupal.org/developing/apiJosé San Martinjose@chuva-inc.comhttp://chuva-inc.com.br
DrupalCamp Foz - Novas APIs Drupal 7
DrupalCamp Foz - Novas APIs Drupal 7
Upcoming SlideShare
Loading in …5
×

DrupalCamp Foz - Novas APIs Drupal 7

2,396 views

Published on

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

DrupalCamp Foz - Novas APIs Drupal 7

  1. 1. Novas APIs no Drupal 7 José San Martin
  2. 2. Database Layer
  3. 3. Banco de dados• “Query builders” paraconsultas INSERT, UPDATE, DELETE, MERGE, e SELECT.• Suporteparareplicação master/slave, transações, consultas multi-insert,delayed inserts, entre outros.• Suporte a SQLite.• Uso do engine InnoDBporpadrão, aoinvés de MyISAM, emMySQL, quandodisponível
  4. 4. Database layer• Camada de abstração• Permiteouso com diversosservidores de BD• Baseadonabiblioteca PDO do PHP• FunçõesespeciaisparaconstruirSELECTs, INSERTs, UPDATEs, DELETEseMERGEs
  5. 5. select com db_queryDrupal 6:$query = db_query(SELECT title FROM {node});while ($n = db_fetch_object($query)) { $titles[] = $n->title;}
  6. 6. select com db_queryDrupal 7:$results = db_query(SELECT title FROM {node});foreach($results as $n) { $titles[] = $n->title;}
  7. 7. select com db_queryDrupal 6:$query = db_query( SELECT title FROM {node} WHERE uid = %d, 1);
  8. 8. select com db_queryDrupal 7:$results = db_query( SELECT title FROM {node} WHERE uid = :uid’,array(:uid => 1));foreach($results as $n) { $titles[] = $n->title;}
  9. 9. select com db_queryDrupal 7:$results = db_query( SELECT title FROM {node} WHERE title = :title,array(:title => Foz));foreach($results as $n) { $titles[] = $n->title;}
  10. 10. select com db_queryDrupal 7:$results = db_query( SELECT title FROM {node} WHERE title LIKE :title,array(:title => Foz%));foreach($results as $n) { $titles[] = $n->title;}
  11. 11. query objects• abstraçãoparadescreverumaconsultaaoba nco de dados• Passos: – Crie a query (db_select, db_insert, db_delete) – Adicionecamposoucondições – ->execute();
  12. 12. insertDrupal 6:$query = db_query( "INSERT INTO {role} (name, weight) VALUES (%s, %d)", Admin, -10);
  13. 13. insertDrupal 7:$fields = array( name => Admin’, weight => 0,);db_insert(role)->fields($fields)->execute(); O método execute() vairetornaro campo auto_increment (nessecaso, rid)
  14. 14. ouusando a API do Drupal…Drupal 6 eDrupal 7:$fields = array( name => Admin’, weight => 0,);drupal_write_record(role, $fields);
  15. 15. multi-insert$values = array( array( title => Example, uid => 1, created => REQUEST_TIME, ), array( title => Example 2, uid => 1, created => REQUEST_TIME, ),);
  16. 16. multi-insert$query = db_insert(node)->fields(array(title, uid, created));foreach ($values as $record) { $query->values($record);}$query->execute(); O método execute() nãovairetornar nada
  17. 17. db_delete$num_deleted = db_delete(node) ->condition(nid, 5) ->execute(); O método execute() vairetornar a quantidade de linhasapagadas
  18. 18. db_select$results = db_select(node, n) ->fields(n, array(title)) ->condition(n.uid, 0, <>) ->execute();foreach ($results as $n) { $titles[] = $n->title;}
  19. 19. conditions->condition(n.uid, 0, =)->condition(n.uid, 0) //igual a linhaacima->condition(n.uid, 0, <>)->condition(n.nid, 300, >=)->condition(n.uid, array(1,2,3), IN)->condition(n.title, db_like($st).%, LIKE)->condition(n.nid, array(10,20), BETWEEN)->isNull(nomedocampo)->isNotNull(nomedocampo)
  20. 20. db_select: joins$query = db_select(node, n) ->fields(n, array(title)) ->condition(n.uid, 0, <>);$alias = $query->join(users, u, n.uid = u.uid);$results = $query->fields(u, array(name)) ->execute();
  21. 21. db_selectoudb_query?• db_queryémaiseficiente• db_selectémaisabstratoepermitequery_alt er
  22. 22. hook_query_alter• Permitealteraruma query definidaporoutromódulo• Baseia-se na tag da $query
  23. 23. hook_query_alter$query->addTag(node_access);function mymodule_query_alter(QueryAlterableInterface$ query) { if ($query->hasTag(nomedatag)) { $query->range(0, 2); $query->range(0, 2); }}
  24. 24. Files API
  25. 25. Novidades• Arquivospassam a ser cidadãos de primeiraclasse, ouseja, entidades• Stream wrappers• Managed Files vs. Unmanaged Files – tabelafiles_managed – funçõesfile_unmanaged_* paratodas as operaçõesquenãosalvem dados natabela
  26. 26. Stream Wrappers• ProvidospeloDrupal – public://arquivo.txt – private://outro-arquivo.txt – temporary://mais-um-arquivo.txt• Providospormóduloscustomizados: – youtube://NIfl2o44zb0
  27. 27. copiar um arquivo$file = file_load(1); // fidif (file_prepare_directory("public://pasta") { $copia =file_copy($file, public://pasta/arq.jpg); // $copiaestásalvanatabelafile_manageddelete_file($copia);}
  28. 28. exibir a url$file = file_load(1);print $file->uri; // public://file.png$url = file_create_url($file->uri);print $url;// http://localhost/foz/sites/default/files/file
  29. 29. maisinformações: drupal.org/developing/apiJosé San Martinjose@chuva-inc.comhttp://chuva-inc.com.br

×