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

[eluser]napster3000[/eluser]
I have multiples table to create a CRUD and write code for each is too expensive.
Is there a way to generate CRUD for each tables? And select which fields must be editable and witch not?

Sorry for my bad english
#2

[eluser]überfuzz[/eluser]
[quote author="Aken" date="1252344263"]...

You could even have a private function inside your model that does the actual DB retrieve, and just define some variables for each situation in the rest of your functions.

Code:
function getDB1()
{
    $params = array(
        'column1' => 'this',
        'column2' => 'this',
        'table1' => 'this',
        'table2' => 'this'
    );
    
    return $this->parse($params);
}

function getDB2()
{
    $params = array(
        'column1' => 'this',
        'column2' => 'this',
        'table1' => 'this',
        'table2' => 'this'
    );
    
    return $this->parse($params);
}

private function parse($params)
{
    $this->db->select($params['table1'].'.'.$params['column1'].' AS when');
    $this->db->select($params['table2'].'.'.$params['column2'].' AS event');
    
    return $this->db->return_array();
}
[/quote]

I've got this suggestion when I posted a similar question. It really helped me, it makes the db-handling nice and tidy when you're using the same query on different tables.
#3

[eluser]jedd[/eluser]
[url="http://ellislab.com/forums/viewthread/47524/"]Code Crafter[/url] might be worth checking out, too.
#4

[eluser]napster3000[/eluser]
ok, i tested CodeCrafter. Is there something better? Codecrafter is stop from 2007!
#5

[eluser]bretticus[/eluser]
Never used either, but you might also look at DMZ
#6

[eluser]jedd[/eluser]
What do you mean 'stopped'?

Did it not work, or do you believe software older than a few months somehow goes off?

As I understand it, CodeCrafter did what it set out to do, so there wasn't much in the way of 'new features' to wedge into the thing.
#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.




Theme © iAndrew 2016 - Forum software by © MyBB