Access all your AnchorCMS data throught a consistent and intuitive API, with an object-oriented approach!
Copy ooanchor.latest.php in your theme's folder
In your header.php, write:
<?php theme_include('ooanchor.latest.php'); ?>
In your anchor/models/category.php file, append the following:
#after the *paginate* function
public static function id($id) {
return static::where('id', '=', $id)->fetch();
}
public static function listing() {
return static::where('id', '>', -1)->get();
}
In your anchor/models/page.php file, append the following:
#after the *active* function
public static function listing() {
return static::where('id', '>', 0)->get();
}
public static function id($id) {
return static::where('id', '=', $id)->fetch();
}
Now you're ready to see how simple it is to use.
To fetch all the records you just need a line:
<?php
#fetch all the categories
$categories = ooanchor\categories::get();
#fetch all the posts
$posts = ooanchor\posts::get();
#fetch all the pages
$pages = ooanchor\pages::get();
?>
Want to fetch only the records that match a key-value pair? Easy as:
<?php $posts_that_match = ooanchor\posts::get('your-key-here', 'your-value-here'); ?>
You just need a single record?
<?php $first_by_admin = ooanchor\posts::first('author_name', 'admin'); ?>
You can even apply your own filter rule:
<?php
#get all the pages whose content contains 'hello'
$custom_filtered_pages = ooanchor\pages::get(function($page) {
return strpos($page->content, 'hello') !== FALSE;
}
?>
When you run your queries with get(), you get an array of MODEL s, which is just a wrapper for an associative array with getters and setters. It's useful 'cause it's an object, so you can implement your custom behavior into it, if you want. It provides a useful getAttr() method which lets you specify a default value to return in case the attribute you're lookin for isn't set:
<?php
#given you got $post from a query
$author_bio_or_default = $post->getAttr('author_bio', 'no bio found');
#if the author_bio is empty, the default value will be returned
?>
The models you get from your queries holds some bits of data regular Anchor's objects don't have:
To have a list of all the attributes available, run a first() query and do a var_dump()