|  Download php.dbqueryWith this library you can update multiple interrelated tables at the same time without writing separated SQL statements. InstallationDownload the project: git clone https://github.com/soloproyectos/php.dbquery
 and copy the classesfolder in your preferred location (optionally, rename it). Finally, copy and paste the following PHP code: require_once "< YOUR PREFERRED LOCATION >/classes/autoload.php";
use com\soloproyectos\common\db\DbConnector;
use com\soloproyectos\common\db\DbTable;
 And that's all. You are ready to use this library. Important methods
`DbTable::get($colName)`: Gets a column value
`DbTable::set($colName, $colValue)`: Sets a column value
`DbTable::insert()`: Inserts a new record
`DbTable::update()`: Updates a record
`DbTable::delete()`: Deletes a record
`DbTable::serOrder($order)`: sets an order
`DbTable::addFilter($filter)`: adds a filter
`DbTable::refresh()`: refreshes the columns
 IntroductionLet's say that we have a contenttable and several tables that depend on it. That is: 
 In this scenario, we can update (or eventually insert) records in the tables content,image,videoandfileat the same time, as if it were a single table. In the following example we are updating a record of the tablecontentand updating (or inserting) the corresponding records of the tablesimage,videoandfile: // connects to the database
$db = new DbConnector("database", "username", "password");
// updates the record "content.id = 101"
$t = new DbTable($db, "content", 101);
$t->set("description", "Record description...");
$t->set("image.path", "/path/to/image.jpg");
$t->set("video.path", "/path/to/video.mp4");
$t->set("file.path", "/path/to/file.pdf");
$t->update();
 Let's consider a more complex example, in which the table imagedepends on the tablevideowhich in turn depends on the tablecontent. That is: 
 In the following example, the expressión image[video.image_id].pathmeans that the tableimagedepends on the tablevideothrough the columnimage_id: // connects to the database
$db = new DbConnector("database", "username", "password");
// updates the record "content.id = 101"
$t = new DbTable($db, "content", 101);
$t->set("description", "Record description...");
$t->set("video.path", "/path/to/video.mp4");
$t->set("image[video.image_id].path", "/path/to/image.jpg");
$t->update();
 AssumptionsBy default, DbTableassumes that each table has a primary key calledid(although this name can be changed in the constructor). Also it assumes thattable1depends ontable0through the columntable1_id. So these expressions are equivalents: // these three commands are equivalents
echo $t->get("image.path");
echo $t->get("image[image_id].path");
echo $t->get("image[id = image_id].path");
// these three commands are equivalents as well 
echo $t->get("image[video.image_id].path");
echo $t->get("image[id = video.image_id].path");
echo $t->get("image[id = video[id = video_id].image_id].path");
 The general format to access a column of a dependent table is as follows: table1[col0 = col1].column
 In case of col0isidandcol1istable1_idwe can write: table1.column
 Some other valids expressions are: table1[col1].column
table1[col0 = col1].column
table1[col0 = table2.col1].column
table1[col0 = table2[col2 = col3].col4].column
etc...
 ExamplesSee more examples at test.php |