We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    Quote from: AHHP at Jan 03, 2009, 02:45 PM

    Would you give me an example of codes for connecting to a remote database?

    Sure...
    <?php
    $dsn = 'mysql:host=localhost;dbname=remote;charset=utf8;collation=utf8_general_ci';
    $usr = 'root';
    $pwd = 'pwd';
    $xpdoOptions = array(
        XPDO_OPT_HYDRATE_FIELDS => true, 
        XPDO_OPT_HYDRATE_RELATED_OBJECTS => true
    );
    $pdoDriverOptions = array(
        PDO_ATTR_ERRMODE => PDO_ERRMODE_WARNING,
        PDO_ATTR_PERSISTENT => false,
        PDO_MYSQL_ATTR_USE_BUFFERED_QUERY => true
    );
    $remote = new xPDO($dsn, $usr, $pwd, $xpdoOptions, $pdoDriverOptions);
    ?>

    I generally create a class to represent my model and have it extend xPDO to abstract the connection details for various situations, for example here is a model representing a set of custom tables that reside in another database that provides easy access to a $modx instance:
    <?php
    require_once(MODX_BASE_PATH . 'xpdo/xpdo.class.php');
    class remoteModel extends xPDO {
        var $modx = null;
        function remoteModel(& $modx) {
            $this->__construct($modx);
        }
        function __construct(& $modx) {
            $this->modx = & $modx;
            parent :: __construct(
                'mysql:host=localhost;dbname=remote;charset=utf8;collation=utf8_general_ci',
                'remote',
                'foobar',
                array(
                    XPDO_OPT_TABLE_PREFIX => 'remote_',
                    XPDO_OPT_HYDRATE_FIELDS => true,
                    XPDO_OPT_HYDRATE_RELATED_OBJECTS => true,
                ),
                array(
                    PDO_ATTR_PERSISTENT => false,
                )
            );
            $this->setPackage('remote', MODX_BASE_PATH . 'assets/remote/model/');
            $this->setLogTarget('HTML');
            $this->setLogLevel(XPDO_LOG_LEVEL_ERROR);
        }
    
        // other custom functions for your remote model here
    }
    ?>

    Then to get a connection, all you need to do in a snippet is:
    <?php
    require_once(MODX_BASE_PATH . 'assets/remote/model/remote/remotemodel.class.php');
    $remoteModel = new remoteModel($modx);
    ?>


    You can also connect with PDO directly if you don’t care to have an object model or the extra features of xPDO, though there is no built-in support for setting the collation in the mysql PDO driver (that is xPDO specific)...
    <?php
    $dsn = 'mysql:host=localhost;dbname=remote;charset=utf8';
    $usr = 'root';
    $pwd = 'pwd';
    $pdoDriverOptions = array(
        PDO_ATTR_ERRMODE => PDO_ERRMODE_WARNING,
        PDO_ATTR_PERSISTENT => false,
        PDO_MYSQL_ATTR_USE_BUFFERED_QUERY => true
    );
    $remote = new PDO($dsn, $usr, $pwd, $pdoDriverOptions);
    ?>


    See more on PDO at http://php.net/manual/en/pdo.construct.php.
      • 8245
      • 37 Posts
      Quote from: OpenGeek at Jan 02, 2009, 09:28 AM

      ... I would use a single core and simply create a database configuration and manager for each domain. In this way, you share only the core files and each domain has their own database.

      OK, I’m ready to get going..
      so I have a couple pending questions.

      1: with this approach how will the upgrade process be handled for Revolution?
      2: I have read these documentation posts
      - http://svn.modxcms.com/docs/display/revolution/Fresh+Installation
      - http://svn.modxcms.com/docs/display/revolution/Contexts
      and am guessing that to share the core I do a ’Advanced Upgrade Install’ and use the ’One Gateway Plugin’. What I did not find is any clear information on how I go about moving the core, and what config info needs to be updated to have it work?

      I will clearly document my process from a newbie perspective and share once I am sucessful.

      thanks in advance...
        • 8245
        • 37 Posts
        Is it possible to set up a single core and create a database configuration and manager for each domain. Using the standard install package, or is it required to use the advanced package?
        Are there specific config options I will need to select/set to allow me to move the core?
        I am guessing that the system will need access to the core (out side the web root) so how am I to grant the system these permissions... right now I have only one user set up on my server with these rights (Root).
        I am wondering about security here?
          • 22303 MODX Staff
          • 10,725 Posts
          Quote from: DusX at Feb 25, 2009, 11:38 AM

          Is it possible to set up a single core and create a database configuration and manager for each domain. Using the standard install package, or is it required to use the advanced package?
          Are there specific config options I will need to select/set to allow me to move the core?
          Yes, this is how I do it, though I use svn to checkout the core directly at the location I’ve chosen on my server. You can use either package, though it makes more sense to use the advanced, since you will want the setup to extract the files into the document root locations you choose for each context (i.e. make sure Files Already in Place is not checked). To move the core, simply move the directory where you want it (you can even rename it) and then go into setup/includes/config.core.php and set the MODX_CORE_PATH to that location. You will also want to change MODX_CONFIG_KEY to whatever you want the configuration to be called (I typically use the domain of the site for easy identification).

          NOTE: if you’ve already installed and want to move the core, find all of the config.core.php files and make the same changes and rename the configuration file in core/config based on the MODX_CONFIG_KEY you have chosen. The setup process will update these files however if you run setup as described above.

          Quote from: DusX at Feb 25, 2009, 11:38 AM

          I am guessing that the system will need access to the core (out side the web root) so how am I to grant the system these permissions... right now I have only one user set up on my server with these rights (Root).
          I am wondering about security here?
          Well, I hope you aren’t running Apache/PHP as root, as that is a definite potential security problem. And unfortunately, this depends on your environment, including how PHP is running in Apache. FWIW, as long as the core/cache and core/packages directories are writable by the PHP user and the rest readable, the core should be secure.
            • 8245
            • 37 Posts
            OK, so I have taken a look at all the config files and it appears that the settings in the config.core.php files are rather simple.
            and I can see how the system needs these to identify the site, and the core.
            When I look at the core/config/config.inc.php I see many settings that reference the install path. (Let me note that I went ahead with the regular install and thought to manually move and config things to get a better understanding)
            It appears that the only setting I need change is:
            MODX_CORE_PATH

            so I have changed it to:
            '/home/core/'

            I have also made the matching changes to: MODX_PROCESSORS_PATH (I don’t see anyothers here that should need changing)


            and have changed the config.core.php files to use:
            define('MODX_CORE_PATH', '/home/core/');

            and
            define ('MODX_CONFIG_KEY', 'domain1');


            do I name the config.inc.php to what?? domain1.php ?
            [Edit] now that I have had a quick lunch I am thinkin a little clearer.. and have renamed the file domain1.inc.php (noting that the setting config matched the previous name.)

              • 8245
              • 37 Posts
              ok, so I thought I got things so that they should work, but I now get this error:
              Warning: main() [function.main]: open_basedir restriction in effect. File(/home/core/model/modx/modx.class.php) is not within the allowed path(s): (/home/domain1:/usr/lib/php:/usr/php4/lib/php:/usr/local/lib/php:/usr/local/php4/lib/php:/tmp)
              This was kind of what i was originally getting at when I asked about root access.
              My sites user doesn’t have access outside the site root. (in this case one dir above public_html)
              How do I work around this?
                • 22303 MODX Staff
                • 10,725 Posts
                Quote from: DusX at Feb 25, 2009, 04:21 PM

                ok, so I thought I got things so that they should work, but I now get this error:
                Warning: main() [function.main]: open_basedir restriction in effect. File(/home/core/model/modx/modx.class.php) is not within the allowed path(s): (/home/domain1:/usr/lib/php:/usr/php4/lib/php:/usr/local/lib/php:/usr/local/php4/lib/php:/tmp)
                This was kind of what i was originally getting at when I asked about root access.
                My sites user doesn’t have access outside the site root. (in this case one dir above public_html)
                How do I work around this?
                Put the core somewhere it has access to, remove open_basedir restrictions, or add the path to your open_basedir configuration.
                  • 8245
                  • 37 Posts
                  Quote from: DusX at Feb 25, 2009, 04:21 PM

                  ok, so I thought I got things so that they should work, but I now get this error:
                  Warning: main() [function.main]: open_basedir restriction in effect. File(/home/core/model/modx/modx.class.php) is not within the allowed path(s): (/home/domain1:/usr/lib/php:/usr/php4/lib/php:/usr/local/lib/php:/usr/local/php4/lib/php:/tmp)
                  This was kind of what i was originally getting at when I asked about root access.
                  My sites user doesn’t have access outside the site root. (in this case one dir above public_html)
                  How do I work around this?

                  OK, this was easily corrected by changing the security settings for my domain in my server manager.
                    • 8245
                    • 37 Posts
                    OK, now my one site is running very nicely, I have added content and things seems to be good.
                    To setup a second site with its own database.... what might the steps be?
                    Can I use the installer on the next site... and then just edit things the same way?
                    I read about context and they have been mentioned.. how do these come into play?
                      • 22303 MODX Staff
                      • 10,725 Posts
                      Quote from: DusX at Feb 25, 2009, 05:15 PM

                      OK, now my one site is running very nicely, I have added content and things seems to be good.
                      To setup a second site with its own database.... what might the steps be?
                      Can I use the installer on the next site... and then just edit things the same way?
                      Yep, just copy the setup/ directory to another virtual host or document root and edit the setup/includes/config.core.php with the core location and configuration name.

                      Quote from: DusX at Feb 25, 2009, 05:15 PM
                      I read about context and they have been mentioned.. how do these come into play?
                      They would not come in to play in separate configurations/databases. Those would be for isolating sections of a site or subdomains or even multiple domains within a single database/configuration. Completely different approaches for completely different needs, i.e. sharing just the core or sharing the site data.