We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3749
    • 24,544 Posts
    Quote from: anso at Oct 19, 2010, 06:15 AM

    Hello

    @kudolink : To understand the ’from schema to tables’ way, I recommend you to download modExtra. It’s a sample (and simple) to build a simple 3PC. It’s an excellent starter point... Have a particular look at the "model/" directory and at the "_build" directory...
    Hope this will point you on the right direction
    Cheers
    Note that, at present, there are some things that can’t be done that way -- e.g. FULLTEXT compound keys, though this will probably change soon.
      Did I help you? Buy me a beer
      Get my Book: MODX:The Official Guide
      MODX info for everyone: http://bobsguides.com/modx.html
      My MODX Extras
      Bob's Guides is now hosted at A2 MODX Hosting
    • OK still not working,

      I have a table in my DB called bobs_quotation, I have 1 insert in the table to make sure it’s populated.

      I created a snippet called CreateXpdoClasses, I changed the path to reflected my server. I tied just /core/ and /home/mysite/public_html/core/

      /**
      * @package = CreateXpdoClasses
      *
      * Create Xpdo Classes script
      *
      * This script creates xPDO-ready classes from existing custom
      * database tables. It only needs to be run once.
      *
      * It works best when your custom tables have been imported into
      * the MODx DB and use a different table prefix than the MODx tables.
      * In theory, you can use a separate DB but this has not been tested
      * and the process of using the classes would be more complicated.
      *
      *
      * Note: If you are running this outside of MODx and testing it
      * in the Manager, it will log you out when it runs, even though
      * the Manager screen will still be visible. Actions taken in the
      * Manager (e.g., saving a snippet) will not be successful. After
      * running the script, reload the Manager page in your browser
      * and you will be prompted to log back in.
      *
      *
      */
      /* assume we're in a snippet */
      $outsideModx = false;
      
       if (!defined('MODX_CORE_PATH')) {
           $outsideModx = true;
          /* put the path to your core in the next line to run outside of MODx */
          define(MODX_CORE_PATH, '/home/mysite/public_html/core/');
          include_once MODX_CORE_PATH . '/model/modx/modx.class.php';
          $modx= new modX();
          $modx->initialize('mgr');
      }
      
      /* set these if running outside of MODx */
      
      if ($outsideModx) {
          $myPackage = 'mypackage';
      
          /* table prefix; must match the prefix of the tables to process */
          $myPrefix  = 'bobs_';
      
          /* optional -- only if your table prefix is the same as the modx prefix */
          // $myTables = 'bobs_quotation';
      }
      
      
      
      /* These two switches let you write the schema and/or create the classes;
       * useful for debugging */
      $createSchema = empty($createSchema)? true : $createSchema;
      $createClasses = empty($createClasses) ? true : $createClasses;
      
      /* Used to include the phpDoc templates below or other custom templates */
      $includeCustomTemplates = empty($includeCustomTemplates)? false : $includeCustomTemplates;
      
      /* $pkg is the name of your package. Use this in your addPackage() call to load all
       * classes created by this script. The class files will be created
       * under the /core/components/$myPackage/model/ directory.
       * Example:
       *     $myPackage = 'quotes';
       *     $myPrefix = 'bobs_';
       *     $path = MODX_CORE_PATH . 'components/' . $myPackage . '/';
       *     $result = $modx->addPackage($myPackage, $path . 'model/', $myPrefix);
       *     if (! $result) {
       *         return('Failed to add package');
       *     }
       */
      $myPackage = empty($myPackage)? 'mypackage' : $myPackage;
      
      /* table prefix; must match the prefix of the tables you want to process */
      $myPrefix = empty ($myPrefix)? '' : $myPrefix;
      
      $myTables = empty($myTables)? '' : $myTables;
      
      /* Table names to process -- this is only necessary if your table prefix is the same
       * as that of the MODx tables. You can send a comma-separated list of full table names;
       * in that case the class name will be the table name minus the prefix with any
       * underscores removed and any letter after an underscore in upper case.
       *
       * You can also send an array of arrays of tableName => className, which allows you
       * to specify the exact class name rather then letting MODx create it from the
       * table name. Each inner array specifies a full table name and the class name to use.
       * Examples:
      
       $tables = 'bobs_quotation';
      
       $tables = array(
           array(
              'bobs_quotation'=>'bobQuotation'
           )
       );
      
      */
      
      
      /* You shouldn't need to modify the code beyond this point
      ********************************************************** */
      
      $sources = array(
          'config' => MODX_CORE_PATH . 'config/config.inc.php',
          'package' => MODX_CORE_PATH . 'components/' . $myPackage . '/',
          'model' => MODX_CORE_PATH. 'components/' . $myPackage . '/model/',
          'schema' => MODX_CORE_PATH . 'components/' . $myPackage . '/schema/',
          );
      
      if (! file_exists($sources['package'])) {
          mkdir($sources['package']);
      }
      
      if (! file_exists($sources['core'])) {
          mkdir($sources['core']);
      }
      if (! file_exists($sources['model'])) {
          mkdir($sources['model']);
      }
      if (! file_exists($sources['schema'])) {
          mkdir($sources['schema']);
      }
      require_once $sources['config'];
      $dbase = str_replace('`','',$dbase);
      $connectString = $database_type . ':' . 'host=' . 
         $database_server . ';' . 'dbname=' . $dbase;
      
      /* note: $tablePrefix is the modx table prefix - not your prefix;
       * should be set automatically */
      $xpdo = new xPDO('mysql:host=localhost;dbname=test',
         $database_user,$database_password,$table_prefix);
      
      $xpdo->setLogLevel(xPDO::LOG_LEVEL_INFO);
      $xpdo->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
      
      $manager= $xpdo->getManager();
      $generator= $manager->getGenerator();
      
      if ($includeCustomTemplates) {
          customTemplates($generator);
      }
      
      
      $file = $sources['schema'] . $myPackage . '.mysql.schema.xml';
      
      // echo '<br />File: ' . $file;
      
      /*    boolean writeSchema (
       *        string $schemaFile,       full path to the schema file you want to write
       *        [string $package = ''],   name of your component
       *        [string $baseClass = ''],  xPDO base class to use (send '' if using args below)
       *        [string $tablePrefix = ''],  table prefix (of tables to process)
       *        [boolean $restrictPrefix = false]), process only tables with the above prefix
       *        [mixed $tableList = ''              array of arrays of full-table-name=>className
                                                    or a string with a comma-separated list of full
                                                    table names; if you send the string
       *                                            the table name will be used as the class name.
      */
      
      if ($createSchema) {
      
          $xml= $generator->writeSchema($file,$myPackage,'',$myPrefix,true,$myTables);
      
          if ($xml) {
             $xpdo->log(xPDO::LOG_LEVEL_INFO, 'schema file written to ' . $file);
          } else {
              $xpdo->log(xPDO::LOG_LEVEL_INFO, 'Error writing schema file');
          }
      }
      
      if ($createClasses) {
           if ($generator->parseSchema($file, $sources['model'])) {
               $xpdo->log(xPDO::LOG_LEVEL_INFO, 'Schema file parsed .
                   Files written to '. $sources['model']);
           } else {
               $xpdo->log(xPDO::LOG_LEVEL_INFO, 'Error parsing schema file');
           }
      }
       $xpdo->log(xPDO::LOG_LEVEL_INFO, 'FINISHED');
       exit();
      
       function customTemplates($generator) {
      $generator->classTemplate= <<<EOD
      <?php
      /**
       * 
       * 
       */
      class  extends  {
      }
      ?>
      EOD;
      $generator->platformTemplate= <<<EOD
      <?php
      /**
       * 
       * 
       */
      require_once (dirname(dirname(__FILE__)) .
           '/.class.php');
      class _ extends  {
      }
      ?>
      EOD;
      $generator->mapHeader= <<<EOD
      <?php
      /**
       * 
       * 
       */
      EOD;
      }



      I created a blank document, with a blank template with this snippet call. [[CreateXpdoClasses? &myPackage=`quotes` &myPrefix=`bobs_`]]

      I created a folder (quotes) in /home/mysite/public_html/core/components/

      I went to the page with the snippet and I still get this error.

      [2010-10-19 19:40:36] (ERROR in xPDO::connect @ /home/mysite/public_html/core/xpdo/xpdo.class.php : 318)
      
      SQLSTATE[28000] [1045] Access denied for user 'nobody'@'localhost' (using password: NO)
      
      
      Fatal error: Call to a member function execute() on a non-object in /home/mysite/public_html/core/xpdo/om/mysql/xpdogenerator.class.php on line 103
      


      In the quotes folder there are 2 directories made, model and schema but both are empty.

      I must be doing something wrong? huh


      Do I need to enable this part of the code.

        $myPackage = 'quotes';
           $myPrefix = 'bobs_';
           $path = MODX_CORE_PATH . 'components/' . $myPackage . '/';
           $result = $modx->addPackage($myPackage, $path . 'model/', $myPrefix);
           if (! $result) {
               return('Failed to add package');
           }


      I do not get that last error but it now says Failed to add package if I do. grin
        Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
        AugmentBLU - MODX Partner

        BLUcart - MODX Revolution E-Commerce & Shopping Cart
        • 3749
        • 24,544 Posts
        Oops. I see what the problem is.

        Change:

        $xpdo = new xPDO('mysql:host=localhost;dbname=test',
           $database_user,$database_password,$table_prefix);
        

        To this:

        $xpdo = new xPDO($connectString,
           $database_user,$database_password,$table_prefix);
        


        If that doesn’t work, change it back and change the dbname in it from "test" to the name of your database.

        Sorry about the confusion.


        FYI: The code with addPackage() in is for snippets that use the classes, it doesn’t go in the CreateXpdoClasses snippet.
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
        • Thanks Bob, I now get this error using

          $xpdo = new xPDO($connectString,
          $database_user,$database_password,$table_prefix);
          
          Fatal error: Class 'xPDODriver_' not found in /home/mysite/public_html/core/xpdo/xpdo.class.php on line 1269


          I get this error if I use

          $xpdo = new xPDO('mysql:host=localhost;dbname=mysite_rev',
             $database_user,$database_password,$table_prefix);
          
          [2010-10-19 21:01:46] (ERROR in xPDO::connect @ /home/mysite/public_html/core/xpdo/xpdo.class.php : 318)
          
          SQLSTATE[28000] [1045] Access denied for user 'nobody'@'localhost' (using password: NO)
          
          Fatal error: Call to a member function execute() on a non-object in /home/mysite/public_html/core/xpdo/om/mysql/xpdogenerator.class.php on line 103
          
          
            Ross Sivills - MD AugmentBLU Edinburgh, Scotland UK
            AugmentBLU - MODX Partner

            BLUcart - MODX Revolution E-Commerce & Shopping Cart
            • 3749
            • 24,544 Posts
            Sorry Rossco,

            There’s a whole new version of the code up at Bob’s Guides. It should be more bulletproof.
            http://bobsguides.com/custom-db-tables.html

            You shouldn’t need to modify anything in the snippet code to make it work. Be sure to log out and back in before pasting it in the snippet, the previous version confused MODx somewhat.

            Let me know.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 3749
              • 24,544 Posts
              Snippet code now available for download. See the message at the top of this thread.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 16429
                • 254 Posts
                I was getting all the errors Rossco had, then I changed the code to the new one and now I’m getting:
                Fatal error: Call to undefined method xPDOManager_mysql::getPhpType() in /path/to/my/core/xpdo/om/mysql/xpdogenerator.class.php on line 140


                My fault?
                  kudo
                  www.kudolink.com - webdesign (surprised?)

                  [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]
                  • 28215
                  • 4,149 Posts
                  What version of Revo are you on?
                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 3749
                    • 24,544 Posts
                    Quote from: kudolink at Oct 20, 2010, 03:52 AM

                    I was getting all the errors Rossco had, then I changed the code to the new one and now I’m getting:
                    Fatal error: Call to undefined method xPDOManager_mysql::getPhpType() in /path/to/my/core/xpdo/om/mysql/xpdogenerator.class.php on line 140


                    My fault?

                    Probably not. Can you try downloading the snippet code (see the top message). That will give me a more accurate line number if the error is still there.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 16429
                      • 254 Posts
                      Quote from: splittingred at Oct 20, 2010, 04:00 AM

                      What version of Revo are you on?

                      2.0.3
                      Need to update to get rid of that?
                        kudo
                        www.kudolink.com - webdesign (surprised?)

                        [img]http://www.kudolink.com/kudolinkcom.png[/img] [sup]proudly uses[/sup] [img]http://www.kudolink.com/modx.png[/img]