-
Notifications
You must be signed in to change notification settings - Fork 0
ActiveRecord Class Mod
This page describes a heavily modified version of the great ActiveRecord_Class library. Please note that some of the methods and functionality of the original are deprecated. Also, the original library was trying to replicate Ruby on Rails' look'n'feel. This is no longer the case with this mod.
[b]Last updated on 10 Nov 2007.[/b] Warning! Some method names were changed.
[h3]Three modifiers to rule all queries[/h3]
This library introduces the concept of query modifiers. Basically, a query modifier is a method you can chain to modify the result of any find*() method call. To better explain this we need an example application.
Let's pretend we are developing a simple book management system for a library. The system will track books and their authors. Each book title and author are stored as separate records in two tables: [b]books[/b] and [b]authors[/b]. A book can have multiple authors, that is why we've got a [b]authors_books[/b] relationship table. Also, a book belongs to a genre, thus we have [b]genres[/b] table.
We create three models: [code]class Book extends ActiveRecord { function __construct () { parent::ActiveRecord(); $this->_has_and_blongs_to_many = array('authors'); $this->_blongs_to = array('genres'); } }[/code] [code]class Author extends ActiveRecord { function __construct () { parent::ActiveRecord(); $this->_has_and_blongs_to_many = array('books'); } }[/code] [code]class Genre extends ActiveRecord { function __construct () { parent::ActiveRecord(); $this->_has_many = array('books'); } }[/code]
[h4]Joining related[/h4] Let's imaging we need to list all books in the database by specifying their name and genre. There is nothing simpler: [code]$this->load->model('book'); $books = $this->book->joining_related_genres->find_all(); echo '
- ';
foreach($books as $book)
echo '
- '.$book->name.' ('.$book->genre_name.') '; echo '
[h4]Counting related[/h4] OK, you've got your list. You will probably need a list of all genres to navigate it (i.e. filter the list). It's simple: [code]$this->load->model('genre'); $genres = $this->genre->counting_books->find_all(); echo '
- ';
foreach($genres as $genre)
echo '
- '.$genre->name.' ('.$genre->num_books.') '; echo '
[h4]Concatenating related[/h4] Let us enhance the first example. We are going to display all books' authors now. Previously, we needed to call fetch_related_authors() for each foreach iteration, which is not wise performance wise. [code]$this->load->model('book'); $books = $this->book->joining_genres->concatenating_authors('name')->find_all(); echo '
- ';
foreach($books as $book)
echo '
- '.$book->name.' by '.$book->authors.' ('.$book->genre_name.') '; echo '
[b]Warning![/b] You cannot use counting_() and concatenating_() together. [b]Warning![/b] concatenating_*() is only available for MySQL version 4.1 and higher
[h3]Other notable changes[/h3]
[h4]No caching for now[/h4] The discover_table_columns() method and all caching logic were ditched. Some hacky benchmarking's shown no difference, but the library got cleaner and there is no more "OMG, I changed my DB structure, but the old one stuck in the cache, and I forgot about that!"
[h4]Find can only find[/h4] Which in plain English means you cannot add SQL clauses via some find*() method attribute. For this purpose use filtering() and searching() query modifiers. These are simple wrappers for CI's Active record where() and like() methods, respectively: [code]$this->book->filtering('year >',2006)->find_all();[/code] [code]$this->author->searching('name','John')->find_all();[/code]
[h4]Simpler __constructs[/h4] You no longer need to specify your $this->_class_name and $this->_table, unless their singular to plural conversion is more complex than adding 's'.
[h3]Download[/h3]