ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

how can codeigniter create a db schema from a model is there a plugin

July 23, 2008 10:35pm

Subscribe [6]
  • #16 / Jul 24, 2008 10:24am

    elrolfe

    12 posts

    an this is a sample stripped-downyml-file… you can put config/params in there too.

    table: people
    label: People
    
    fields:     
        name:
            label: name
            type: textfield
            validators: 
                - required    
        email:
            label: E-Mail-Adresse
            type: textfield
            validators:
                - email
                - required
        
        password:
            label: Password
            type: password
       
        description:
            label: description
            type: textarea
            params: 
                -editor
                
        birthday:
            label: Your Birthday
            type: date
            validators:
                - required
                - valid_date
            options:
                -readonly
                
        fav_food:
            label: Your Favourite Food
            type: select_one
            fTable: food
            fTableId:id
            fTableLabel: name
            fTableSort: essen.name DESC
            
        friends:
            label: Your Friends
            type: select_many
            fTable: people
            fTableId: id
            fTableLabel: name
            fTableSort: name DESC
  • #17 / Jul 24, 2008 11:57am

    Sally D

    129 posts

    @elrolfe Thanks I will examine your work very carefully like a shaman examines a vision

    I will come up with a nice db utiliy that gets a design and builds the db so we can build a depository for db designs for CI and just use them in our models

    also if you can send a design template in your models it will make collaboration between two people easier

  • #18 / Jul 25, 2008 9:05am

    Sally D

    129 posts

    Hi all this is what my brain came up with I don’t know if it’s logical or not that is why I am posting it here.

    I did not want to mess around with yaml cause I don’t have pear library’s installed on my machine to make it work since its not native on php 5 yet, so I thought of the next easiest thing INI files

    for each table in your db you would create an ini file the name must match the name of a table in a db and the result array much match the array the $this->dbforge->add_fields().

    main controller

    <?php
    class Blog extends Controller {
        function Blog()
        {
            parent::Controller();    
            $this->load->model('Blog');
            $this->load->library('db_utils');
        }
        function index()
        {
            $this->load->view('welcome_message');
        }
        function set_up_db(){
            $table_names=$this->db_utils->get_tables();
            $this->Blog->make_tables($table_names);
        }
    }
    ?>

    model

    <?php
    class Blog extends Model {
        function Blog() {
            parent::Model();
            $this->load->library('db_util');
        }
        
        // this function takes and array of db tables names you 
        // want to create. The names of the table corresponds to 
        // names of ini files created for each table
        // array('blog','comments') 
        // would mean you would need a blog.ini and comments.ini
        // that would describe the data type for each field in the table
        function make_tables($tables_array) {
            $this->db_util($tables_array);
            
        }
    }
    ?>

    db util library

    <?php if(!defined('BASEPATH')) exit('No Direct access');
    class Db_util {
        function Db_util()
        {
            $CI=& get_instance();
            $this->load->dbforge();
        }
        function get_tables(){
            // array of files in the ini dir
            $files = array();
            // path to db ini files
            $path=base_url()."/db_ini";
            $handle=opendir($path);
            chdir($handle);
            while ($file !== false) {
                $file=readDir($handle);
                $files[] = $file;
            }
            $temp = preg_grep("/ini$/", $files);
            $table_names=array();
            foreach($temp as $val) {
                $x=substr($val,-4,4);
                $table_names[]=$x;
            }
            return $table_names;
        }
        function make_tables($array){
            foreach($array as $value) {
                $ini_url=base_path()."/db_ini/$value.ini";
                $fields = parse_ini_file($ini_url, true);
                $this->dbforge->add_field($fields);
                $this->dbforge->create_table($value);
            }
        }
    }
    ?>

    you would have to make an ini file for each table in the database like this and then store them in a db_ini Directory

    ; database ini
    ; this is a database schema for that works with the
    ; codeigniter forge db class
    ;
    ;


    [blog_id]
    type=int
    contraint=5
    unsigned=true
    auto_increment=true

    [blog_title]
    type=varchar
    contraint=100

    [blog_author]
    type=varchar
    constraint=100
    defualt=me
    [blog_description]
    type=text
    null=true

    I had this revalation in my sleep so I don’t know if it’s logical to make an ini file for each table in a database or not

    any way I got a job interview at 10 am and then it’s off to myrtle beach, sc for a vacation I will play around with this there

    I hope you can tell me if this approach is logical or not

  • #19 / Jul 25, 2008 10:21am

    elrolfe

    12 posts

    Hello again,

    at first look I would say we go the same way here. you chose ini, i preferred yml. either way we have table-schemes which need to be interpreted.
    so far so good, the next thing to do is changing the structure of the tables as you sometimes have to do in mid-project without deleting data.

    btw im off to vacation for 10 days now. have a good time. 😊
    lets talk about it again when were both back.

  • #20 / Jul 25, 2008 1:43pm

    m4rw3r

    647 posts

    Nice to see that my parser helped 😊

    I think this is also one thing I’ll have to add to IgnitedRecord, maybe as a side module (which probably remind of RoR’s db migrate).
    But I will probably wait for the improved db abstraction in the db forge class before I start on my project.

  • #21 / Jul 27, 2008 4:50am

    Grahack

    266 posts

    Hi guys, you could even work on the same project, and let the format of the schema be an option. What do you think?

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases