macha
macha

Reputation: 7487

Generate tables and corresponding models using cakePHP

My application allows users (admin) to design their own forms, and each form would have a corresponding table to store that information. So whenever the form is created, I would need to generate a table in the database, for that form, based on its input type and possible value.

Creation of table can be done using $this->Model->query('create table ....') method, but one thing I am unable to figure out is, how would I later generate a model for the corresponding table?

Any help regarding this would be appreciated.

Upvotes: 0

Views: 1080

Answers (2)

Eduardo Romero
Eduardo Romero

Reputation: 1159

How about using $this->Model->query to create the table, then use Bake to create the views/models/controllers? Giving that you'll only need basic CRUD. But I do agree that this will become un-mantainable soon.

Upvotes: 0

deceze
deceze

Reputation: 522076

You should not create a self-modifying application. This will soon become a headache, if only from an optimization standpoint. Even worse than modifying a database dynamically is to generate models dynamically. Don't do it.

If your goal is to create an application that allows users to create forms, you will have to create a meta-application that stores information about forms and schemas as data in a database.

id | form_id | field_name | field_type | length | ...
---+---------+------------+------------+--------+----
1  | 42      | firstname  | string     | 255    | ...
2  | 42      | lastname   | string     | 255    | ...
...

Based on these meta information you need to dynamically create the actual forms and store the input data in a similar meta-fashion in your database.

Unless you're trying to make a phpMyAdmin database administration clone, this is the only realistic way. If you're trying to make a database administration app, you won't create models dynamically, but just use one database manipulation model, which would do pretty much everything via $this->query().

Either way, you're creating a meta application, which is often a bad idea. You're trying to give the job of programmers (designing and creating databases and forms) to users, which will inevitably do a worse job. That's of course just my humble opinion, I don't know your business case.

Upvotes: 6

Related Questions