We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36565
    • 73 Posts
    When I try to access data in an xPDO db object in a FormIt preHook, the thing crashes and I get no output. Because of that I keep resorting to non-xPDO database calls in all of my preHooks (which I use to prefill some forms with values).

    Here is the snippet I call first in the preHook to load the db into the $modx object (formerly, a $modx placeholder).
    <?php
    //$db = $modx->getPlaceholder('inventar.db');
    //if (!($db instanceof xPDO)) {
        $host = 'localhost';
        $port = 3306;
        $database = 'inventar';
        $username = 'user';
        $password = 'password';
     
        $dsn = "mysql:host=$host;port=$port;dbname=$database";
        $db = new xPDO($dsn, $username, $password);
        $db->addPackage('Inventardatenbank','/var/www/core/components/Inventardatenbank/model/','');
        //$modx->setPlaceholder('inventar.db', $db);
        $modx->inventar_db = & $db;
    //}
    return '';

    Here is the preHook I'd like to use. The bit that makes the whole thing crash is $aussteller->get('Aussteller') - but only here, in preHooks. Everywhere else the same code works fine.
    <?php
    if(isset($_GET["auid"])) {
        $renr = $_GET["auid"];
        $hook->setValue('auid',$auid);
    } 
    
    $modx->runSnippet('getInventarConnection');
    $db = & $modx->inventar_db;
    if (!empty($db)) {
    $aussteller = $db->getObject('Aussteller',$auid);
        $hook->setValue('auid',$auid);
        $hook->setValue('auname',$aussteller->get('Aussteller'));
    }
    
    return true;

    Thanks alot for your help!
    René [ed. note: neo2.0 last edited this post 14 years, 2 months ago.]
      PHP illiterate MODx enthusiast
      • 4172
      • 5,888 Posts
      where do you set $auid ?

      and you should check, if there is a object, before using it:

      $hook->setValue('auid',$auid);
      if ($aussteller = $db->getObject('Aussteller',$auid)){
          $hook->setValue('auname',$aussteller->get('Aussteller'));
      }
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 36565
        • 73 Posts
        Dear Bruno, I owe you one. The whole problem consisted in myself constantly overlooking $renr where $auid should have been.

        You made my day, I now feel a *lot* better.
        Thanks for prodding me on it!

        René
          PHP illiterate MODx enthusiast