nuts and bolts of cakephp

Changing model’s table from the controller

Posted in CakePHP by teknoid on November 19, 2008

For one reason, or another you might wish to change your Model’s table on the fly…

It would seem that it should be quite easy to do with:


$this->Company->useTable = 'another_company_table';

…but it’s not going to work…

Instead, in your controller, use:


$this->Company->table = 'another_company_table';

I haven’t checked to see why this works the way it does, so if someone has an explanation, I’ll gladly update the post with your insight.

P.S. Phally pointed out that it is better is use the wrapper for setting the table variable (always better than setting vars directly), like so: $this->Company->setSource('another_company_table');

11 Responses

Subscribe to comments with RSS.

  1. Phally said, on November 19, 2008 at 7:48 pm

    I used this and it works, it is probably a wrapper:

    $this->Company->setSource('another_company_table');

    I got it from AD7six.

  2. teknoid said, on November 19, 2008 at 11:04 pm

    @Phally

    Yep, thanks.

  3. keymaster said, on November 20, 2008 at 8:40 am

    Why would you want to change your model’s table?

  4. teknoid said, on November 20, 2008 at 11:18 am

    @keymaster

    I would not, but there are case where someone would… Perhaps based on the logged in user (tracking).
    Or if you have a middle-tier system that requires dynamic table switching, there are certain scenarios where this could be useful.

    Just showing the available option, how it’s used… well that’s, up to the developer.

  5. Phally said, on November 29, 2008 at 6:46 am

    @keymaster

    I used it to get data from a remote database where the tablename came from the local database. So I had a Remote model which changes tablename.

  6. MistaJJ said, on February 6, 2009 at 9:15 am

    Wow Phally, you are amazing!

  7. teknoid said, on February 6, 2009 at 10:04 am

    @MistaJJ

    :)

  8. Deckard said, on March 7, 2009 at 9:55 am

    For a completely dynmic table with an unusual table name (eg. ‘results_1′) I found it to be working only when setting both properties.

    $this->Company->useTable = ‘another_company_table’;
    $this->Company->table = ‘another_company_table’;

    But then it worked even inside the model!

    Thank you for this hint – will save lots of time for the future!

  9. teknoid said, on March 7, 2009 at 4:20 pm

    @Deckard

    Thanks for sharing, that’s an interesting observation.

  10. empe said, on June 6, 2009 at 12:34 pm

    thanks teknoid

    this is what i need

    i need a code that can change table for my multilanguage site

    example

    en_contents for english content
    nl_content for dutch content

    but only use one model


    class Contents extends AppModel
    {
    $useTable = "en_contents"; //for default language
    }

    and for controller


    class ContentsController extends AppController
    {

    var $use = array('Content');

    function get($lang)
    {

    if($lang == 'nl') :

    $this->Content->setSource('nl_contents');

    endif;

    }
    }

  11. teknoid said, on June 7, 2009 at 8:54 am

    @empe

    Cool, thanks for sharing… have you tried the translate behavior, seems like it would do what you are after more efficiently. Unless you have a legacy DB to work with.


Leave a Reply