We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38954
    • 24 Posts
    If i make my xPDO model, i use this constructor in the top of every snnipet i work with my model.

    $dsn = 'mysql:host=localhost;dbname=test;port=3306;charset=utf-8';
    $xpdo = new xPDO($dsn,'username','password');


    Any way i can avoid it? Put constructor code ones somewhere and use it in every snippet.
    Thx.
      from Russia with love
      • 34012
      • 88 Posts
      You could use xPDO service layer http://rtfm.modx.com/display/xPDO20/As+Service+Layer and call your data on class __construct()

      Apparently you are using MODX so you can call the service layer with plugin on WebPageInit or any plausible event you perfect.. Then it should be available on $modx->youService and change the $xpdo->addService to $modx->addService

      This is just an idea, haven't tried it for the plugin idea smiley
        Almost retired from web-development industry but still randomly writing at Lazylegs.info and on schedule hopefully in near future to finish Oracle and PostgreSQL ports of MODX
        • 22303 MODX Staff
        • 10,725 Posts
        I hope this is only used for connecting to an external database by very infrequent requests. If not, you might consider reducing overhead by moving the tables into the same database container as MODX; having two database connections is never going to be an efficient approach FWIW.
          • 38954
          • 24 Posts
          Quote from: opengeek at Apr 16, 2012, 08:02 AM
          I hope this is only used for connecting to an external database by very infrequent requests. If not, you might consider reducing overhead by moving the tables into the same database container as MODX; having two database connections is never going to be an efficient approach FWIW.
          I have my custom table which is located in the same database.
          You are right, i comment line codes where's i have this constructor and everything works fine, for mostly of time, if i use now direct sql with xPDOCriteria.
          But i think if i want play with xPDO stuff, i should make something like
          $core = $modx->getOption('core_path');
          $xpdo->addPackage('forum', $core.'model/');
          

          and this need $xpdo constructor for work right?
            from Russia with love
            • 22303 MODX Staff
            • 10,725 Posts
            The modX class extends xPDO, i.e. it is xPDO.