BingoSql
Github Repository : https://github.com/Shyam-Achuthan/BingoSql
Creating Models
- All models should extend BingoSql Class
class User_details extends BingoSqlModel { }
- Default primary key will be chosen as ‘id’ otherwise you should specify the primary key field
class User_details extends BingoSqlModel { protected $key='Id'; }
- Model name should be same as the table name otherwise you should add protected $table=”tablename” to the class
class User extends BingoSqlModel { protected $table='user_details'; protected $key='Id'; }
- Defining relations – As of now BingoSql support two kind of relations, belongs_to and has_many
While defining the relation your should represent the foreign filed and native primary key field.
Here in the example a user->belongs_to->groups and a groups->has_many->usersclass User extends BingoSqlModel { protected $table='user_details'; protected $key='Id'; protected $belongs_to = array('groups'=>'group_id|Id'); } // protected $belongs_to =array('table_name'=>'foreign_field_name|native_primary_key_for_table_which_it_belong_to'); class Groups extends BingoSqlModel { protected $table='groups'; protected $key='Id'; protected $has_many = array('user_details'=>'Id|group_id'); } // protected $has_many =array('table_name'=>'native_primary_key|foreign_key_field_at_related_table');
- Models can be in separate php class files in the models directory or any other single directory.
Code Samples
include('../bootstrap.php'); // Creating a new row in a table $newuser = new User(); $newuser->email = 'new@gmail.com'; $newuser->password = md5('password'); $newuser->fullname = 'My Fullname'; $newuser->groups = 1; $newuser->save(); //Finding a user by id $existinguser = new User(); $existinguser->find(204); //accessing fields of that specific user echo "Fullname: " . $existinguser->fullname . ' '; echo "Email: " . $existinguser->email . ' '; //Updating the found and existing record $existinguser->email="updatedemail@gmail.com"; $existinguser->save(); //Accessing a relation to groups table assuming groups table have a field group_name echo "User belongs to Group: " . $existinguser->groups->group_name; //To find all relations to the group $grp = new Groups(); $grp->find(1); echo "There are " . count($grp->user_details) . " users in this group "; foreach ($grp->user_details as $user) { echo "Fullname: " . $existinguser->fullname . ' '; echo "Email: " . $existinguser->email . ' '; echo '
‘; }