1. Go to this page and download the library: Download n0nag0n/super-model 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/ */
n0nag0n / super-model example snippets
use n0nag0n\Super_Model;
class User extends Super_Model {
protected $table = 'users';
}
// somefile.php
$pdo = new PDO('sqlite::memory:', '', '', [ PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]);
$User = new User($pdo);
// WHERE company_id = 50
$users = $User->getAllBycompany_id(50);
// same as above
$users = $User->getAll([ 'company_id' => 50 ]);
// get by the id field on the users table
$User->getByid(3);
/*
[
'id' => 3,
'email' => '[email protected]',
'company_id' => 61
]
*/
$User->getBycompany_id(61);
/*
// it only will pull the first row, not all rows
[
'id' => 2,
'email' => '[email protected]',
'company_id' => 61
]
*/
// this is pointless, but will still work
$User->getAllByid(3);
/*
[
[
'id' => 3,
'email' => '[email protected]',
'company_id' => 61
]
]
*/
$User->getAllBycompany_id(61);
/*
[
[
'id' => 2,
'email' => '[email protected]',
'company_id' => 61
],
[
'id' => 3,
'email' => '[email protected]',
'company_id' => 61
]
]
*/
// Full example
$filters = [
//
// arguments in the WHERE statement
//
'some_field' => 5, // some_field = ?
'some_field-=' => 5, // some_field = ?
'another_field' => 'IS NULL', // some_field IS NULL
'another_field' => 'IS NOT NULL', // some_field IS NOT NULL
'another_field->' => 'Apple', // another_field > ?
'another_field->=' => 'Apple', // another_field >= ?
'another_field-<' => 'Apple', // another_field < ?
'another_field-<=' => 'Apple', // another_field <= ?
'another_field-!=' => 'Apple', // another_field != ?
'another_field-<>' => 'Apple', // another_field <> ?
'another_field-LIKE' => 'Ap%ple', // another_field LIKE ?
'another_field-NOT LIKE' => 'Apple%', // another_field NOT LIKE ?
'another_field-IN' => [ 'Apple', 'Banana', 'Peach' ], // another_field IN(??) double question mark gets parsed as array
'another_field-NOT IN' => [ 'Apple', 'Banana', 'Peach' ], // another_field NOT IN(??) double question mark gets parsed as array
// If you need some custom action
'another_field-RAW-> DATE_SUB(?, INTERVAL 1 DAY)' => '1980-01-01', // another_field > DATE_SUB(?, INTERVAL 1 DAY)
//
// Other parts of the query
//
// choose what columns you want to select
'select_fields' => 'id, first_name',
// Get any joins
'joins' => [ 'LEFT JOIN companies ON companies.id = users.company_id' ],
// Group by
'group_by' => 'company_id',
// having
'having' => 'count > 5',
// order by
'order_by' => 'id DESC',
// limit
'limit' => 15,
// offset
'offset' => 10000,
];
$users = $User->getAll($filters);
use n0nag0n\Super_Model;
class User extends Super_Model {
protected $table = 'users';
protected $disallow_wide_open_queries = false;
}
$User->create([ 'email' => '[email protected]', 'company_id' => 55 ]);
// returns 4
$User->create([ [ 'email' => '[email protected]', 'company_id' => 55 ], [ 'email' => '[email protected]', 'company_id' => 56 ] ]);
// returns 6, only the last id will be returned
$User->update([ 'id' => 1, 'email' => '[email protected]' ]);
// returns 1 and will only update the email field
$User->update([ 'email' => '[email protected]', 'company_id' => 61 ], 'email');
// returns 1
$User->update([ 'company_id' => 61, 'email' => '[email protected]' ], 'company_id');
// returns 3, not really logical, but it would update all the emails
use n0nag0n\Super_Model;
class User extends Super_Model {
protected $table = 'users';
public processResult(array $process_filters, array $result): array {
// add some trigger here and do whatever checks you need
if(isset($process_filters['set_full_name']) && $process_filters['set_full_name'] === true && !empty($result['first_name']) && !empty($result['last_name'])) {
$result['full_name'] = $result['first_name'].' '.$result['last_name'];
}
return $result;
}
}
// later on in some other file.
$User = new User($pdo);
// setting the processResults filter here is the key to connecting the getAll statement with your processResult method
$users = $User->getAll([ 'company_id' => 51, 'processResults' => [ 'set_full_name' => true ] ]);
echo $users[0]['full_name']; // Bob Smith
use n0nag0n\Super_Model;
class User extends Super_Model {
protected $table = 'users';
public function processCrazyKukooQuery(/* add whatever c.title AS Category
FROM
tags2Articles t2a
INNER JOIN tags t ON t.id = t2a.idTag
INNER JOIN categories c ON t.tagCategory = c.id
INNER JOIN (
SELECT
a.id
FROM
articles AS a
JOIN tags2articles AS ta ON a.id=ta.idArticle
JOIN tags AS tsub ON ta.idTag=tsub.id
WHERE
tsub.id IN (12,13,16)
GROUP BY a.id
HAVING COUNT(DISTINCT tsub.id)=3
) asub ON t2a.idArticle = asub.id");
$statement->execute();
return $statement->fetchAll();
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.