Welcome Guest, Not a member yet? Register   Sign In
Dynamic Member Functions in CodeIgniter
#1

[eluser]Davvolun[/eluser]
Okay I'd like to believe this will be interesting to others...

Let's say I have a MySQL database with a lot of tables, and I'd like to provide the basic scaffolding for each of them. One way I could do that would be to create a function for each table that redirects to the proper url i.e.

Code:
class Welcome extends Controller {
  function Welcome() {
    parent::Controller();
    $this->load->helper('url');

    $tables = $this->db->list_tables();
    foreach( $tables as $table ) {
      $this->load->scaffolding( $table );
    }
  }
  ...
  function proxyFunction( $tableName ) {
    //ehh I think this is right, close enough for the example
    redirect( 'welcome/' . $tableName . '/scaffolding/' );
  }
  ...
  function tableNameOne() {
    proxyFunction( 'tableNameOne' );
  }
  function tableNameTwo() {
    proxyFunction( 'tableNameTwo' );
  }
  ...
}

Problem is I need to create a function for each table (replicating the database structure), and if the database changes (say, on early prototyping), then I need to change the controller also.

Does anyone know if I can do something like this (the following code does not work, but can it be changed to work???):

Code:
class Welcome extends Controller {
  function Welcome() {
    parent::Controller();
    $this->load->helper('url');

    $tables = $this->db->list_tables();
    foreach( $tables as $table ) {
      $this->load->scaffolding( $table );
      
      //new code
      $lcTable = strtolower($table);
      $this->$lcTable = &$this->proxyFunction; //this line fails to work as hoped

      $this->$lcTable(); // <----- causes a PHP error
    }
  }
  ...
  function proxyFunction() {
    $table = basename(uri_string());
    redirect( "welcome/" . $table . "/scaffolding/" );
  }
  ...
}

I can use a member variable as a work-around, such as

Code:
class Welcome extends Controller {
  private $dynamicFunctions = array();
  ...
  function Welcome() {
    ...
    foreach( $tables as $table ) {
      $this->load->scaffolding( $table );
      
      //new code
      $lcTable = strtolower($table);
      $this->{$this->dynamicFunctions[$lcTable]} = &$this->proxyFunction;

      $this->{$this->dynamicFunctions[$lcTable]}();
    }
  }
  ...
}

But CodeIgniter won't find the proxied function through the member variable now. Additionally, I can use __get and __set, but then CI can't find stuff it needs (if anyone thinks the last example is a worthy area of pursuit, I'll post my code for that).

Note: obviously in each of these I'm just trying to see if the PHP code was valid by calling the proxied function; in a real application, I'd remove that and use the URL with CI calling the function.

Solutions? Worthy problem for discussion? Airspeed velocity of an unladen swallow?
#2

[eluser]TheFuzzy0ne[/eluser]
Code:
class Welcome extends Controller {
  function Welcome() {
    parent::Controller();
    $this->load->helper('url');

    $tables = $this->db->list_tables();
    foreach( $tables as $table ) {
      $this->load->scaffolding( $table );
    }
  }
  ...
  function proxyFunction( $table_name ) {
    //ehh I think this is right, close enough for the example
    redirect( 'welcome/' . $table_name . '/scaffolding/' );
  }

  function table($table_name="") {
    proxyFunction( $table_name );
  }
  ...
}
#3

[eluser]Davvolun[/eluser]
Hmmph, apparently quite a bit less interesting than I'd hoped. Thanks for that though, seems to be a great solution. Although, the scaffolding bit isn't working as hoped, but I'll look into that.
#4

[eluser]Jondolar[/eluser]
Why are you redirecting? Why don't you pass the table name as the 3rd parameter in the url and then you can use it as a parameter to a generic function

/scaffolding/view/table1
/scaffolding/view/table2

I'm assuming you are going to perform the exact same function on each table. If not, then you'll need to design a function specific to each table anyways so you wouldn't have the problem.




Theme © iAndrew 2016 - Forum software by © MyBB