1. Go to this page and download the library: Download ekhaled/f3-cortex 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/ */
ekhaled / f3-cortex example snippets
// SQL - MySQL
$db = new \DB\SQL('mysql:host=localhost;port=3306;dbname=MyAppDB','user','pw');
// SQL - SQlite
$db = new \DB\SQL('sqlite:db/database.sqlite');
// SQL - PostgreSQL
$db = new \DB\SQL('pgsql:host=localhost;dbname=MyAppDB','user','pw');
// SQL - SQL Server
$db = new \DB\SQL('sqlsrv:SERVER=LOCALHOST\SQLEXPRESS2012;Database=MyAppDB','user','pw');
// Jig
$db = new \DB\Jig('data/');
// Mongo
$db = new \DB\Mongo('mongodb://localhost:27017','testdb');
// file at app/model/user.php
namespace Model;
class User extends \DB\Cortex {
protected
$db = 'AppDB1', // F3 hive key of a valid DB object
$table = 'users'; // the DB table to work on
}
class User extends \DB\Cortex {
function __construct() {
// get the DB from elsewhere
$this->db = \Registry::get('DB');
$f3 = \Base::instance();
// load fields from .ini file
if (!$f3->exists('usermodel'))
$f3->config('app/models/usermodel.ini');
foreach ($f3->get('usermodel') as $key => $val)
$this->{$key} = $val;
parent::__construct();
}
}
// load a specific author
$author = new \AuthorModel();
$author->load(['_id = ?', 2]);
// create a new profile
$profile = new ProfileModel();
$profile->status_message = 'Hello World';
// link author and profile together, just set the foreign model to the desired property
$profile->author = $author;
// OR you can also just put in the id instead of the whole object here
// (means you don't need to load the author model upfront at all)
$profile->author = 23;
$profile->save();
// create a new profile
$profile = new ProfileModel();
$profile->status_message = 'Hello World';
$profile->save();
// load a specific author and add that profile
$author = new \AuthorModel();
$author->load(['_id = ?', 2]);
$author->profile = $profile;
$author->save();
$author->load(['_id = ?', 23]);
echo $author->profile->status_message; // Hello World
$profile->load(['_id = ?', 1]);
echo $profile->author->name; // Johnny English
$author->load(['name = ?','Johnny English']);
$news->load(['_id = ?',42]);
$news->author = $author; // set the object or the raw id
$news->save();
echo $news->author->name; // 'Johnny English'
$author->load(['_id = ?', 42]);
$author->news; // is now an array of NewsModel objects
// if you like to cast them all you can use
$allNewsByAuthorX = $author->castField('news'); // is now a multi-dimensional array
$news->load(['_id = ?',1]);
// array of IDs from TagModel
$news->tags = [12, 5];
// OR a split-able string
$news->tags = '12;5;3;9'; // delimiter: [,;|]
// OR an array of single mapper objects
$news->tags = [$tag,$tag2,$tag3];
// OR a hydrated mapper that may contain multiple results
$tag->load(['_id != ?',42]);
$news->tags = $tag;
// you can also add a single tag to your existing tags
$tag->load(['_id = ?',23]);
$news->tags[] = $tag;
$news->save();
class User extends \DB\Cortex {
// [...]
// validate email address
public function set_mail($value) {
if (\Audit::instance()->email($value) == false) {
// no valid email address
// throw exception or set an error var and display a flash message
$value = null;
}
return $value;
}
// hash a password before saving
public function set_password($value) {
return \Bcrypt::instance()->hash($value);
}
public function get_name($value) {
return ucfirst($value);
}
}
// find all tags with the sum of all news that uses the tag, ordered by the top occurring tags first.
$tag = new \Model\Tag();
$tag->countRel('news');
$result = $tag->find(null,['order'=>'count_news DESC, title']);
// just set a simple value
$user->virtual('is_online', TRUE);
// or use a callback function
$user->virtual('full_name', function($this) {
return $this->name.' '.$this->surname;
});
$user->cast(NULL, [
'*' => 0, // cast all own relations to the given depth,
// 0 doesn't cast any relation (default if this key is missing)
'modelA' => 0,// if a relation key is defined here, modelA is being loaded and casted,
// but not its own relations, because the depth is 0 for it
'modelB' => 1,// modelB and all its 1st level relations are loaded and casted
'modelC' => [...] // you can recursively extend this cast array scheme
]);
// simple sample: only cast yourself and the author model without its childs
$news->cast(NULL,[
'*'=>0,
'author'=>0
]);
// nested sample: only cast yourself,
// your own author relation with its profile and all profile relations
$news->cast(NULL,[
'*'=>0,
'author'=>[
'*'=>0,
'profile'=>1
]
]);
array|null castField( string $key [, int $rel_depths = 0 ])
// fetch all posts, with comments and count its likes (reactions of type "like") on each comment
$post->countRel('comments.reaction','count_likes', ['type = ?', 'like']);
$results = $post->find();
string dbtype()
array defaults([ bool $set = FALSE ])
bool dry()
$mapper->load(['_id = ?','234']);
if ($mapper->dry()) {
// not found
} else {
// record was loaded
}
php
$filter1 = ['_id = ?', 999];
$filter2 = ['published = ? or active = ?', true, false];
$new_filter = $mapper->mergeFilter([$filter1, $filter2]);
// array('(_id = ?) and (published = ? or active = ?)', 999, true, false)
php
array paginate([ int $pos = 0 [, int $size = 10 [, array $filter = NULL [, array $options = NULL [, int $ttl = 0 ]]]]])
php
Cortex rel( string $key )
php
$user->load();
var_dump($user->comments); // array of comments
$new_comment = $user->rel('comments');
// $new_comment is a new empty \Model\Comment