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.

Simple tool for Scaffolding to manage all tables faster

November 23, 2007 9:27pm

Subscribe [9]
  • #1 / Nov 23, 2007 9:27pm

    H. Bernaciak

    3 posts

    Hello,

    I have a great deal of hope, that it help although one of You to manage all tables in Scaffolding easier and faster. I have written it for my projects and use it still as well, bacause you can access any table much quicker than manual edit table name in controller.

    <?php
    /**
     * CodeIgniter
     *
     * An open source application development framework for PHP 4.3.2 or newer
     *
     * @package        Scaffolding Manager Helper
     * @author         Hubert Bernaciak
     * @copyright      Copyright (c) 2007, Hubert Bernaciak.
     * @link           <a href="http://www.hubi.pl">http://www.hubi.pl</a>
     * @since          Version 1.0
     * 
     */
    
    // ------------------------------------------------------------------------
    class Manager extends Controller {
    
           function Manager()
           {
                parent::Controller();
                if(!$this->uri->segment(3) == '')
                {
                $this->load->scaffolding($this->uri->segment(3));
                }
           }
           function index()
           {
                $result = mysql_list_tables('homelab');
                while ($row = mysql_fetch_row($result))
                {
                echo '<a href="http://.site_urlmanager/SECRETPASSWORD./.$row0">.'"]'.$row[0].'</a>
    ';
                } 
           
           }
    }
    ?>

    Code is very simple (probably not perfect), but feel free to ask 😉

  • #2 / Nov 26, 2007 4:54pm

    daniel.affonso

    23 posts

    Congrats and thanks for sharing.

    Simplicity and result, Good!

  • #3 / Nov 29, 2007 10:55am

    sophistry

    906 posts

    Thank you! This is nice.

    Better db portablility may be achieved by using CI table listing function:
    http://ellislab.com/codeigniter/user-guide/database/table_data.html

    EDIT: edited code for even better code portability. This is not the best, long-term way to get the scaffolding_trigger word, but it works.

    // should be more portable, also not hard coded with db name
    // since it is in config file already, code not tested on multiple dbs
    // assumes db library is loaded
     function index()
           {
               $tables = $this->db->list_tables();
                foreach ($tables as $table)
                {
                echo '<a >uri->router->routes['scaffolding_trigger']).'/'.$table.'">'.$table.'</a>
    ';
                } 
           }
  • #4 / Nov 29, 2007 12:10pm

    H. Bernaciak

    3 posts

    yeah, it works also good and… is more IGNITER 😛 but everyone should remember about adding this at the beggining:

    class Manager extends Controller {
    
           function Manager()
           {
                parent::Controller();
                if(!$this->uri->segment(3) == '')
                {
                $this->load->scaffolding($this->uri->segment(3));
                }
           }
  • #5 / Nov 29, 2007 12:51pm

    sophistry

    906 posts

    the whole code for easy copying.

    <?php
    /**
    * CodeIgniter
    *
    * An open source application development framework for PHP 4.3.2 or newer
    *
    * @package        Scaffolding Manager Helper
    * @author         Hubert Bernaciak
    * @copyright      Copyright (c) 2007, Hubert Bernaciak.
    * @link           <a href="http://www.hubi.pl">http://www.hubi.pl</a>
    * @since          Version 1.0
    * 
    */
    
    // ------------------------------------------------------------------------
    class Manager extends Controller {
    
           function Manager()
           {
                parent::Controller();
                if(!$this->uri->segment(3) == '')
                {
                $this->load->scaffolding($this->uri->segment(3));
                }
           }
    
           function index()
           {
               $tables = $this->db->list_tables();
                foreach ($tables as $table)
                {
                echo '<a >uri->router->routes['scaffolding_trigger']).'/'.$table.'">'.$table.'</a>
    ';
                } 
           }
    }
    ?>
  • #6 / Dec 22, 2007 4:05am

    eljunior

    9 posts

    I get and SQL error if i try to create a new record using this.

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘add LIMIT 1’ at line 1

    Did it happen to anyone of you? Or it’s just my scaffolding that uses the third argument as the table name? (Or neither one? :D)

  • #7 / Dec 22, 2007 7:31pm

    sophistry

    906 posts

    post your error and code and you’ll get better help. but, anyway, this thread does not seem to pertain to your issue - this code is only for easily creating hyperlinks to scaffolding rather than creating records.

    you have a SQL syntax error: ‘add LIMIT 1’ is being rejected. this little helper code shouldn’t be the root of the problem. most likely you have a reserved word as a fieldname.

  • #8 / Dec 22, 2007 8:25pm

    eljunior

    9 posts

    sorry, I didn’t explain it well.
    the problem is, if i’m using correctly, the hyperlinks it’s creating looks like this:

    codeigniter/index.php/manager/SCAFF_TRIGGER/yourtable

    which works fine, opening the scaffolding page for each table.
    problem is, the scaffolding pages generated use links like this:

    codeigniter/index.php/manager/SCAFF_TRIGGER/add

    that’s why it shows that SQL error.
    (i don’t know if there is someone who would use ‘add’ for a table name… :D)

    am I using this thing right?
    by the way, thanks for the attention, sohpistry. 😉

  • #9 / Dec 22, 2007 11:44pm

    sophistry

    906 posts

    ok, eljunior… i have to admit that when i posted my code changes they were untested; i had not actually taken it out for a spin, i just assumed that the OP had gotten this to work in his setup and i was just coming along to make the code a little more portable.

    really, i only posted to this thread because it was next door to a really nice thread about a working set of code called Matchbox. 😉 you should check it out if you haven’t already.

    as you’ve discovered. this code does not work as indicated.

    i just tried it out and got the same error you did. SQL error complaining about add.

    well, the reason it doesn’t work took me a while to figure out… the scaffolding is only loaded properly when there is a tablename passed into its load function. so, it works fine to “get” to the list of records in scaffolding.

    but, once you start to try to do anything, the scaffolding is the one in charge and it doesn’t pass the table name along anymore, so the “manager” constructor actually passes the command “add” to SQL as the tablename. “add” is the 3rd segment and you see in the constructor that the 3rd segment is passed as the tablename. i’m sure that this code never actually *worked* for anyone - it gets you *to* the scaffolding record list, but then you can’t do anything.

    anyhow, i started trying to amend the code with an idea that you could store the actual tablename you were working in scaffolding (in a session var) and then pass it along to the scaffolding loader in the constructor but then i decided that the whole idea of automating scaffolding using this architecture was flawed to begin with. it could be done with the session var, but then you could only have one scaffolding “window” open at a time.

    that’s when i came back here to let you know my findings.

    sorry for making it look like i was using the code! my bad.

    welcome to CI, BTW. 😊

  • #10 / Dec 23, 2007 11:38am

    eljunior

    9 posts

    I forgive you for making it look like you were using this and you forgive me for don’t explaining the problem in detail… (though maybe I would have done this, if you didn’t have done that. :-D :-D)

    well, I think it’s better I try to find another way to do this, then…

    thanks a lot!

  • #11 / Dec 25, 2007 12:01am

    sophistry

    906 posts

    forgive me for correcting the english, (assuming you are spanish speaking with a name like eljunior! - el modo subjunctivo es dificil en inglés también:

    instead of what you have above… these small corrections make it sound perfect (and still funny like the original!):

    I forgive you for making it look like you were using this and you forgive me for not explaining the problem in detail… (though maybe I would not have done this in the first place, if you hadn’t done that. :-D :-D)

    well, I think it’s better if I try to find another way to do this, then…

  • #12 / Dec 25, 2007 1:00am

    eljunior

    9 posts

    oh, no! I already forgave you once, now I’ll have to forgive you one more!

    correcting my english is ok, but assuming I’m spanish… (soy brasileño, ¡hombre viejo! 😊 pero no te preocupes, me gustaría mucho ser uruguayo… :-D y mi nombre es “Elias Junior”, por eso “eljunior”. 😉)

    thanks for the corrections, it actually looks funnier when corrected… 😊

    well, since you seems to use CI seriously and I’ve got your attention, I’d like some
    advice in the following (I know it’s not the appropriate thread, but if this turns in
    something good, we’ll create a thread for it):

    I wanted to be able to use a dropdown for the foreign keys in scaffolding,
    so I created this method in system/scaffolding/Scaffolding :

    function _prepare_field_data($field_data)
            {
                    foreach($field_data as $i => $field){
                            $table=null;
                            if(ereg('_id$',$field->name)){ // matches table_id
                                    $table=ereg_replace('_id$','',$field->name);
                            }
                            if(ereg('^id_',$field->name)){ // matches id_table
                                    $table=ereg_replace('^id_','',$field->name);
                            }
                            if(! ($table && $this->CI->db->table_exists($table)) ){
                                    continue; // isn't a reference, skip it
                            }
    
                            $options = array(); // sets up an array of options
                            $query=$this->CI->db->get($table);
                            foreach($query->result_array() as $row){
                                    // this assumes that first field is id,
                                    // followed by a field description
                                    $options[ current($row) ] = next($row);
                            }
    
                            $field_data[$i]->options=$options;
                            // changes field type for testing in the view
                            $field_data[$i]->type='reference';
                    }
                    return $field_data;
            }

    and called it from methods add:

    'fields' => $this->_prepare_field_data($this->CI->db->field_data($this->current_table)),

    and in method edit:

    'fields'        => $this->_prepare_field_data($query->field_data()),

    and finally added an elsif in the views for showing the dropdown:

    <?php elseif ($field->type == 'reference'): // cont. acoxambre EDSJ ?>
            <td><?  echo form_dropdown($field->name,$field->options); ?></td>

    now, I want to know if this thing is a good practice, if there are problems in this approach
    or if it makes sense to have something like this in CI and if CI is likely to have something like that, and if it can be good just for me, or if you could use something like this,
    and probably more questions, depending on your answer… :-D

    ¡muchas gracias, muchacho!
    y ¡feliz navidad! :-D

  • #13 / Dec 27, 2007 1:08pm

    sophistry

    906 posts

    haha… well, actually, i didn’t assume you were “spanish” only “spanish speaking” (which is *way* different and in my mind something of a compliment! 😉 )

    regarding your foreign key approach… here are a few thoughts in random order:

    in general, i believe that changing core files is not a GoodIdea; that sentiment is echoed throughout the CI forums. things change and then your mods break and it’s annoying.
    scaffolding in CI is useful but, not something to build an application around. i think there are other approaches with similar (development-easing) results. the “Rails Inspired Active Record” thread comes to mind.  http://ellislab.com/forums/viewthread/61192/
    i personally would not use this approach - i typically write small applications with highly customized interfaces for each one and i only rely on scaffolding for the managing the first few test datapoints.

    HTH.

  • #14 / Dec 27, 2007 1:40pm

    eljunior

    9 posts

    yeah, yeah, well I’m not spanish spanish, though I sometimes try to be a spanish speaking or a spanish singing, I don’t know which one I fail best…
    but thanks for the something of a compliment in your mind, anyway! :-D

    and “yeah, yeah” again, I know that scaffolding is just something to easily create some data faster
    when in development phase (I use to do this in the mysql prompt, so scaffolding help to go a little faster),
    I just thought that could be a little easier having a dropdown instead of a simple input text for
    “the first few test datapoints”... 😊

    the major problem with this stupid idea of mine is what you have said about the core files…
    anyway, it’s stupid… but I think will keep using it until it gets broken, cause I’m stupid as well! :D

    thanks again!
    HAPAH.  <-  (“Hope Another Palindrome Acronym Helps”, if you guessed it, won a cookie!)

  • #15 / Jan 05, 2008 6:39am

    chiros

    1 posts

    I found problem like #6 so I have an idea to fix it by using session .

    parent::Controller();    
    
    $this->load->library('session');
    if(!$this->uri->segment(3) == '') {
            $seg3 =  $this->uri->segment(3);
            if($seg3 !="add" &&  $seg3 !="view" && $seg3 != "edit" && $seg3 != "delete")
                    $this->session->set_userdata('scaffolding_tbname', $this->uri->segment(3));
            $this->load->scaffolding($this->session->userdata('scaffolding_tbname'));
    }
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases