Welcome Guest, Not a member yet? Register   Sign In
Is there a way to auto generate CRUD for multiple tables?
#7

[eluser]BrianDHall[/eluser]
Over in the Ignited Code forum there are a few options, you can probably just go through and look through the ones about CRUD and so forth.

Personally, I use DMZ mentioned by bretticus. It handles CRUD functions, but goes beyond that to control relationships as well. This is basically how it works:

1- Create table according to certain conventions (like the table name is plural, the column names are singular), each has an 'id' index column...I guess that is about all. You can use in-table foreign keys or special join tables for relationships, but that is beyond the CRUD you asked about.

2- Use the model template provided by DMZ and do a search-replace. You just replace "Template" with "Yourmodelname", and "template" with "yourmodelname".

3- In the model file you define any relationships, such as if you have a User and Usergroup model you will enter 'usergroup' into the User $has_one definition, and in the Usergroup you will enter 'user' into the $has_many definition.

That's it for the model file, but you are free to put special functions and variables and such if you want.

Then in your code you do things like this to create a new user and associate them with a certain usergroup:

Code:
$user = new User();
$user->username = 'test';
$user->password = 'pass';

$ug = new Usergroup();
$ug->where('role', 'admin')->get();

$user->save($ug);

Then to get a persons related usergroup some other time you might do:

Code:
$user = new User();
$user->where('username', 'test')->get();

echo "Username: " . $user->usergroup->role;

Create, read, update, and delete are all handled by simple get() and save() calls. Validation is set in the model file much like CI form validation, so you can automatically encrypt password on save() call or run trim() or anything you want.

You have to create one model for each database table, but this process actually only takes a couple of minutes at most as it is basically just search and replace - the rest of the time you are going well beyond CRUD.

You can, if you want, just use DMZ for pure CRUD and not worry about relations (ORM), but you'd be missing a very big point.

For controlling issues of editability and such you might look at the DMZ html form extension that adds even more functionality.


Messages In This Thread
Is there a way to auto generate CRUD for multiple tables? - by El Forum - 10-01-2009, 12:38 PM



Theme © iAndrew 2016 - Forum software by © MyBB