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]
  • #1 / Jul 23, 2008 10:35pm

    Sally D

    129 posts

    like I would like a database plug in that I can instantiate into a model so that it creates the sql table or tables with all the correct data types set for me, so I really would not have to use php my admin at all or even know sql to make a database. The plug in will create the db on the way I am using my data in my model.

    All I would have to do is make a model and then this plug in will build my database schema based on the model so I don’t have write any sql at all is that plausible


    like in my models constructor I would say:

    $this->load->library(‘db_schema’,‘schema_set_up’);

    then I would have a private function that would specifiy data type and sets like this
    function _schema_set_up() {
    $this->db_schema->CharField('feild_name',max_length=200);
    $this->db_schema->IntegerField('feild_name');

    }

    then at the start of a new project I can call the db_schema class to build a database from the private function that I would declare in my model

  • #2 / Jul 23, 2008 11:39pm

    Colin Williams

    2601 posts

    All I would have to do is make a model and then this plug in will build my database schema based on the model so I don’t have write any sql at all is that plausible

    First of all, sorry, but there is no standard for defining schemas in CodeIgniter Models, so you would first need to establish one, and only then can a library be written that reads this schema and produces the proper SQL. Rails does this, as does Django, Symfony, and maybe Cake, too, but not CodeIgniter.

    Second, if your only motivation is to get by with learning the least amount of tools possible, you’re probably in the wrong profession. And sorry again, but an ORM-system is still not going to keep you from getting dirty with SQL. You’re going to need to construct a complex, unique query eventually.

    Learning SQL will take you further than having a library like you describe above.

  • #3 / Jul 23, 2008 11:48pm

    Sally D

    129 posts

    I am sorry colin but can not help but sense your smugness and I dis agree with you what if they told that to Edison hey we don’t need light bulbs we can just use candles just fine instead then we would still would be in the dark.

    And I don’t make a living doing this. This is my hobby.

  • #4 / Jul 24, 2008 1:55am

    Colin Williams

    2601 posts

    Didn’t want to come across as smug, just realistic. And I understand that it’s just a hobby for you. I started the same way. I’m not sure your Edison allusion is an apt metaphor, because I don’t think he developed a framework for candle light, he developed a new form of illumination entirely. You weren’t talking about inventing a new candle, you just don’t care to learn how to light one.

    But the crux of my reply stands that there is no standard way of defining schemas in CI Models. And there’s no library to interpret any schema. Perfect opportunity for someone to introduce one. I would consider it, but I’m just fine with CI’s ActiveRecord and the occasional SQL command.

    EDIT:

    Then again, search the Wiki for Rapyd. I think it is a fairly solid ORM solution, if I remember correctly. Seemed to kind of stand alongside CI rather than incorporate with it, but I never actually used it, so no final word.

  • #5 / Jul 24, 2008 2:20am

    Sally D

    129 posts

    well I am thinking of developing a method that returns return a schema array to a database utility builder class

    function return_schema() {

    return array('CharField'=>array('feild_name'=>'name of feild','length'=>'length of feild');

    }

    then I can list the attributes that I want in a multidimensional array and then I would return that array to a database constructor utility that will build and it will contruct the database for me with out using another app to build the db for me.

    and then I can create a front end to keep track of my projects and apps that I create with for CI and delete them all on this front end CI project

    Colin I understand your smugness now thanks does this all make sense or am I tripping

  • #6 / Jul 24, 2008 2:28am

    Randy Casburn

    997 posts

    @Raymond—The short answer is “No - it doesn’t exist in CI natively”

    Randy
    ======================================
    P.S - My perception of your rebuke of Colin:

    You compared:

    1) Your display of outwardly wanting to avoiding gaining knowledge (re-read your first post)

    TO (your second post)

    2) One of the World’s greatest inventors, an American, who lived by the credo that knowledge was a gift that must be gathered up at every opportunity regardless of the lengths at which one must go to gain it.

    I take great exception this comparison.  Edison’s famous quotes such as “10,000 attempts…not failures” are a testament to his will to try and experiment and try again.
    =====================

    What we have from you is a testament that you aren’t willing to try to learn SQL, that you don’t “want to know SQL”, “so I don’t have to write any sql”.  My perception is what it is and I doubt you change it.

    ====================

    Colin was just being realistic.

  • #7 / Jul 24, 2008 2:36am

    Colin Williams

    2601 posts

    Sounds like you’ve got something cooking there. March on and good luck. Something dead simple, straight-forward, and that doesn’t impact the normal flow of CI is the type of thing this community craves, so keep that in mind.

  • #8 / Jul 24, 2008 2:41am

    Randy Casburn

    997 posts

    @Raymond—Before you go to all that trouble, have you actually looked at the DB_Forge class?

    I mean, before you go and rewrite the whole thing.  You might as well look at it.

    http://ellislab.com/codeigniter/user-guide/database/forge.html

    Randy

    ============

    @Colin—( make a note: Randy is nice even when he doesn’t like somebody :wow: )

  • #9 / Jul 24, 2008 2:48am

    Colin Williams

    2601 posts

    @Colin—( make a note: Randy is nice even when he doesn’t like somebody :wow: )

    What, am I not always nice? Maybe my wife actually has a point… And, just for the record, I like everyone here, whether they want to learn anything or not.

  • #10 / Jul 24, 2008 2:52am

    Randy Casburn

    997 posts

    Geeze—that came out wrong.  That was all suppose to be SELF DEPRECATING.  You know about me.  I didn’t me you at all man.  I was being mean.  Not you.

    Forums stink.

    Good night.

  • #11 / Jul 24, 2008 2:55am

    Randy Casburn

    997 posts

    This is why I don’t use humor in my //comments//  heheh // because the next nerd would take it completely out of context!

    I was the meany…I was implying I was the one that didn’t like the guy.

    It was all in fun 🧛

  • #12 / Jul 24, 2008 3:02am

    Colin Williams

    2601 posts

    Hahahaha… I’m the one who came off wrong. I knew what you were saying and I took the opportunity to self-deprecate myself as well!

    Forums do stink.

  • #13 / Jul 24, 2008 4:19am

    elrolfe

    12 posts

    hi guys,
    i recently set up a little tool using CI that allows me to describe my db-table in yml-files. the script takes these files and creates the db-tables using db-forge.
    in order to work you need to set up routes, create yaml-files, load a library and use a controller. its very beta and not tested (it worked at first look…).
    if youre interested i can post the files. but i have to say that im not the einstein of php-coding and wont have the time to support you. to make it worse: all comments are german 😊

  • #14 / Jul 24, 2008 9:05am

    Sally D

    129 posts

    You guys really are convinced that I don’t know my way around a sql prompt wow! Well I do understand sql and how to manually create a table pretty easily

    @Randy why did you say I don’t want to learn sql when I watched over 10 hours of videos of how to construct sql statements on the web all ready so I am familiar with the create table sometable (count int, note text);

    all I would have to do is loop through an object, xml or an array and echo this text to an open mysql connection and wallah a database table will be created for me

    here is the paradigm that will start the table creation process. You will pass it an object, xml or array that you set up in a model. Then in your controller you would make a function called make_db and this functions argument will be fed a call to the method you made in your model that returns the schema array.  From that array the make_db library class will build the db real easily. I can make this in a matter of an hour really straight forward and simple. Then I can start working development with out messing around with sql or another application I won’t have to leave CI for anything.

    and If I want to share a model with you all I would need to do is include a schema with my model and then if you had my make_db class you would have the table all set up with out the need to use another app like php my admin or something else

    function make_db($this->Model_name->return_schema()){
    // creation of table takes place here and show details of out put to user
    }

    @elrolfe I would love to see your class I understand php and I can read it well

    I really like CI I don’t know what I did to Randy to make him not like me. You don’t even know me who I am or what I do? 

    And thanks for the Forge link that is just what I needed to make the ace go into the hole

  • #15 / Jul 24, 2008 10:22am

    elrolfe

    12 posts

    Hi Raymond,
    youll need yayparser to get this working. i created a directory called structure in the application dir, that holds all yml-files.
    i hope code-pasing works this way…
    feel free to use it or to work on it. if you have any ideas about what to do better, please tell me 😊

    the library:

    <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    class ymlcrud 
    {
        /**
         * params
         * library-Parameter
         * @var object
         **/
        public $params;
        
        
        
        
        function __construct()
        {
            $this->CI = &get;_instance();
            
            $this->params->showProfiler     = true;
            $this->params->ymlDir             = base_url().'structure/';
            $this->params->ymlFileExtension = '.yml';
        }
        
        
        
        
        
        /**
         * loadYmlFileAsArray
         * 
         * @return array
         **/
        public function loadYmlFileAsArray($fileBaseName)
        {
            return yayparser(utf8_decode(file_get_contents($this->params->ymlDir.$fileBaseName.$this->params->ymlFileExtension)));
        }
        
        
        
        
    
        
        /**
         * showProfiler
         *
         * @return bool
         **/
        public function showProfiler()
        {
            return $this->params->showProfiler;
        }
    }
    // END ymlcrud Class
    ?>

    the controller

    <?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
    
    /**
     * generator
     *
     * @package ymlcrud
     **/
    class generator extends Controller {
        
        private  $table;
        private  $fields = array();
        
        
        
        
        /**
         * __construct
         *
         * @return void
         **/
        function __construct()
        {
            parent::Controller();
            log_message('debug', 'generator controller initialized');
            $this->load->dbforge();
        }
        
        
        
        
        /**
         * index
         * stellt uebersicht dar mit generator-links
         * @return bool
         **/
        function index($table='structure')
        {
            $data['yml'] = $this->ymlcrud->loadYmlFileAsArray($table);
            $this->load->view('generator_view', $data['yml']);        
        }
        
        
        
        
        /**
         * createTable
         * erstellt eine tabelle mit allen feldern. wenn es die tabelle mit dem namen gibt, wird zuvor ein backup ("altername_timestamp") angelegt
         * @return void
         * @param $table string die zu erstellende tabelle
         **/
        function createTable($table)
        {        
            $data['yml'] = $this->ymlcrud->loadYmlFileAsArray($table);
            
            
            if (in_array($table, $this->db->list_tables()))
            {
                $this->dbforge->rename_table( $data['yml']['table'],$data['yml']['table'].'_'.time());    
                $this->modifyTable($table);        
            }
            
            
            
            $this->dbforge->add_field('id');
            foreach ($data['yml']['fields'] as $name => $field)
            {
                
                switch ($field['type']) 
                {
                    #einfaches textfeld
                    case 'textfield':
                        $this->dbforge->add_field(array($name => array('type' => 'VARCHAR','constraint' => '255', 'null' => 'TRUE')));
                        break;
                    
                    #textarea    
                    case 'textarea':
                        $this->dbforge->add_field(array($name => array('type' => 'TEXT', 'null' => 'TRUE')));
                        break;
                    #password
                    case 'password':
                        $this->dbforge->add_field(array($name => array('type' => 'VARCHAR','constraint' => '255', 'null' => 'TRUE')));
                        break;
                    
                    #datumsfeld
                    case 'date':
                        $this->dbforge->add_field(array($name => array('type' => 'DATE', 'null' => 'TRUE')));
                        break;
                    
                    #einfachauswahl per select
                    case 'select_one':
                        $this->dbforge->add_field(array($name => array('type' => 'INT', 'constraint'=>'9', 'unsigned' => 'TRUE', 'default'=>'0')));
                        break;
    
                    #mehrfachauswahl per checkboxen (als csv in textfeld speichern)
                    case 'select_many':
                        $this->dbforge->add_field(array($name => array('type' => 'TEXT', 'null' => 'TRUE')));
                        break;
                    
                    case 'number':
                        $this->dbforge->add_field(array($name => array('type' => 'INT', 'constraint'=>'9', 'unsigned' => 'TRUE')));
                        break;
                    
                    // case 'float':
                    //     $this->dbforge->add_field(array($name => array('type' => 'FLOAT', 'constraint'=>'9', 'unsigned' => 'TRUE')));
                    //     break;
                    
                    #enum aus yml
                    case 'radio':
                        $this->dbforge->add_field(array($name => array('type' => 'VARCHAR','constraint' => '255', 'null' => 'TRUE')));
                        break;
                    
                    
                    default:
                        $this->dbforge->add_field(array($name => array('type' => 'VARCHAR','constraint' => '255', 'null' => 'TRUE')));
                        break;
                }    
            }
    
            $this->dbforge->create_table($data['yml']['table']);
    
            $this->load->view('generator_result_view', $data['yml']);
        }
        
    
    }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases