-
Notifications
You must be signed in to change notification settings - Fork 0
Model
Bastien Crettenand edited this page Aug 28, 2019
·
10 revisions
- An already structured database must exist
- A working configuration file
Example 1 :
use slsql\Model\Model;
class Users extends Model
{
public $name, $psw, $email, $id;
}
Example 2 :
use slsql\Model\Model;
class Users extends Model
{
public $name, $firstName, $psw, $email, $id;
public function getInitials(){
$initials = substr($this->firstName, 0, 3);
return $initials .= substr($this->name, 0, 3);
}
}
Example 1 :
$user = new Users();
$user->name = "CreBast";
$user->psw = "123";
$user->email = "[email protected]";
$user->save();
Example 2 :
$user = Users::get()->firstOrDefault()
Save object on database :
$user = new Users();
$user->name = "Bastien"; // Actually not on DB
$user->save(); // Now yes
Get list of 'Users' with condition :
$user = Users::get('id = ? or id = ?', array(1, 2));
// Return ListModels() with users with ids 1 and 2 (if exist)
Remove object on database :
$user = Users::get('id = ?', array(1))->first();
$user->remove()
// Remove user with id 1
Return all ids on one list :
$user_ids = Users::ids();
// Return : array(1, 2, 3, ...)
Return all values of field on list :
$all_names = Users::all("name");
// Return : array(Bastien, José, Bastien, ...)
Return all values of field on list (distinct) :
$all_names = Users::allDistinct("name");
// Return : array(Bastien, José, ...)
Count number of objects :
$number = Users::count();
// Return : number
Count with condition :
$number = Users::countWhere('name = ?', array('Bastien'));
// Return : number
Add new elements on ListModels() :
$listModels = new ListModels;
$listModels->add(new Users);
Return one list with all datas :
$users = Users::get()->all();
// Return all users
Return the first user returned. With default value if nothing :
$user = Users::get()->firstOrDefault(false);
// Return the first user returned. If nothing : false
Return the first user returned :
$user = Users::get()->first();
// Return the first user returned. If nothing : null
Return the last user returned. With default value if nothing :
$user = Users::get()->lastOrDefault(false);
// Return the last user returned. If nothing : false
Return the last user returned :
$user = Users::get()->last();
// Return the last user returned. If nothing : null