PHP-WAX Model Associations
For complicated model associations, the framework gives you a big helping hand by taking care of the dirty work of managing join tables.
Take a typical example, the user can create pages and upload images, but you also need to provide a way to link images to pages. Normally this would be done through a join table which simple links the records.
This table in the database doesn't serve any application purpose, so ideally it wouldn't have it's own model representation. So the framework will now automatically create and modify join tables with only a little bit of coding on your part.
The following is the method that sets up the association.
function has_many(String $table, String $method, String $join=false)
You can call it anywhere before you make a database call, but to aid in setting up a model a new overridable method called 'after_setup' has been created to take care of any database setup that you may need.
class Page extends WXActiveRecord {
public function after_setup() {
$this->has_many("cms_file", "images");
}
}
The first value is the name of the database table you are associating with, the second is an internal reference that you want to name the association, the third is optional unless you want to override the default name of the join table.
So now you want to associate an image with your page. Well magically we can just do....
$page = new Page(4); $page->images = "12";
This adds the image with an id of 12 to the join table. Want to see what images are associated with this page?
$page_images = $page->images;
This will return an array of objects, one for each image.
For no extra charge you also get some more magic methods, you can call....
$page->add_images($id); $page->delete_images($id); $page->find_images($id); $page->clear_images();
All self explanatory, just pass an id to the first three and of course the fourth needs no value as it deletes all images associated with this page.
RECENT ARTICLES
Rails and Apache
In what appears to be a very useful find ' read more
Useful bit of Javascript
Never thought about using javascript for this before but I had a requ... read more
Quick jquery Gallery
I had a look at some of the gallery plugins available for jquery and ... read more
Massive CPU usage when updating Fedora using YUM/RPMQ
We noticed that YUM and RPMQ processes were using a massive amount CP... read more