When database grows!
Today I was quite frustrated with the size of database I created yesterday. It took a whole of the day to design database terms. Then it took another whole day to create the necessary fields and tables. When I saw it today morning, looking at the code, I had to do all the copying and pasting of the common database methods in the php classes. (Classes are the means to create objects of same properties. Each object has its own collection of variables.) Looking at the whole 3 page list of tables, I really was considering an intensive task for the avalanche I created today.Ideas happen to the lazy guys
There is a saying "Why we progressed in the society today is because we are getting more and more lazy." Maybe it is not a good thing to live a healthy life but believe me or not; we are always trying to find comfort in our lifestyle --- finding a way to live a lazy life. Well, the world behaves in a different way. For a person thinking to live a lazy life, another wise lazy guy helps him to trade the progression. For example, I may spend a whole 10 years to develop a wooden car that runs out of solar power and if another guy is spending his 10 years to develop an automatic plowing machine --able to do weeding as well as loosening the soil then after our feats, our results will bring a trigger of 20 years to life of people in just 5 years.
Speaking of the part
Well, enough of the nonsense and lets take code. I collected common methods in a separate file. Then I inquired the database system to give me list of table and ask each table the description of variables it held in its definition. Then the principle was simple. I just had to use arrays to load tables and use each item to load arrays of fields. It then was easy running the combined effort in generating class files in php. (Take each variable instance and loop it to create new file each time it encountered a table item). Now I can easily do a CRUD method using the classes I created. Well, it did saved me a time to cross consider whether I was right about the design I used. But also now with the code developed I can easily manage more database classes by using it.
Look at the code
Maybe you are wondering what I was really talking about! Well, you may want to grasp the idea. And here it is:
$file1 = fopen("includes/class/myclass.php","w");
$file2 = fopen("includes/class/data.txt", "r");
$code = fread($file2,filesize("includes". DS. "class". DS. "data.txt")) ;
fclose($file2);
// echo getcwd();
// echo filesize("includes". DS. "class". DS. "data.txt");
function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set)) {
$object_array[] = $row[0];
}
return $object_array;
}
$myarray = find_by_sql('show tables');
print_r($myarray);
foreach ($myarray as $myclass) {
$file1 = fopen("includes/class/". $myclass . ".php","w");
$clas = "class ". $myclass ."{";
// echo $myclass ."
";
";
// print_r( find_by_sql("desc ".$myclass));
// echo "
";
";
$var = '';
unset($w1);
$w1 = find_by_sql("desc ".$myclass);
foreach ($w1 as $key => $value) {
$var .= "
public $".$value .";";
}
// echo $var . "
";
";
$tb = "protected static db_fields = array('". join("', '", array_values($w1)) . "');";
// echo "
";
";
$output = "
$clas . "
" . $tb . "
" . $var . "
".
$code .
"} \n" .
" ?>" ;
fwrite($file1, $output);
fclose($file1);
}