On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • I am trying to use xPDO outside MODX. I use the following php code, which connects to my DB (successfully) and tries to generate xPDO classes. Everything runs just fine, except in $target directory, instead of real php classes, php files with the following content are crated.

    [+class-header+]
    [+class-declaration+]
    [+class-traits+][+class-constants+][+class-properties+][+class-methods+][+class-close-declaration+][+class-footer+]
    


    require __DIR__ . '/vendor/autoload.php';
    
    $xpdo = \xPDO\xPDO::getInstance('aMySQLDatabase', [
        \xPDO\xPDO::OPT_CACHE_PATH => __DIR__ . '/../cache/',
        \xPDO\xPDO::OPT_HYDRATE_FIELDS => true,
        \xPDO\xPDO::OPT_HYDRATE_RELATED_OBJECTS => true,
        \xPDO\xPDO::OPT_HYDRATE_ADHOC_FIELDS => true,
        \xPDO\xPDO::OPT_CONNECTIONS => [
            [
                'dsn' => 'mysql:host=myserver.com;dbname=myDB_name;port=3306;charset=utf8',
                'username' => 'username',
                'password' => '********',
                'options' => [
                    \xPDO\xPDO::OPT_CONN_MUTABLE => true,
                ],
                'driverOptions' => [],
            ],
        ],
    ]);
    
    echo $o = ($xpdo->connect()) ? 'Connected' : 'Not Connected';
    
    $manager= $xpdo->getManager();
    $generator= $manager->getGenerator();
    
    $schema = './vendor/schema/expharm.mysql.schema.xml';
    $target = './vendor/model/';
    $generator->parseSchema($schema,$target);
    


    Is there a way, hwo can I force
    $generator->parseSchema($schema,$target);
    

    not to use MODX features? Or is there any workaround? I will be grateful for any advice.

    Quant.
    • You are using the 3.x branch of xPDO which has a new way to generate classes built into a CLI script. You will need to execute a command from the terminal in the following format:

      bin/xpdo parse-schema [[--config|-C]=CONFIG/FILE] [[--compile|-c]|--update=[0-2]|--regen=[0-2]] PLATFORM SCHEMA_FILE PATH


      The --update option has the following possible values:
      0=no, 1=update platform classes, 2=update all classes

      The --regen option has the following possible values:
      0=no, 1=regenerate platform classes, 2=regenerate all classes


      If you installed xpdo into your project using composer, this script will be in vendor/bin/xpdo (or wherever you configured your bin dir if you explicitly set this). So an example command in your project might be:

      vendor/bin/xpdo parse-schema --config=config/config.php mysql db/my.schema.mysql.xml src/model/
      • Also, you can see how this calls the code you were attempting to call here
        • Thank you. Using this command, my classes seems to be generated correctly.
          vendor/bin/xpdo parse-schema mysql vendor/schema/expharm.mysql.schema.xml vendor/model/
          


          but I think, there must be some modification in accesing the generated classes as well. Users is successfully generated class which I want to access. What is recommended way to do that? The old way I was used to is not working anymore. Every line, I have commented as ERROR is reached at runtime.
          require __DIR__ . '/vendor/autoload.php';
          
          $xpdo = \xPDO\xPDO::getInstance('aMySQLDatabase', [
              \xPDO\xPDO::OPT_CACHE_PATH => __DIR__ . '/../cache/',
              \xPDO\xPDO::OPT_HYDRATE_FIELDS => true,
              \xPDO\xPDO::OPT_HYDRATE_RELATED_OBJECTS => true,
              \xPDO\xPDO::OPT_HYDRATE_ADHOC_FIELDS => true,
              \xPDO\xPDO::OPT_CONNECTIONS => [
                  [
                      'dsn' => 'mysql:host=expharm.miniserver.cz;dbname=modx_obchmodel;port=3306;charset=utf8',
                      'username' => 'username',
                      'password' => '********',
                      'options' => [
                          \xPDO\xPDO::OPT_CONN_MUTABLE => true,
                      ],
                      'driverOptions' => [],
                  ],
              ],
          ]);
          
          echo $o = ($xpdo->connect()) ? 'Connected' : 'Not Connected';
          
          // this is OK
          if ( ! $xpdo->addPackage('expharm', __DIR__ . '/vendor/model/', 'expharm_') ) {
            die('error loading package');
          }
          
          if ( ! $xpdo->loadClass('Users', __DIR__ . '/vendor/model/') ) {
              // ERROR
          }
          
          $users = $xpdo->getCollection( 'Users', array('last_name:LIKE' => '%') );
          if ( ! $users ) {
             // ERROR
          }
          
          $user = $xpdo->getObject('Users',1);
          if ( ! $user ) {
             // ERROR
          }
          
          • The generated classes are namespaced now. What namespace did you give your schema? You can see what this is in the generated classes...