We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 49407
    • 159 Posts
    I want to retrieve all the rows that have "grower_id=`$grower_id`". The grower ID is an index but not primary. Is there a way to do this?

    I tried using...

    $grows = $modx->getObject($classname, array('grower_id'=>[[!+modx.user.id]]));


    But I am getting an error...

    [2014-12-22 13:53:22] (ERROR @ /index.php) Error 42S22 executing statement:
    Array
    (
    [0] => 42S22
    [1] => 1054
    [2] => Unknown column 'Grows.grower_id' in 'where clause'
    )

    Edit: Correction!

    That was a previous error that I fixed. the current error is...

    (ERROR in chronicleThis Hook @ /index.php) Failed to create object of type: Grows

    I know the classname is correct and here's the code.

    $prefix = 'xyz_';
    $packagename = 'chronicleThis';
    $classname = 'Grows';
    
    $packagepath = $modx->getOption($packagename . '.core_path', NULL, $modx->getOption('core_path') . 'components/' . $packagename . '/');
    $modelpath = $packagepath . 'model/';
    
    $modx->addPackage($packagename, $modelpath, $prefix);
    
    $grows = $modx->getObject($classname, array('owner_id'=>[[!+modx.user.id]]));
    
    if (!is_object($grows) || !($grows instanceof xPDOObject)) {
     //     echo "Not an object or not an instance of xPDOObject!";
        $errorMsg = 'Failed to create object of type: ' . $classname;
        //$hook->addError('error_message', $errorMsg);
        $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'chronicleThis Hook');
        return false;
    }
    
    foreach ( $grows as $grow => $data ) {
        $gStack .= '<div class="responsiveCircle floatLeft" style="background-image:url('.$grow[$data]['leader_url'].');">'.
                       '<div class="growTitleWrapper">'.
                           '<div class="myGrowTitle"><p class="bubbleTitle center middle">'.$grow[$data]['title'].'</p></div>'.
                       '</div>'.
                       '<div class="growLinksWrapper">'.
                           '<div class="myGrowLinks">'.
                               '<div class="circlrTableCell">'.
                                   '<a class="myGrowLink center middle link" onclick="updateGrow('.$grow[$data]['grow_id'].')">Post Update</a>'.
                                   '<br>'.
                                   '<a class="myGrowLink center middle link" onclick="editGrow('.$grow[$data]['grow_id'].')">Edit</a>'.
                               '</div>'.
                           '</div>'.
                       '</div>'.
                   '</div>';
    }
    
    return $gStack;


    Is it not passing the modex.user.id? If not, how should I be passing it? Shouldn't it be in the session somewhere anyways? Can't find any doc on this.

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

    [ed. note: aaronkent last edited this post 9 years, 4 months ago.]
      • 49529
      • 196 Posts
      aaronkent, you can't use modx parser syntax (all "[[]]" stuff) in php. Try to replace
      $grows = $modx->getObject($classname, array('owner_id'=>[[!+modx.user.id]]));

      with
      $grows = $modx->getObject($classname, array('owner_id'=>$modx->user->get('id')));


      [ed. note: BobRay last edited this post 9 years, 4 months ago.]
      • discuss.answer
        • 3749
        • 24,544 Posts
        You can't use a placeholder in snippet code (PHP doesn't know what to do with it). You want this:


        $grows = $modx->getObject($classname, array('owner_id'=>$modx->user->get('id;)));


        If you want more than one row, use getCollection() rather than getObject(). That will get you an array of objects, or an empty array if the query fails.

        You should also check the return value of addPackage(). It returns false on failure.
          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
          • 49529
          • 196 Posts
          Bob Ray, to tell the truth, i learned this trick from your awesome articles!
            • 49407
            • 159 Posts
            I changed the code as suggested...

            $prefix = 'xyz_';
            $packagename = 'chronicleThis';
            $classname = 'Grows';
            
            $packagepath = $modx->getOption($packagename . '.core_path', NULL, $modx->getOption('core_path') . 'components/' . $packagename . '/');
            $modelpath = $packagepath . 'model/';
            
            $modx->addPackage($packagename, $modelpath, $prefix);
            
            $grows = $modx->getCollection( $classname, array( 'grower_id' => 1 ));
            
            if (!is_object($grows) || !($grows instanceof xPDOObject)) {
             //     echo "Not an object or not an instance of xPDOObject!";
                $errorMsg = 'Failed to create object of type: ' . $classname;
                //$hook->addError('error_message', $errorMsg);
                $modx->log(modX::LOG_LEVEL_ERROR, $errorMsg, '', 'chronicleThis Dashboard');
                return false;
            }
            
            print_r($grows);


            Getting the following error 11 times in the modx error logs (for each single page load)...

            [2014-12-23 12:42:10] (ERROR @ /index.php) Error 42S22 executing statement:
            Array
            (
            [0] => 42S22
            [1] => 1054
            [2] => Unknown column 'Grows.owner_id' in 'field list'
            )

            [2014-12-23 12:42:10] (ERROR in chronicleThis Dashboard @ /index.php) Failed to create object of type: Grows

            Firstly, why would it be throwing the error 11 times?

            Secondly, I'm not even trying to access the field "owner_id". I changed it to "grower_id" in the database and in the code and reset the cache.

            I already updated the schema also and changed "owner_id" to "grower_id" in the XML file.

            What is really going on here? Can someone explain to me why it's still trying the wrong column even though the code CLEARLY and explicitly is NOT written to access the column that modx is trying to access?

            None of my calls to snippets are cached and I cleared the cache several times trying to fix this.

            I don't even want to return objects, I want arrays to make iteration easier, is that possible? Can I get back arrays rather than objects? I feel violated.
            • discuss.answer
              • 49529
              • 196 Posts
              aaronkent, as far as i know, you should not only edit your schema, but also parse it as described here: http://rtfm.modx.com/xpdo/2.x/getting-started/creating-a-model-with-xpdo/generating-the-model-code. Or use ClassExtender or MIGX if it's suitable.
              • discuss.answer
                • 49529
                • 196 Posts
                Firstly, why would it be throwing the error 11 times?
                I suggest you have 11 entries in your Grows table?
                • discuss.answer
                  • 49407
                  • 159 Posts
                  I find it faster to edit the files directly rather than write code to rebuild the schema files. It's also less wasteful of space to just edit the files. I found that my error was being caused by the map file. All fixed, though I rather wish that I could access the database without creating the schemas and maps for objects. It's really slowing production down to build a schema for each "class" that essentially equates to a table in the database. I thinks it's quite overkill. Not to mention S L O W. I'm sure some time down the road I will want to slap myself for making this comment but, still, as of late it's a pain in my asterisk.
                  • discuss.answer
                    • 3749
                    • 24,544 Posts
                    My guess is that you updated the schema, but didn't regenerate the class and map files. MODX doesn't actually use the schema at all. The schema is only used to generate the class and map files, which are what MODX uses to understand the relationships.
                      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
                      • 49407
                      • 159 Posts
                      Yes, BobRay, that's correct.

                      IMHO, when you attempt to access an XPDO object, MODX should automatically build the map file, if it doesn't exist, based on the table name. All that should be handled behind the scenes, and not interfere with my job, but it does. So, guess I'll have to put up with it until they change the core functionality.

                      You would think after 4+ years they would have addressed this by now. I can't make ANY logical reason for this being a constant obstacle while developing. Having to build these maps for each table manually, it's really absurd. And you'll say, "you can generate them", but it's still an extra step that is unwarranted. It's like having to program the pump that transfers petrol into your auto's tank when you gas up. It's just unnecessary.