@ -63,21 +63,41 @@ abstract class Model {
//-------------------------------------//
//-------------------------------------//
/**
* Retreive data via PDO prepared statements
*
* The most frequently used constants for PDO are listed below,
* find more at: {@link https://www.php.net/manual/en/pdo.constants.php}
* - PDO::PARAM_BOOL
* - PDO::PARAM_NULL
* - PDO::PARAM_INT
* - PDO::PARAM_STR
*
* Usage:
* self::query(
* "SELECT * FROM `example` WHERE `id` = :id AND `number` = :number AND `text` = :text", [
* [':id', 1],
* [':number', 7, \PDO::PARAM_INT],
* [':text', 'A random string', \PDO::PARAM_STR],
* ]);
*
* self::query(
* 'SELECT * FROM `example` WHERE `id` IN (?, ?, ?) AND `thing` = ?, [
* 1, 2, 3, 'stuff'
* ],
* '?'
* );
*
* @param $query The full prepared query statement
* @param $parameters The values to insert into the prepared statement
* @param $type Type of prepared statement, ':' for named placeholders,
* '?' for value placeholders
*
* @return array|null Retreived data, or null
*/
protected static function query(string $query, array $parameters = [],
protected static function query(string $query, array $parameters = [],
$type = ':'): ?array
$type = ':'): ?array
{
{
// Example
// $parameters = [
// [':id', 1],
// [':number', 7, \PDO::PARAM_INT],
// [':string', 'A random string', \PDO::PARAM_STR],
// ];
// PDO::PARAM_BOOL
// PDO::PARAM_NULL
// PDO::PARAM_INT
// PDO::PARAM_STR
if (substr_count($query, $type) != count($parameters)) {
if (substr_count($query, $type) != count($parameters)) {
return null;
return null;
}
}
@ -237,7 +257,7 @@ abstract class Model {
$required = false;
$required = false;
foreach ($this->rules as $rule) {
foreach ($this->rules as $rule) {
if ($rule[0] == $attribute & & $rule[2] == 1) {
if ($rule[0] == $attribute & & $rule[2] == 1 & & $rule[3] == 0 ) {
$required = true;
$required = true;
break;
break;
}
}
@ -325,16 +345,6 @@ abstract class Model {
return $model;
return $model;
}
}
// $media = MediaModel::selectAll(
// '*', 'ORDER BY id DESC LIMIT :offset, :limit', [
// [':offset', $offset, \PDO::PARAM_INT],
// [':limit', $limit, \PDO::PARAM_INT],
// ]
// );
//
// $contents = ContentModel::selectAll(
// '*', 'WHERE id IN (?, ?, ?)', [1, 2, 3], '?'
// );
public static function selectAll(string $select = '*', string $filter = '',
public static function selectAll(string $select = '*', string $filter = '',
array $parameters = [], $type = ':'): ?array
array $parameters = [], $type = ':'): ?array
{
{
@ -413,14 +423,15 @@ abstract class Model {
}
}
/**
/**
* Load Model data: all, with a limit or pagination
* Load all Models, optionally with a limit or pagination
*
*
* @param int $limitOrPage Treated as page if $limit is provided, limit otherwise
* @param int $limitOrPage Treated as page if $limit is provided, limit otherwise
* @param int $limit The amount to limit by
* @param int $limit The amount to limit by
*
*
* @return array|null The found model data, or null
* @return array|null The found model data, or null
*/
*/
public static function all(int $limitOrPage = -1, int $limit = -1): ?array {
public static function all(int $limitOrPage = -1, int $limit = -1): ?array
{
$class = get_called_class();
$class = get_called_class();
$model = new $class;
$model = new $class;
@ -451,7 +462,9 @@ abstract class Model {
/**
/**
* Retreive Model, or instantiate
* Retreive Model, or instantiate
* Usage: $model = \App\Model\Example::firstOrNew(['name' => 'Example name']);
*
* Usage:
* $model = \App\Model\Example::firstOrNew(['name' => 'Example name']);
*
*
* @param $search Retrieve by
* @param $search Retrieve by
* @param $data Instantiate with search plus data
* @param $data Instantiate with search plus data
@ -474,13 +487,16 @@ abstract class Model {
/**
/**
* Create new Model
* Create new Model
* Usage: $model = \App\Model\Example::create(['name' => 'Example name']);
*
* Usage:
* $model = \App\Model\Example::create(['name' => 'Example name']);
*
*
* @param $data Create with this data
* @param $data Create with this data
*
*
* @return Model The Model
* @return Model The Model
*/
*/
public static function create(array $data): Model {
public static function create(array $data): Model
{
$class = get_called_class();
$class = get_called_class();
$model = new $class;
$model = new $class;
$model->fill($data);
$model->fill($data);
@ -489,15 +505,20 @@ abstract class Model {
}
}
/**
/**
* Retreive Model, or create
* Retreive Model, create if it doesn't exist
* Usage: $model = \App\Model\Example::firstOrCreate(['name' => 'Example name']);
*
* Usage:
* $model = \App\Model\AddressModel::firstOrCreate(
* ['zip_code' => '1234AB', 'house_number' => 3],
* ['street' => 'Example lane']);
*
*
* @param $search Retrieve by
* @param $search Retrieve by
* @param $data Instantiate with search plus data
* @param $data Data used for creation
*
*
* @return Model The Model
* @return Model The Model
*/
*/
public static function firstOrCreate(array $search, array $data = []): Model {
public static function firstOrCreate(array $search, array $data = []): Model
{
$model = self::firstOrNew($search, $data);
$model = self::firstOrNew($search, $data);
if (!$model->exists()) {
if (!$model->exists()) {
@ -507,14 +528,27 @@ abstract class Model {
return $model;
return $model;
}
}
// // Update existing Model, or create it if doesn't exist
/**
// public static function updateOrCreate(array $data, array $data): Model {
* Update Model, create if it doesn't exist
// // $flight = App\Flight::updateOrCreate(
*
// // ['departure' => 'Oakland', 'destination' => 'San Diego'],
* Usage:
// // ['price' => 99]
* $model = \App\Model\FlightModel::updateOrCreate(
// // );
* ['departure' => 'Oakland', 'desination' => 'San Diego'],
// return new Model;
* ['price' => 99]);
// }
*
* @param $search Retrieve by
* @param $data Data used for creation
*
* @return Model The Model
*/
public static function updateOrCreate(array $search, array $data): Model
{
$model = self::firstOrNew($search, $data);
$model->fill($data);
$model->save();
return $model;
}
}
}