We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35781
    • 25 Posts
    Hello. I am trying to use modx tables through xpdo with my snippet
    $id=empty($id)?1:$id;
    
    $c = $modx->newQuery('modTemplateVarResource');
    $c->setClassAlias("v");
    $c->leftJoin('modTemplateVar','t','v.tmplvarid=t.id');
    $c->select("t.name,v.value");
    $c->where(array("v.contentid"=>"$id"));
    $modx->setDebug(true);
    $t = $modx->getCollection('modTemplateVarResource',$c);
    $modx->setDebug(false);
    $placeholders['ttt']='';
    
    //dummy output
    foreach($t as $f)
    {
      $placeholders['ttt'].=$f->pagetitle;
    }
    $out = $modx->getChunk('123',$placeholders);
    return $out;


    And get a strange error. On virtual server I have "Segmentation Fault(11)", on my local computer I have "PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 261900 bytes) in /home/.../core/xpdo/om/xpdoobject.class.php on line 1530". As I can see both error are connected with xpdo or mysql client, but I cant figure out exactly. If I comment getCollection or change it to query without join I won’t get any errors.

    My database contains 3 resources.

    VPS configuration:
    mysql: 5.1.58-1~dotdeb.0
    PHP: 5.2.17-0.dotdeb.0
    Mysql client api: 5.0.51a
    Error message: "child pid 26493 exit signal Segmentation fault (11)"


    local configuration
    mysql 5.1.54-1ubuntu4
    PHP: 5.3.5-1ubuntu7.2
    MYSQL CLIENT API: 5.1.54
    PDO: 5.1.54
    Mem limit: 512 MB
    Error message: "PHP Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 261900 bytes) in /home/.../core/xpdo/om/xpdoobject.class.php on line 1530".

    Can anybody help me?
      • 22303 MODX Staff
      • 10,725 Posts
      You must select the primary key (and possible other columns) fields of all tables you want to retrieve objects for. You are trying to get a result set of data from two different objects; this is not what getObject/getCollection are for. Also, there is no pagetitle variable on modTemplateVarResource.
        • 35781
        • 25 Posts
        Thankyou for help. That’s it. I add PK to my select and it has solved the problem.
        So my correct snippet is
        $id=empty($id)?1:$id;
        
        $c = $modx->newQuery('modTemplateVarResource');
        $c->setClassAlias("v");
        $c->innerJoin('modTemplateVar','t','v.tmplvarid=t.id');
        //Added v.id
        $c->select("v.id,t.name,v.value");
        $c->groupby('v.tmplvarid');
        $c->where(array("v.contentid"=>"$id"));
        $t = $modx->getCollection('modTemplateVarResource',$c);
        
        foreach($t as $f)
        {
          $placeholders['ttt'].=$f->value;
        }
        $out = $modx->getChunk('123',$placeholders);
        return $out;
        


        I didn’t think that absence of Primary Key in select may lead to apache crash. It’s new for me.
          • 22303 MODX Staff
          • 10,725 Posts
          FWIW, it could cause the crash since the query has no way to identify unique rows. It likely is causing some kind of infinite loop trying to populate the objects from the invalid result set.