Models - ace411/Bingo-Framework GitHub Wiki

#Interacting with models

Models make it possible to interact with the database. The database used by the framework is MySQL to which the back-end connects via PDO. Set the database connection variables in the Config script in the App directory.

###Configuring database variables

Database information can be changed in the Config.php file in the App directory to reflect the breakdown shown below:

namespace App;

class Config
{
	const DB_USER = '{$USERNAME}';

	const DB_HOST = 'localhost';

	const DB_PASS = '{$PASSWORD}';

	const DB_NAME = '{$DATABASENAME}';

	const SHOW_ERRORS = true;

	const DEP_ROOT = 'packages';
}

###Creating models

Models are best defined by the purpose they are intended for. Since Bingo is predominantly object-oriented in design, the names of the Model classes and the sub methods should be related to the context in which the scripts will be applied. For instance; a Posts model can have the methods getArticles() and getAuthors() to return the article and author information stored in a certain table in the database.

Below is an example of a model:

namespace App\Models;
use \Core\Model;
class Posts 
{
	public function getArticles()
	{
		$connect = Model::connectTo();
		$connect->sqlQuery("
			SELECT post_id, post_title 
			FROM posts
		");
		$connect->executeQuery();
		$posts = $connect->resultSet();
		$posts = $connect->printJson($posts); //optional
		return $posts;
	}

        public function getAuthors()
	{
		$connect = Model::connectTo();
		$connect->sqlQuery("
			SELECT post_id, post_author 
			FROM posts
		");
		$connect->executeQuery();
		$authors = $connect->resultSet();
		$authors = $connect->printJson($posts); //optional
		return $authors;
	}

}