We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21841
    • 36 Posts
    Hi,

    I am trying to retrieve a record using a non primary key. The statement that I am currently using generates an error - but no error message - it just stops.

    I am trying to return the Template Variable record for a TV called ’tags’ : T huhhe code is :
    $ctvcrit= $xpdo->newQuery('modTemplateVar', array('name:=' => 'tags'));


    I have looked at it for hours (well almost) - have I got the wrong end of the stick (again) ?

    Any help much appreciated.

    NWJ
      • 22303 MODX Staff
      • 10,725 Posts
      @nowhinjing:

      The newQuery method simply creates an xPDOQuery object to use in another method that accepts criteria. To execute that query and get an object returned just add this after your code:
      $obj= $xpdo->getObject('modTemplateVar', $ctvcrit);


      But you don’t need an xPDOQuery object just to retrieve an object by a simple condition:
      $obj= $xpdo->getObject('modTemplateVar', array ('name' => 'tags'));

      ...is all it takes.
        • 21841
        • 36 Posts
        Thanks, Jason,

        I will try it out and let you know.

        NWJ
          • 21841
          • 36 Posts
          Sorry, still no luck sad

          Here is the snippet I am calling uncached; it returns "Starting : " then stops. If I comment out the getObject line, it returns "Starting :" - "No Record Found" and "Ending :"

          I am using xPDO 34 that I retrieved via SVN.

          <?php
          global $modx;
          define('XPDO_MODE', 2);
          include_once ( $modx->config['base_path'] . 'xpdo/xpdo.class.php');
          
          //  setup and connect to the database
          $xpdo= new xPDO('mysql:host=localhost;dbname=mdx', 'mdxAdmin', 'mdxAdminLouches', 'modx_');
          $xpdo->setPackage('modx095');
          $xpdo->setDebug(true);
          
          echo "<br>Starting :";
          //  get the record to be worked on
          $ctvdocm= $xpdo->getObject('modTemplateVar', array ('name' => 'tags'));
          if ($ctvdocm) {
              echo "</br>Record Found";
          } else {
              echo "</br>No Record Found";
          }
          
          echo "</br>Ending :";
          return "";
          ?>


          Thanks in advance for your help.

          By the way, could I help you document xPDO, perhaps by providing worked examples as in Noddy’s Guide ?

          NWJ
            • 22303 MODX Staff
            • 10,725 Posts
            Try
            return print_r($xpdo->errorInfo(), 1); 
            immediately after the getObject call. I have a feeling the legacy MODx error handling or output buffering is preventing proper reporting of the xPDO error logging.

            Regarding documentation, I will be enabling user-contributions once I stabilize the changes coming in 1.0 beta, but by all means, feel free to assist with documentation in any way you feel you can. I’m working on having tutorials and extended documentation be authored into DocBook format and linked directly into the API documentation. I can then use PhpDocumentor to produce CHM, HTML, PDF, and many other formats for distribution along with the API docs. More details on this soon...
              • 21841
              • 36 Posts
              Hi, Jason,

              Thanks for your quick response.

              Unfortunately I am still getting nothing - "Starting :" and then blank.

              Commenting out the getObject line and running again gives "Starting :Array ( [0] => )" which I suppose is logical.

              Should I update to a newer SVN ?

              NWJ
                • 22303 MODX Staff
                • 10,725 Posts
                Can you put your code in a php file and try it directly, rather than via MODx snippet? You need to figure out why it’s not returning a record. With debug on, you should be getting all kinds of output...
                  • 21841
                  • 36 Posts
                  Hi, Jason,

                  Have done as you suggested, but no luck so far.

                  Results are exactly as described above.

                  doncha luv computers !

                  NWJ.
                    • 21841
                    • 36 Posts
                    Hi, Jason,

                    I think I’m getting somewhere. I tried changing the table being queried and all of a sudden it all jumped into life. I guess this points to an error in the schema / class map, so I am attaching them for your perusal as I must confess its all greek to me. When I understand how it works I would be happy to write a noddys guide; the fields I understand, but the afregates and composits huh

                    Schema
                        <object class="modTemplateVar" table="site_tmplvars" extends="modElement">
                            <field key="type" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
                            <field key="name" dbtype="varchar" precision="50" phptype="string" null="false" default="" />
                            <field key="caption" dbtype="varchar" precision="80" phptype="string" null="false" default="" />
                            <field key="description" dbtype="varchar" precision="255" phptype="string" null="false" default="" />
                            <field key="editor_type" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
                            <field key="category" dbtype="int" precision="11" phptype="integer" null="false" default="0" />
                            <field key="locked" dbtype="tinyint" precision="4" phptype="integer" null="false" default="0" />
                            <field key="elements" dbtype="text" phptype="string" null="false" default="" />
                            <field key="rank" dbtype="int" precision="11" phptype="integer" null="false" default="0" index="index" />
                            <field key="display" dbtype="varchar" precision="20" phptype="string" null="false" default="" />
                            <field key="display_params" dbtype="text" phptype="string" null="false" default="" />
                            <field key="default_text" dbtype="text" phptype="string" null="false" default="" />
                            <aggregate class="modCategory" key="id" local="category" foreign="id" cardinality="one" />
                            <composite class="modTemplateVarTemplate" key="id" local="tmplvarid" foreign="id" cardinality="many" />
                            <composite class="modTemplateVarDocument" key="id" local="tmplvarid" foreign="id" cardinality="one" />
                            <composite class="modTemplateVarDocumentGroup" key="id" local="tmplvarid" foreign="id" cardinality="many" />
                        </object>
                    


                    Map
                    $xpdo_meta_map['modTemplateVar']= array (
                      'table' => 'site_tmplvars',
                      'fields' => 
                      array (
                        'type' => '',
                        'name' => '',
                        'caption' => '',
                        'description' => '',
                        'editor_type' => '0',
                        'category' => '0',
                        'locked' => '0',
                        'elements' => '',
                        'rank' => '0',
                        'display' => '',
                        'display_params' => '',
                        'default_text' => '',
                      ),
                      'fieldMeta' => 
                      array (
                        'type' => 
                        array (
                          'dbtype' => 'varchar',
                          'precision' => '20',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                        'name' => 
                        array (
                          'dbtype' => 'varchar',
                          'precision' => '50',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                        'caption' => 
                        array (
                          'dbtype' => 'varchar',
                          'precision' => '80',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                        'description' => 
                        array (
                          'dbtype' => 'varchar',
                          'precision' => '255',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                        'editor_type' => 
                        array (
                          'dbtype' => 'int',
                          'precision' => '11',
                          'phptype' => 'integer',
                          'null' => 'false',
                        ),
                        'category' => 
                        array (
                          'dbtype' => 'int',
                          'precision' => '11',
                          'phptype' => 'integer',
                          'null' => 'false',
                        ),
                        'locked' => 
                        array (
                          'dbtype' => 'tinyint',
                          'precision' => '4',
                          'phptype' => 'integer',
                          'null' => 'false',
                        ),
                        'elements' => 
                        array (
                          'dbtype' => 'text',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                        'rank' => 
                        array (
                          'dbtype' => 'int',
                          'precision' => '11',
                          'phptype' => 'integer',
                          'null' => 'false',
                          'index' => 'index',
                        ),
                        'display' => 
                        array (
                          'dbtype' => 'varchar',
                          'precision' => '20',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                        'display_params' => 
                        array (
                          'dbtype' => 'text',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                        'default_text' => 
                        array (
                          'dbtype' => 'text',
                          'phptype' => 'string',
                          'null' => 'false',
                        ),
                      ),
                      'aggregates' => 
                      array (
                        'modCategory' => 
                        array (
                          'id' => 
                          array (
                            'local' => 'category',
                            'foreign' => 'id',
                            'cardinality' => 'one',
                          ),
                        ),
                      ),
                      'composites' => 
                      array (
                        'modTemplateVarTemplate' => 
                        array (
                          'id' => 
                          array (
                            'local' => 'tmplvarid',
                            'foreign' => 'id',
                            'cardinality' => 'many',
                          ),
                        ),
                        'modTemplateVarDocument' => 
                        array (
                          'id' => 
                          array (
                            'local' => 'tmplvarid',
                            'foreign' => 'id',
                            'cardinality' => 'one',
                          ),
                        ),
                        'modTemplateVarDocumentGroup' => 
                        array (
                          'id' => 
                          array (
                            'local' => 'tmplvarid',
                            'foreign' => 'id',
                            'cardinality' => 'many',
                          ),
                        ),
                      ),
                    );
                    $xpdo_meta_map['modtemplatevar']= & $xpdo_meta_map['modTemplateVar'];
                    


                    Thank you for taking the time to help - I really do appreciate your efforts and xPDO is just wonderful !

                    NWJ
                      • 21841
                      • 36 Posts
                      Bump !

                      Can anyone help - pretty please kiss

                      NWJ