<?php $host=$modx->dbConfig['host']; $dbname=str_replace("`", "", $modx->dbConfig['dbase']); $dbusername=$modx->dbConfig['user']; $dbpassword=$modx->dbConfig['pass']; $dbprefix=$modx->dbConfig['table_prefix']; define('XPDO_MODE', 2); include_once ( $modx->config['base_path'] . 'core/xpdo/xpdo.class.php'); $xpdo= new xPDO('mysql:host='.$host.';dbname='.$dbname, $dbusername, $dbpassword, $dbprefix); $xpdo->setPackage('modx095',XPDO_CORE_PATH .'../model/'); $xpdo->setDebug(false); $xpdo->loadClass('modElement'); // // get the docid of your choice, here its the actuell doc // $id=$modx->documentIdentifier; $criteria= $xpdo->newQuery('modResource', $id); $criteria->limit(1); $docm= $xpdo->getObject('modResource', $criteria); if ($docm) { $output.= "DOC OK <br/>"; $content= $docm->get('content'); $pagetitle= $docm->get('pagetitle'); $longtitle= $docm->get('longtitle'); $output.= '<br/>Content : '.$content; $output.= '<br/>PageTitle : '.$pagetitle.'<br/>'; $tvname= $xpdo->getObject('modTemplateVar', array ('name' => 'test')); $tv_criteria=array ('tmplvarid' =>$tvname->get('id'), 'contentid'=> $docm->get('id')); //print_r($param); if ($tvname) { $output.= "TVname OK <br/>"; //$docm->getMany('modTemplateVarDocument'); $tvs= $xpdo->getObject('modTemplateVarDocument', $tv_criteria); //print_r($docm); if ($tvs) { $value=$tvs->get('value'); $value++; $tvs->set('value', $value); $tvs->save(); $output.= 'TV edited to '.$value.' <br/>'; } else { $tvs=$xpdo->newObject('modTemplateVarDocument'); if ($tvs) { $tvs->set('value', '0'); $tvs->fromArray($tv_criteria); $tvs->save(); $output.= "new TV created <br/>"; } else {$output.= "TV could not be created <br/>";} } } else {$output.= "TVname NOT OK <br/>";} } else {$output.= "DOC NOT found";} ?>
First of all it looks like you are using the new classes with 0.9.6, which will not work, as there is a lot of code in the new Revolution code that would not apply in the legacy offering. Or are you just creating a model based on the same class structure? modElement is very specifically one of those classes making me think you are headed for trouble.
Created my first experiment with xPDO and trying to understand.
It counts each call to a Document, where the snippet is placed, and saves the count in a TV.
<?php $host=$modx->dbConfig['host']; $dbname=str_replace("`", "", $modx->dbConfig['dbase']); $dbusername=$modx->dbConfig['user']; $dbpassword=$modx->dbConfig['pass']; $dbprefix=$modx->dbConfig['table_prefix']; define('XPDO_MODE', 2); include_once ( $modx->config['base_path'] . 'core/xpdo/xpdo.class.php'); $xpdo= new xPDO('mysql:host='.$host.';dbname='.$dbname, $dbusername, $dbpassword, $dbprefix); $xpdo->setPackage('modx095',XPDO_CORE_PATH .'../model/'); $xpdo->setDebug(false); $xpdo->loadClass('modElement'); // // get the docid of your choice, here its the actuell doc // $id=$modx->documentIdentifier; $criteria= $xpdo->newQuery('modResource', $id); $criteria->limit(1); $docm= $xpdo->getObject('modResource', $criteria); if ($docm) { $output.= "DOC OK <br/>"; $content= $docm->get('content'); $pagetitle= $docm->get('pagetitle'); $longtitle= $docm->get('longtitle'); $output.= '<br/>Content : '.$content; $output.= '<br/>PageTitle : '.$pagetitle.'<br/>'; $tvname= $xpdo->getObject('modTemplateVar', array ('name' => 'test')); $tv_criteria=array ('tmplvarid' =>$tvname->get('id'), 'contentid'=> $docm->get('id')); //print_r($param); if ($tvname) { $output.= "TVname OK <br/>"; //$docm->getMany('modTemplateVarDocument'); $tvs= $xpdo->getObject('modTemplateVarDocument', $tv_criteria); //print_r($docm); if ($tvs) { $value=$tvs->get('value'); $value++; $tvs->set('value', $value); $tvs->save(); $output.= 'TV edited to '.$value.' <br/>'; } else { $tvs=$xpdo->newObject('modTemplateVarDocument'); if ($tvs) { $tvs->set('value', '0'); $tvs->fromArray($tv_criteria); $tvs->save(); $output.= "new TV created <br/>"; } else {$output.= "TV could not be created <br/>";} } } else {$output.= "TVname NOT OK <br/>";} } else {$output.= "DOC NOT found";} ?>
Ok, works fine. But now some questions.
- Is there a way in xPDO to get all or just some TVs together with the Document with getObjectGraph or so as one object, using the relations from the schema?
- make some changes to the TVs in that document (create new if not saved before) with $docm->set.......
- make changes to the document
- then save the document-object together with all the changes on document and TVs with $docm->save?
Is it possible? And when yes, how can I do that magic thing?
Bruno
First of all it looks like you are using the new classes with 0.9.6, which will not work, as there is a lot of code in the new Revolution code that would not apply in the legacy offering. Or are you just creating a model based on the same class structure? modElement is very specifically one of those classes making me think you are headed for trouble.
$xpdo->loadClass('modElement');
Also, template variables are probably the most complex part of the Revolution code, because, well, it’s simply a complex situation, but, you can indeed get child objects populated with getObjectGraph() or getCollectionGraph(). You could make changes to the document or TVs and simply add a new object using xPDOObject->addOne() or ->addMany() or update an existing ones you already pulled in with your graph query, then just save()’ing the parent will take care of child changes (a.k.a. cascading save()’s).
<?php $host = $modx->dbConfig['host']; $dbname = str_replace("`", "", $modx->dbConfig['dbase']); $dbusername = $modx->dbConfig['user']; $dbpassword = $modx->dbConfig['pass']; $dbprefix = $modx->dbConfig['table_prefix']; define('XPDO_MODE', 2); include_once ($modx->config['base_path'].'core/xpdo/xpdo.class.php'); $xpdo = new xPDO('mysql:host='.$host.';dbname='.$dbname, $dbusername, $dbpassword, $dbprefix); $xpdo->setPackage('modx095', XPDO_CORE_PATH.'../model/'); $xpdo->setDebug(false); // get the docid of your choice, here its the actuell doc $id = $modx->documentIdentifier; $tvname = 'test2'; //get the tmplvarid of the TV with given name $tvobj = $xpdo->getObject('modTemplateVar', array ('name'=>$tvname)); if ($tvobj) { $tmplvarid = $tvobj->get('id'); //get the document $docm = $xpdo->getObject('modResource', $id); //get the TVs related to the document $tvs2 = $docm->getMany('modTemplateVarResource'); //make an array of all TVs related to the document for access with given tmplvarid //key -> tmplvarid, value -> id $tvarr = array (); foreach ($tvs2 as $key=>$tv) { $tvarr[$tv->get('tmplvarid')] = $tv->get('id'); } //check if tv exists, if not create a new one if ( isset ($tvarr[$tmplvarid])) { //get the id for given TV from the array $tvid = $tvarr[$tmplvarid]; $value = $tvs2[$tvid]->get('value'); $value++; $tvs2[$tvid]->set('value', $value); $output .= 'TV edited to '.$value.' <br/>'; } else { echo 'TV not found - must create new one'; $newtv = $xpdo->newObject('modTemplateVarResource'); $newtv->set('value', '1'); $newtv->set('tmplvarid', $tmplvarid); $docm->addmany($newtv); } //echo 'class:'.get_parent_class($tvs2[$tvid]).'<br/>'; //echo $tvs2[$tvid]->get('tvarid').':'.$tvs2[$tvid]->get('value').'<br/>'; } else { $output .= "TVname NOT OK <br/>"; } //save the doc and related TVs $docm->save(); ?>