We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 2611
    • 394 Posts
    When i execute this code...it doesn’t work. It inserts tmplvarid 0 and templateid 0
    or it inserts nothing (because 0, 0, 0 already exists) and gives me the return value "false"

    update: when i add rank => 5 to the query it DOES save rank 5 in the database but still
    tmplvarid 0 and templateid 0...how is this possible? We are running RC3 (7083)


    update2: Why does the table (site_tmplvar_templates) have 2 primary keys?

    $templateLink = $this->modx->newObject('modTemplateVarTemplate');
    $templateLink->fromArray(array(
    	'tmplvarid' => 1,
    	'templateid' => 4,
    	'rank' => 5
    ));
    
    $templateLink->save();


    I cannot wrap my head around this...this is the most plain query one can
    execute right?

    edit:
    For the next person viewing this...this SHOULD work:


    $templateLink = $this->modx->newObject('modTemplateVarTemplate');
    $templateLink->fromArray(array(
    	'tmplvarid' => 1,
    	'templateid' => 4,
    	'rank' => 5
    ), '', true); // by true you say it's ok to write/overwrite primary keys
    
    $templateLink->save();
      Follow me on twitter: @b03tz
      Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
      CodeMaster
      • 22303 MODX Staff
      • 10,725 Posts
      See the parameters of the fromArray() method. It will not set primary key values unless specifically told to do so.
        • 2611
        • 394 Posts
        Thanks alot...this clears it up. When i used the $obj->set(key, value) method it worked.
        This was so strange (in my experience) but now i get it. I guess it’s pretty neet that
        it doesn’t just overwrite primary keys wink

        Thanks for your help again!
          Follow me on twitter: @b03tz
          Follow SCHERP Ontwikkeling on twitter: @scherpontwikkel
          CodeMaster