We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14147
    • 26 Posts
    Thanking everyone in advance for what will probably be a silly school boy error on my part.

    I'm currently converting a website built on Evolution to Revolution and looking to use the database models. I have an external database table (although it is in the MODX database) which contains details about football teams. But every time I to try and use the CMP Generator and then access the database table, I get the following error:

    [2012-08-27 19:40:36] (ERROR @ /draytongrange/index.php) Could not get table class for class: teamDetails
    [2012-08-27 19:40:36] (ERROR @ /draytongrange/index.php) Could not get table name for class: teamDetails
    [2012-08-27 19:40:36] (ERROR @ /draytongrange/index.php) Error 42000 executing statement: 


    My schema for the table is as below:

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="teamDetails" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
    	<object class="TeamDetails" table="teamDetails" extends="xPDOSimpleObject">
    		<field key="teamAge" dbtype="tinyint" precision="4" phptype="integer" null="false" />
    		<field key="teamName" dbtype="varchar" precision="25" phptype="string" null="true" />
    		<field key="managerName" dbtype="varchar" precision="50" phptype="string" null="true" />
    		<field key="leagueLink" dbtype="varchar" precision="256" phptype="string" null="true" />
    		<field key="leagueName" dbtype="varchar" precision="56" phptype="string" null="true" />
    		<field key="displayName" dbtype="varchar" precision="56" phptype="string" null="true" />
    		<field key="reportTo" dbtype="varchar" precision="128" phptype="string" null="true" />
    		<field key="leagueEmail" dbtype="varchar" precision="128" phptype="string" null="true" />
    	</object>
    </model>


    My class file for this contains:

    <?php
    class TeamDetails extends xPDOSimpleObject {}


    And my code to try and access this database table is :

    <?php
    // add package so xpdo can be used:
    	$package_path = $modx->getOption('core_path').'components/teamDetails/model/';
    
    	// see the scheme file and the xml model element and you will see the attribute package and that must match here
    	$modx->addPackage('teamDetails', $package_path);
    
    	$c = $modx->newQuery('teamDetails');
    	$c->select(array('id'));
    	$c->where(array(
    	   'team' => 1,
    	));
    	print "-->".count($modx->getCollection('teamDetails', $c))."<-- Records";


    The worst thing is, I've already got this working (by hook or by crook) three times already but this time it doesn't want to play. I would also like to know what I am doing wrong such that I don't have this problem in the future.

    I've rebuilt it many times, deleted and started again, used all lower case - but to no avail.

    Regards
      • 4172
      • 5,888 Posts
      you have
      <object class="TeamDetails"
      and

      $modx->newQuery('teamDetails')


      lower/uppercase

      also, can you show us a mysql-dump of your table-structure?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 14147
        • 26 Posts
        The object class stating "TeamDetails" was created by CMP Generator despite me putting in the name of "teamDetails". I also have another table called "playerStats" which works but once again in the object it has the text "PlayerStats". I did try with all lowercase, and even went through the class's making sure that everything was in lower case.

        -- --------------------------------------------------------
        
        --
        -- Table structure for table `teamDetails`
        --
        
        CREATE TABLE IF NOT EXISTS `teamDetails` (
          `id` tinyint(4) NOT NULL AUTO_INCREMENT,
          `teamAge` tinyint(4) NOT NULL,
          `teamName` varchar(25) DEFAULT NULL,
          `managerName` varchar(50) DEFAULT NULL,
          `leagueLink` varchar(256) DEFAULT NULL,
          `leagueName` varchar(56) DEFAULT NULL,
          `displayName` varchar(56) DEFAULT NULL,
          `reportTo` varchar(128) DEFAULT NULL,
          `leagueEmail` varchar(128) DEFAULT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
          • 14147
          • 26 Posts
          I hate it when you've been away and forgotten something important about test snippets. My school boy error is that for some reason, GetCollection doesn't work instead, I needed to have the code below:

          // Add package so xpdo can be used:
          	$package_path = $modx->getOption('core_path').'components/teamDetails/model/';
          	$modx->addPackage('teamDetails', $package_path);
          
          	// Get the players who have been awarded a MOM, grouped by name.
          	$c = $modx->newQuery('TeamDetails');
          	$c->select('*');
          	$c->where(array(
          	   'id' => '1',
          	));
          
          	if ($c->prepare() && $c->stmt->execute()) {
          		print_r($c->stmt->fetchAll(PDO::FETCH_ASSOC));
          	}
          


          Thanks Bruno for taking a look at this for me - cheers...
            • 34012
            • 88 Posts
            You don't need getCollection as you are apparently just querying one object with id => 1. So you are good to go with getObject('TeamDetails', 1);

            Also for the earlier query, you do not have field 'team', which definitely will result in MySQL error.

            Also, you already have all fields selected with the newQuery, so you don't need to select('*').

            Just few pointers

              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
              • 14147
              • 26 Posts
              Thanks for that 'LazyLegs', I'm very new to PDO and still learning. Any tips like that are gratefully received and appreciated.

              Cheers