PHP code example of basecode / querymodel
1. Go to this page and download the library: Download basecode/querymodel library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
basecode / querymodel example snippets
define("DB_CONFIG", [
"driver" => "mysql",
"host" => "localhost",
"name" => "querymodel",
"user" => "root",
"password" => "",
"options" => [
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8",
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ
]
]);
date_default_timezone_set("America/Sao_Paulo");
define("DS", DIRECTORY_SEPARATOR);
el;
Class User extends QueryModel
{
protected $table = "users";
// protected $primary = "id";
protected $ return $this->select("users.name, posts.title")
->join("INNER JOIN posts ON users.id = posts.id_user")
->where("users.id = :id")
->params(["id" => $this->id])
->execute();
}
public function usersPosts()
{
return $this->select("users.id, users.name, posts.title")
->join("INNER JOIN posts ON users.id = posts.id_user")
->execute();
}
}
Class Post extends QueryModel
{
protected $table = "posts";
// protected $primary = "id";
protected $
$user->findById(2)->fill();
print_r($user);
// select by id and return object stdClass with the data
print_r($user->findById(2)->first());
// select by id and update
$user->findById(1)->fill();
$user->email = "[email protected] ";
$user->save();
// select by condition without parameters | not recommended
$result = $user->findBy("email = '[email protected] '")->execute();
print_r($result);
// select by condition with parameters | recommended
$result = $user->findBy("email = :email")->params([
"email" => "[email protected] "
])->execute();
print_r($result);
// destroy user current
$user->findById(4)->fill();
$user->destroy();
// delete by condition without parameters | not recommended
$user->delete("email = '[email protected] '");
// delete by condition with parameters | recommended
$user->delete("email = :email", [
"email" => "[email protected] "
]);
// mode of use select joins (INNER JOIN, LEFT JOIN, RIGHT JOIN...)
$user->findById(9)->fill();
print_r($user->posts());
// erros
print_r($user->error());
echo "<pre>";