We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40735
    • 119 Posts
    Hi!
    I'm just learning xPDO and I've used MIGX to create my model classes. The database tables I had already created in advance and contain some data. My schema file is below. I can successfully add the package but when I try to getCollection it returns an empty array. What am I doing wrong?
    Thanks in advance!

    $path = MODX_CORE_PATH."components/deanGradList/";
    
    if(!$modx->addPackage("deanGradList",$path."model/","ttu_"))
    	return "<p>Failed to add package.</p>";
    	
    $terms = $modx->getCollection("NameslistSemester");
    
    $output .= "<p>Items: ".count($terms)."</p>";
    var_dump($terms);
    return $output;


    Output:
    array(0) { }

    Items in collection: 0

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="deanGradList" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
    	<object class="Nameslist" table="_nameslist" extends="xPDOSimpleObject">
    		<field key="firstname" dbtype="varchar" precision="250" phptype="string" null="false" />
    		<field key="middlename" dbtype="varchar" precision="250" phptype="string" null="false" />
    		<field key="lastname" dbtype="varchar" precision="250" phptype="string" null="false" />
    		<field key="city" dbtype="varchar" precision="250" phptype="string" null="false" />
    		<field key="county" dbtype="varchar" precision="250" phptype="string" null="false" />
    		<field key="state" dbtype="varchar" precision="250" phptype="string" null="false" />
    		<field key="termcode" dbtype="varchar" precision="250" phptype="string" null="false" />
    		<field key="category" dbtype="varchar" precision="250" phptype="string" null="false" />
    	</object>
    	<object class="NameslistSemester" table="_nameslist_semester" extends="xPDOObject">
    		<field key="termcode" dbtype="varchar" precision="6" phptype="string" null="false" index="pk" />
    		<field key="description" dbtype="varchar" precision="20" phptype="string" null="false" />
    		<field key="published" dbtype="tinyint" precision="1" phptype="integer" null="false" default="0" />
    
    		<index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" >
    			<column key="termcode" length="" collation="A" null="false" />
    		</index>
    	</object>
    </model>


    Also, if it helps, the CREATE TABLEs for both tables:
    CREATE TABLE IF NOT EXISTS `ttu_nameslist_semester` (
      `termcode` varchar(6) NOT NULL,
      `description` varchar(20) NOT NULL,
      `published` tinyint(1) NOT NULL DEFAULT '0',
      PRIMARY KEY (`termcode`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
    
    CREATE TABLE IF NOT EXISTS `ttu_nameslist` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `firstname` varchar(250) NOT NULL,
      `middlename` varchar(250) NOT NULL,
      `lastname` varchar(250) NOT NULL,
      `city` varchar(250) NOT NULL,
      `county` varchar(250) NOT NULL,
      `state` varchar(250) NOT NULL,
      `termcode` varchar(250) NOT NULL,
      `category` varchar(250) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4646 ;
    [ed. note: dwillis210 last edited this post 13 years, 8 months ago.]
      • 3749
      • 24,544 Posts
      I think you want xPDOSimpleObject for the base classes.

      BTW, this is another option if the table already exists. It will create the schema and the class and map files for you: http://bobsguides.com/custom-db-tables.html
        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
        • 40735
        • 119 Posts
        My understanding is that xPDOSimpleObject includes an auto-increment ID field as a primary key and xPDOObject does not. Is this the case?
          • 3749
          • 24,544 Posts
          Yes, I thought that the reason it's not working for you might be that there's no ID field.

            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
            Yes, I thought that the reason it's not working for you might be that there's no ID field.

              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
              • 40735
              • 119 Posts
              Quote from: BobRay at Feb 05, 2013, 04:22 PM
              Yes, I thought that the reason it's not working for you might be that there's no ID field.


              Then I must not understand the way it works. I thought that with xPDOSimpleObject it already has an ID field and so if I created my own in the schema it would then have two of them. Is that not the case or are you talking about the other table that's using xPDOObject? If the latter, does it have to be an ID field? I want to use the termcode field as the primary key for that one and I set it up that way, or at least tried to set it up that way, in my schema.
                • 3749
                • 24,544 Posts
                The pk probably doesn't have to be called 'id', but I've never done that and I'm not sure about the details. I've always just used the 'id' field for the pk. I was just saying that there's an 'id' field in your CREATE TABLES code, but there isn't one in your schema, so they don't match. I've only ever seen that with xPDOSimpleObject, which gives you the id automatically.
                  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
                  • 22303 MODX Staff
                  • 10,725 Posts
                  You seem to have defined the primary key properly, AFAICT from the information provided.

                  I think the problem is you have an underscore in front of each table name, and a prefix that includes an underscore. Are those really the table names, i.e. ttu__{tablename}?
                    • 40735
                    • 119 Posts
                    Quote from: opengeek at Feb 05, 2013, 05:18 PM
                    You seem to have defined the primary key properly, AFAICT from the information provided.

                    I think the problem is you have an underscore in front of each table name, and a prefix that includes an underscore. Are those really the table names, i.e. ttu__{tablename}?

                    Ah, yeah, the table names should only have a single underscore. So I don't need to include the underscore in the prefix parameter of my call to addPackage then? I'll give that a try.
                      • 22303 MODX Staff
                      • 10,725 Posts
                      I would have assumed you don't want the underscores as the first letter in the table names, before the prefix is applied. In MODX for instance, the table name is site_content, not _site_content, and the prefix is modx_, not just modx.