1

Downloaded a few open source php scripts such as Wordpress and Moodle to study the way they structure there code etc. However when looking through both of these scripts I am always unable to find there database queries (select, insert, update)? Why are they so hidden and how do I find them?

ritch
  • 1,760
  • 14
  • 37
  • 65

4 Answers4

4

They may actually be generated through an ORM layer. Which is an object that generates a query through the object methods.

datasage
  • 19,153
  • 2
  • 48
  • 54
  • Interesting, how difficult is to learn this approach and how long would it take? – ritch Jun 10 '11 at 16:01
  • Alot of frameworks already have built in ORM layers. ZF has Zend_Db_Select. Thats probably a good place to start. – datasage Jun 10 '11 at 16:04
1

Take a look at /wp-includes/user.php in Wordpress for specific details of the implementation. You will see SQL statements as well as the code that processes them, like this:

$user_nicename_check = $wpdb->get_var( $wpdb->prepare("SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1" , $user_nicename, $user_login));
George Cummins
  • 28,485
  • 8
  • 71
  • 90
1

In WordPress, they're in wp-db.php, funnily enough :) "Hiding" the low-level database calls from the majority of the code isolates the "business logic" from the database as much as possible.

This makes it easier to make changes in one without having to re-code the other, or to support multiple database (or other storage) back-ends without having to litter your code with checks to see which kind of DB you're using.

If you really want to figure out what's going on in a large open-source project, especially an OO one, it's probably worth using an editor/IDE which is capable of analysing code and jumping to definitions, such as NetBeans. This often makes it easier to follow the code through when you're reading without having to figure out which class in which file is being called (especially when PHP autoloading is happening.)

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
0

There not hidden, there just written in an OOP way!

fire
  • 21,383
  • 17
  • 79
  • 114