Using Models in Fuel
In my previous post on using the Fuel framework I looked at the basics behind getting a site up and running. This time I want to take it a step further and tell you a bit about my experiences getting a Fuel application to talk to a database (in my case MySQL). To learn the process, I fell back on one of my standby sites that's been rewritten in a few different frameworks now including Solar and a really, really old version of the Zend Framework. It's not an overly complex site, but the version is starting to show its age. So, I usually take that as an opportunity to rewrite the site in something different, just for the experience.
I've already started on a version with the latest download of Fuel and, as a part of it, explored the database model connectivity feature. Now, all of this code is generated from me just looking at the source (since there's no docs to speak of *cough*) so there's probably a better way to do some of this that I don't know. So, lets get started with a basic model.
In my database, I have a few tables - two of them are the aonewsa and aonews_tagsa tables. The relationship between the two is news.ID - news_tags.news_id. Thankfully it works out that this is the default mapping for the relationships between the models. More on that later thoughahere's my sample SQL:create table news (news_title VARCHAR(200), news_story TEXT, date_posted INT, ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID)); create table news_tags (news_id INT, tag_name VARCHAR(50), ID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(ID));
Let's start with the model files - one for each of the tables. Here's the basic structure of the file:find('all',array('select' = array('news_title','news_story','date_posted','ID'), 'limit' = 10, 'order' = array('date_posted','desc'))); return $results; } } ?
This is enough for Fuel to now know about the aonewsa database table. Obviously, you'll need to have the database connection information correct in your db.php to connect to your instance. A few things to note here - the namespace loads in the base Model functionality so the News class can extend the ActiveRecord bits it needs. The ActiveRecord functionality is contained in a package in the standard Fuel download.
In this model, I've defined a simple function to pull out the latest news from our aonewsa database table. This uses the aofind()a method that comes with the ActiveRecord stuff. That first parameter can be one of a few things.
- An ID number of the record you're looking for
- The string aoalla to return all matching rows from the where
- The string aofirsta that essentially puts a aolimit 1a on the end of the statement
The second parameter sets up the rest of the SQL statement - the aoselecta array lists the columns to fetch, the aolimita restricts the results and the aoordera allows you organize the results by a column. There's also aooffseta and aowherea that can help get it more specific. The results are then returned back from the method. If you're more in the aoheavy controllera camp and want to do more complex stuff in there, you can also make a model object inside your controller:find('all',array('select' = array('news_title','news_story','date_posted','ID'), 'limit' = 10, 'order' = array('date_posted','desc'))); } } ?
This essentially does the same thing. Now, lets talk about conventions for models here. If your table name is one word, it's simple enough - for the aonewsa table, the model's name is aoNewsa and the file in /classes/models is aonews.phpa. For something a bit more complex like aonews_tagsa,
Truncated by Planet PHP, read more at the original (another 4699 bytes)