On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hello everyone.

    I have used CMP Generator to connect my custom datatable with modx.
    So now i have a schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <model package="TinyStory" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
    	<object class="Story" table="_story" extends="xPDOSimpleObject">
    		<field key="author" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" />
    		<field key="name" dbtype="varchar" precision="126" phptype="string" null="false" />
    		<field key="story" dbtype="text" phptype="string" null="false" />
    		<field key="lang" dbtype="varchar" precision="3" phptype="string" null="false" />
    		<field key="fav" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" />
    		<field key="date" dbtype="date" phptype="date" null="false" />
    		<field key="next" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" />
    		<field key="prev" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" />
    	</object>
    </model>


    And i have a class:
    <?php
    class Story extends xPDOSimpleObject {}


    All neccesery files are here: core/components/TinyStory/model/TinyStory/

    So i have created a simple snippet to test if the connection is work (there is couple of rows in my databse with id =1 and 2):
    <?php
    $package_path = $modx->getOption('core_path').'components/TinyStory/model/';
    $modx->addPackage('TinyStory', $package_path,'modx_');
    
    $query = $modx->newQuery('Story');
    $row = $modx->getObject('Story',1);
    
    if($row==NULL)
    {
    $output="row is NULL";
    }
    else
    {
    $output="We have something! Wow!";
    }
    
    return $output;


    But it's looks like "getObject('Story',1);" returns NULL.

    What i am doing wrong?

    Best regards, Paul

    This question has been answered by multiple community members. See the first response.

    • discuss.answer
      if your table is 'modx_story'

      it should be table="story"

      you don't need 'modx_' in your addPackage, since this is the default table-prefix.

      and if you define null="false", you should also define a default-value
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
      • discuss.answer
        and I would name the package all lowercase:

        package="tinystory"


        $package_path = $modx->getOption('core_path').'components/tinystory/model/';
          -------------------------------

          you can buy me a beer, if you like MIGX

          http://webcmsolutions.de/migx.html

          Thanks!
        • Quote from: Bruno17 at Sep 19, 2015, 02:39 PM
          if your table is 'modx_story'

          it should be table="story"

          you don't need 'modx_' in your addPackage, since this is the default table-prefix.

          and if you define null="false", you should also define a default-value

          It works!

          Thank you for your help, Bruno17!