Working with the database to get modeldata

There are two ways to access the data:
You can use the model objects or you can get the databse with bpfw_getDb() and send queries directly.

Use the Models

All models are derived from the class BpfwModel that has a lot of methods for easy data access and conversion.
You can create a model of a specific type by calling createModelByName(“modelname”);.

Examples of the most common cases:

Get userdata of one user:

$userId = 3;
$usermodel = bpfw_createModelByName("user"); //new UserModel();
$user = $usermodel->GetEntry($userId);

// put all userdata into an array
$variables = array();
foreach($user->GetKeyValueArray(false) as $key=>$value){
    $variables["user".".".$key] = $value;

Multiselect: Iterate through all adminusers:

$usermodel = bpfw_createModelByName("user"); //new UserModel();

// select all users that are at least admins
$users = $usermodel->GetEntries("userRank <= '" . USERTYPE_ADMIN . "'");

// put all usernames into an array with the userid as key
$variables = array();
foreach(users as $userId=>$userdata){    
    $variables[$userId] = $userdata->lastname.", ".$userdata->firstname;

Multiselect: But this time get an array instead of objects

$usermodel = bpfw_createModelByName("user"); //new UserModel();

// select all admins
$users = $usermodel->dbSelectAllAndCreateObjectArray("userrank < '" . USERTYPE_ADMIN . "'");

// or you could use plain SQL if you need full control
$users = $usermodel->dbSelectAllFromPlainSqlAndCreateObjectArray("select * from users where userrank < '" . USERTYPE_ADMIN . "'");

// put all usernames into an array with the userid as key
$variables = array();
foreach(users as $userId=>$userdata){    
    $variables[$userId] = $userdata["lastname"].", ".$userdata["firstname"];

Delete a user:

$userId = 123;
$usermodel = bpfw_createModelByName("user"); //new UserModel();

// delete by key

// or delete all users with name John
$userModel->DbDeleteByWhere("firstname = 'John'");

Insert a user:

$usermodel = bpfw_createModelByName("user"); //new UserModel();
$userModel->DbInsert(array("firstname"=>"John", "lastname"=>"Doe", "userrank"=>USERTYPE_ADMIN));

Update a user:

$userId = 123;
$usermodel = bpfw_createModelByName("user"); //new UserModel();
$userModel->DbUpdate(array("userId"=>$userId, "firstname"=>"John", "lastname"=>"Doe", "userrank"=>USERTYPE_ADMIN));

// you could also use InsertOrUpdate if you are not sure if the entry is existing:
$userModel->DbInsertOrUpdate(array("userId"=>$userId, "firstname"=>"John", "lastname"=>"Doe", "userrank"=>USERTYPE_ADMIN));

// another Option is InsertOrIgnore to prevent errors
$userModel->DbInsertOrIgnore(array("userId"=>$userId, "firstname"=>"John", "lastname"=>"Doe", "userrank"=>USERTYPE_ADMIN));

Select by Foreign Key:

This is useful, to get all child entries. For example all addresses of an user

$userId = 123;
$useraddressmodel = bpfw_createModelByName("useraddress"); //new UseraddressModel();
$entries = $userModel->DbSelectByForeignKey(userId); // array of UseraddressModel with all addresses of the user

Use the Database directly

You can get the database class from anywhere using $db = bpfw_getDb();
The database is basically a utility class for php mysqli to make it work a bit better with the BPFW structure.

Using it should only be required when doing complex sql statements that would be not possible or slow otherwise. Or if you want to access sql features like deleting tables or creating a database.
All the methods can be found in bpfw/core/db/

The most commonly used are:

// Plain query that returns the mysqli result and will not be processed in any way
$anySql = "TRUNCATE TABLE 'example'";

// second parameter is the value that will be the keyvalue in the array. Can be left empty
$dataarray = bpfw_getDb()->makeSelect("select * from user left join moreuserdata on user.userId = moreuserdata.moreuserId", "userId");
// variations: makeSelectSingleOrNullBySql makeSelectSingleOrNull makeSelectSingleOrNullByWhere makeSelectAll makeSelectValue

// INSERT and/or Update
bpfw_getDb()->makeInsert(array("firstname"=>"John", "lastname"=>"Doe"), "user");
bpfw_getDb()->makeInsertOrUpdate(array("userId"=>123, "firstname"=>"John", "lastname"=>"Doe"), "user");
bpfw_getDb()->makeUpdate(array("userId"=>123, "firstname"=>"John", "lastname"=>"Doe"), "user");

bpfw_getDb()->makeDelete("user", 123);

// INFO about a specific table (Describe)

// Show tables -> array about all tables existing in database

WordPress Cookie Plugin by Real Cookie Banner