I have worked with some Laravel projects in the past. Creating a table and using them in a controller is pretty easy. However, I mostly work with pure PHP. Most of them are not class object based. I want to write code to work similar to the Laravel model system. I think it will be helpful to generate a DB table by hand.
I need to achieve the following:
Create a table manually and make class and the same name as the table. The class should able to get all the table info (fields, id, etc.) using the class name when creating the object (not assigning table name).
The class should able to get table fields dynamically and must have the ability to save data using Object->save()
, and get a row using id: $obj=Object::find(2);
class drivers extends Model
{
//'''''
}
$driver = new $driver();
$diver->name="foo";
$diver->age=19;
$diver->save();
//------------
$diver=Driver::find(3);
$diver->delete();
I want to do the above in in pure PHP but I am unable to manage. Can anyone help? I really appreciate it, thank you.
I tried it like this:
<?php
class Model
{
protected function __construct() {
public static function find($id){
$tablename=get_somehow_extented_classname;
//....
return $datarow;
}
public static function delete($id){
$tablename=get_somehow_extented_classname;
//....
$result=query("delete from $tablename where id=$id");
return $result;
}
}
use Model; // core model (I want this)
class drivers extends Model
{
// nothing lot of things here
}