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

    i’m trying to convert an existing snippet to revolution and experiencing with xPDO as well.

    Here the query that i’m trying to convert:

    $query = "SELECT stv.name,stc.tmplvarid,stc.contentid,stv.type,stv.display,stv.display_params,stc.value";
    $query .= " FROM ".$tb1." stc LEFT JOIN ".$tb2." stv ON stv.id=stc.tmplvarid ";
    $query .= " LEFT JOIN ".$tb_content." tb_content ON stc.contentid=tb_content.id ";
    $query .= " WHERE stv.name='".$tvTags."' AND stc.contentid IN (".implode($cIDs,",").") ";
    $query .= " AND tb_content.pub_date >= '".$pub_date."' ";
    $query .= " AND tb_content.published = 1 ";
    $query .= " ORDER BY stc.contentid ASC;";
    


    I’m trying by doig this:

    $t = $this->modx->newQuery('modTemplateVar');
    $t->leftJoin('modTemplateVarResource','tvValues');
    $t->leftJoin('modResource','content');
    $t->where(array(
    	'modTemplateVar.name' => $tvTags,
    	'tvValues.contentid:IN' => '('.implode($cIDs,",").')',
    	'content.pub_date:>=' => $pub_date,
    	'content.published' => '1',
    ));
    $t->sortby('tvValues.contentid','ASC');
    $tags = $this->modx->getCollection('modTemplateVar',$t);
    


    I believe that the problem is with the IN clause.
    Is there another way to use it that i don’t know of, or shoul i use xPDOCriteria?
    • The IN expression is tricky because of quoting in PDO prepared statements. The easiest way to work around this for now is to make it a literal expression rather than a prepared statement expression:
      "tvValues.contentid IN (" . implode($cIDs,",") . ")"

      I’ll work on a solution to make IN work as expected, but it may take some time and thought...
      • Quote from: OpenGeek at Aug 24, 2009, 03:30 PM

        The IN expression is tricky because of quoting in PDO prepared statements. The easiest way to work around this for now is to make it a literal expression rather than a prepared statement expression:
        "tvValues.contentid IN (" . implode($cIDs,",") . ")"

        I’ll work on a solution to make IN work as expected, but it may take some time and thought...

        I just tested, it does not work with the code i provided.
        I have to rewrite it in another way?

        <?php
        $t = $this->modx->newQuery('modTemplateVar');
        $t->leftJoin('modTemplateVarResource','tvValues');
        $t->leftJoin('modResource','content');
        $t->where(array(
        	'modTemplateVar.name' => $tvTags,
        	"tvValues.contentid IN (" . implode($cIDs,",") . ")",
        	'content.pub_date:>=' => $pub_date,
        	'content.published' => '1',
        ));
        $t->sortby('tvValues.contentid','ASC');
        $tags = $this->modx->getCollection('modTemplateVar',$t);
        ?>
        


        I also wonder if there is a simple way to debug the query?
        Because actually it just give me an empty array.
        • Use this code to debug the SQL generated:
          $query->prepare();
          $sql = $query->toSQL();

          That will give you the actual SQL that would be executed by any xPDOCriteria/xPDOQuery object. Do that before you call getCollection()...
          • Thanks Jason, it’s really helpful.

            However, i’ve another issue.

            I’m trying to use

            <?php
            $t = $this->modx->newQuery('modTemplateVar');
            $t->select('modTemplateVar.name AS name, tvValues.tmplvarid AS varid'); 
            $t->leftJoin('modTemplateVarResource','tvValues','modTemplateVar.id = tvValues.tmplvarid');
            $t->leftJoin('modResource','content','tvValues.contentid = content.id');
            $t->where(array(
            	'modTemplateVar.name' => $tvTags,
            	"tvValues.contentid IN (" . implode($cIDs,",") . ")",
            	'content.pub_date:>=' => $pub_date,
            	'content.published' => '1',
            ));
            $t->sortby('tvValues.contentid','ASC');
            $t->prepare();
            $sql = $t->toSQL();
            ?>
            


            And the process die (Firefox message : Connection has been reinitialized).

            I know that the issue is with the $query->select, since the query work if i comment the line.

            Any idea what the problem is?

            Ok, i sorted it out by using $query->setClassAlias(’tv’); before setting the $query->select line.

            I should have been able to do it without that workaround isn’t it?
            • Try this instead:
              $t->select(array('name' => 'modTemplateVar.name', 'varid' => 'tvValues.tmplvarid'));

              • I’ve tried it (and also by inversing key & value), but the script reinitialize the database connection each time i try to put a select statement with the command.

                I don’t really get it, because when i use the debug line and past the code in my editor, it works fine. When comment the two line and put back getCollection, the script fail.

                During my test, i’ve take a look at Ditto to try to understand how it get the TV’s and replace the function getTVDisplayFormat of the Evo.
                I’ve tried the funtion (line 826) and it works well with my script.

                But i would really like to understand why the query failed...
                • Re-initialization of the database connection should not happen because of the SELECT part of the query; that should simply create a SQLER. I’m stumped on that TBH. That points to other problems, possibly with the MySQL client API or drivers compiled with your PHP. I’ll do some more research, but nothing obvious is turning up.
                  • Ok, if you need infos of my config, let me know!
                    • I’ve continued to play with xpdo and the xpdoquery.

                      In fact, at least on my computers, it woks only if i put the select statement just the line before getCollection.

                      It it can help...