Welcome Guest, Not a member yet? Register   Sign In
Automatically Creating Database Tables
#1

[eluser]Jonah11[/eluser]
Hi,

Does codeigniter have any way to automatically create database files from either php model classes, or some kind of text based model description? I am thinking of the django framework, where you can create a model class, specifying all your field information, and then just run a snycdb command to have those models created in your MySQL db or whatever you are using.

Based on the tutorials for codeigniter, it seems like you have to manually create all your tables, but I wanted to make sure.

Thanks,
Jonah
#2

[eluser]WanWizard[/eluser]
No, CI doesn't have that functionality.

CI doesn't have fixed form models in which you would be able to specify table definitions. In CI, a model is just another class.
#3

[eluser]mi6crazyheart[/eluser]
Try this: http://ellislab.com/codeigniter/user-gui...forge.html

May be this thing make u'r task easier...
#4

[eluser]Jonah11[/eluser]
[quote author="WanWizard" date="1292973894"]No, CI doesn't have that functionality.

CI doesn't have fixed form models in which you would be able to specify table definitions. In CI, a model is just another class.[/quote]

Thanks for the info. So what is the preferred method of creating tables for a MySQL database with code igniter? Do you just use phpMyAdmin?
#5

[eluser]Jonah11[/eluser]
[quote author="mi6crazyheart" date="1292974357"]Try this: http://ellislab.com/codeigniter/user-gui...forge.html

May be this thing make u'r task easier...[/quote]

Thanks, that looks useful. I don't see any support for foreign key or many to many relationships though. Do you just add those manually to the db?
#6

[eluser]TaylorOtwell[/eluser]
I usually just use phpMyAdmin for manipulating my tables. For a good ORM solution, check out http://www.phpactiverecord.org/
#7

[eluser]WanWizard[/eluser]
[quote author="Jonah11" date="1292974401"]Thanks for the info. So what is the preferred method of creating tables for a MySQL database with code igniter? Do you just use phpMyAdmin?[/quote]
I used to use dbforge, but due to the fact that it has to support a lot of database engine's, you can only use it for generic table definitions.

So now I don't bother anymore, and include .sql files with my projects for the different database I'd like to support, and a generic one which will probably work on most databases. Then I just load it using file I/O calls, and feed the commands to $this->db->query().
I do upgrades the same way (with .sql files containing 'diffs').

And I use Datamapper ORM to access my databases.
#8

[eluser]Jonah11[/eluser]
[quote author="WanWizard" date="1292989420"]

And I use Datamapper ORM to access my databases.[/quote]

I am a bit confused. I thought CodeIgniter's Model classes provided you an ORM. Are you using a different one because you are doing more advanced database interactions?
#9

[eluser]nickaceph[/eluser]
Is there any new solution to this, under any ORM like generate from Datamapper ORM?

is there any extension that parses Datamapper ORM through db->forge() to generate basic table base on

models as $has_many, $has_one, and $validation array. Iam planning to create one just for MYSQL is there any so I will not have to reinvent the wheel Big Grin

Thanks
#10

[eluser]Pert[/eluser]
[quote author="nickaceph" date="1369391802"]is there any extension that parses Datamapper ORM through db->forge() to generate basic table base on
[/quote]

When writing this kind of solution, there are few things to keep an eye out for.

Not all hosting providers will allow PHP to modify databases - you should first try few commands on live server to make sure it's even technically possible, before writing full feature product.

It's usually disabled to limit damage malicious hackers might do if they do happen to get a hold of database connection details.

The best practise is to lock down MySQL as much as possible and allow structure changes to be run through DBMyAdmin or other similar tool.

There are ways to restructure your database in a way that you would not need to edit database structure.

If that is not an option, letting users alter tables on fly could be dangerous as data could be accidentally deleted.

Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB