Developing with xPDO - MODX Community Forums https://forums.modx.com/board/?board=46 <![CDATA[pdoFetch examples anyone?]]> https://forums.modx.com/thread/104571/pdofetch-examples-anyone#dis-post-562831 https://forums.modx.com/thread/104571/pdofetch-examples-anyone#dis-post-562831 Mon, 19 Nov 2018 08:37:26 +0000 https://forums.modx.com/thread/104571/pdofetch-examples-anyone#dis-post-562831 <![CDATA[create new tv and customize view for article]]> https://forums.modx.com/thread/104502/create-new-tv-and-customize-view-for-article#dis-post-562306
I did a couple research online, but not much luck, so I wonder some expert can help me out here.
Modx version: 2.65
I tried to create a new TV to show up articles grid system. so they don't need to click every single article and template variable to do the edit.
I saw collections plugin can do customize the view like here:https://modx.com/blog/2014/09/30/collections-easily-customizable-admin-views-for-content-types/
So I wonder can I do the same for articles? because the site already has a decent amount of articles already, not planning to move to collections.

Like this:https://imgur.com/a/9ssKJXO

Thank you so much for the help!
]]>
https://forums.modx.com/thread/104502/create-new-tv-and-customize-view-for-article#dis-post-562306 Wed, 24 Oct 2018 03:57:26 +0000 https://forums.modx.com/thread/104502/create-new-tv-and-customize-view-for-article#dis-post-562306
<![CDATA[Query/Join Multiple Tables with xPDO]]> https://forums.modx.com/thread/99087/query-join-multiple-tables-with-xpdo?page=3#dis-post-560678
I have 3 separate tables (yes, the need to be separate tables) that all have different types of data for the same list of products.

I need to be able to create a page for a product and display all of these cells of data using simple placeholders.

My Package is all set up and ready to go and I know how to do this with a single table using getCollection. But I can't seem to figure out how I would join these so that ALL of the attributes will be available to me for use in TPLs.

Has anyone done something like this.]]>
https://forums.modx.com/thread/99087/query-join-multiple-tables-with-xpdo?page=3#dis-post-560678 Sat, 11 Aug 2018 06:52:37 +0000 https://forums.modx.com/thread/99087/query-join-multiple-tables-with-xpdo?page=3#dis-post-560678
<![CDATA[Mysterious problem with $c->where() result in processor.]]> https://forums.modx.com/thread/104123/mysterious-problem-with-c--where-result-in-processor#dis-post-559990 I've got an interesting problem with where() method smiley
There are one processor exdends the modObjectGetListProcessor.
On it's input is an array.
In prepareQueryBeforeCount() function I do where() like this:
foreach ($query as $k => $v){
    $this->where[$k.':LIKE'] = "%$v%";
}
$c->where($this->where);

Then I use $c->toSql() for checking query result.
Selects and joins are not so interested for us - there is ok, but results of where() method work - that is interesting and slightly mysterious for me smiley

Lets start.
Input array is like this (print from log):
[$query] => Array
        (
            [id] => 100
            [city] => 200
            [name] => 300
            [phone] => 400
            [mail] => 500
            [performer] => 600
        )

In this array can be digits or strings - whatever.

Variable $this->where looks like:
[$this->where] => Array
              (
                  [id:LIKE] => %100%
                  [city:LIKE] => %200%
                  [name:LIKE] => %300%
                  [phone:LIKE] => %400%
                  [mail:LIKE] => %500%
                  [performer:LIKE] => %600%
              )

And after where() method works we've got (part of print from log):
WHERE  ( `a`.`id` LIKE 0 AND `a`.`city` LIKE 0 AND `a`.`name` LIKE '%300%' AND `a`.`phone` LIKE 0 AND `a`.`mail` LIKE 0 AND `a`.`performer` LIKE 0 )

Only one of values is not 0. Always. Even if input array contains only one of this elements.
By experimental way I clear that zeroing happens when where() get concatenated values like "%$v%" OR "%".$v."%". But why for 'name' is not the same bechaviour?

Why Lord?
Oh... Sorry... smiley
Why can it be, guys?
Any thoughts?

You can ask any additional questions.

MODX 2.6.5-pl
PHP 7.2.1
]]>
https://forums.modx.com/thread/104123/mysterious-problem-with-c--where-result-in-processor#dis-post-559990 Wed, 25 Jul 2018 07:10:16 +0000 https://forums.modx.com/thread/104123/mysterious-problem-with-c--where-result-in-processor#dis-post-559990
<![CDATA[Get PHP Variable from ID]]> https://forums.modx.com/thread/103352/get-php-variable-from-id?page=2#dis-post-559708
it's a simple question, but i cant find the right solution:

how i get the PHP-Variable from one resource in the other?

I try to get the value "X-Type" form resource "12" to resource "25"

MODx Resource ID "12":
[[!mySnippet? &type=`X-Type`]]


MODx Resource ID "25":
[[mySnippet]]


mySnippet:
<?php
return $type;


]]>
https://forums.modx.com/thread/103352/get-php-variable-from-id?page=2#dis-post-559708 Thu, 19 Jul 2018 11:05:57 +0000 https://forums.modx.com/thread/103352/get-php-variable-from-id?page=2#dis-post-559708
<![CDATA[Modparser error "You should not call uncached elements inside cached!"]]> https://forums.modx.com/thread/103351/modparser-error-you-should-not-call-uncached-elements-inside-cached#dis-post-559209
Any ideas?

[2018-01-04 18:11:05] (ERROR @ /var/www/vhosts/ultimusfundsolutions.com/httpdocs/core/model/modx/modparser.class.php : 453) You should not call uncached elements inside cached!
Outer tag: [[pdoResources? &parents=`0` &tpl=`ufsRSSItemE` &class=`ufsEvents` &loadModels=`ufsevents` &where=`{"published:=":"1","eenddate:<=":"[[!now]]"}` &fastMode=`0` &showLog=`0` &sortby=`eenddate` &sortdir=`ASC`]]
Inner tag pdoResources? &parents=`0` &tpl=`ufsRSSItemE` &class=`ufsEvents` &loadModels=`ufsevents` &where=`{"published:=":"1","eenddate:<=":"[[!now]]"}` &fastMode=`0` &showLog=`0` &sortby=`eenddate` &sortdir=`ASC`

Server and install details:

OS: CentOS Linux 7.4.1708 (Core)
Apache web server
PHP 7.0.24
Database server:
Server: Localhost via UNIX socket
Server version: 5.5.56-MariaDB - MariaDB Server
nginx/1.11.10

Modx 2.6.1-pl
packages installed:
ActivationEmail
Breadcrumbs
ClassExtender
Collections
FileLister
FirstChildRedirect
FormIt
Gallery
getReqParam
Login
MIGX
pdoTools
Personalize
Rampart
ReCaptchaV2
Redactor
SimpleSearch
StageCoach]]>
https://forums.modx.com/thread/103351/modparser-error-you-should-not-call-uncached-elements-inside-cached#dis-post-559209 Sun, 24 Jun 2018 10:47:05 +0000 https://forums.modx.com/thread/103351/modparser-error-you-should-not-call-uncached-elements-inside-cached#dis-post-559209
<![CDATA[xPDO database connection script]]> https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-559147
This works fine as code inline:

$dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
$xpdo = new xPDO($dsn,'username','password');

echo $o=($xpdo->connect()) ? 'Connected<br><br>' : 'Not Connected<br><br>';

$results = $xpdo->query("SELECT * FROM `table` LIMIT 20");
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
  $output .= $modx->getChunk('test', $row);
  }

return $output;


But when I put the connection script into a snippet 'pdoConnect'

define('MODX_CORE_PATH', '/core/');
define('MODX_CONFIG_KEY','config');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';

$dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
$xpdo = new xPDO($dsn,'username','password');


and call it using

$modx->runSnippet('pdoConnect');


it throws an error


Fatal error: Call to a member function connect() on a non-object in /home2/pghmin/public_html/core/cache/includes/elements/modsnippet/76.include.cache.php on line 22

Modx 2.2.14 and 2.5.0 same problem.

Seems trivial but I can't find a solution]]>
https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-559147 Wed, 20 Jun 2018 03:47:08 +0000 https://forums.modx.com/thread/101157/xpdo-database-connection-script#dis-post-559147
<![CDATA[Get parent page titles in foreach loop of all resources when constructing a resource list for a TV]]> https://forums.modx.com/thread/103405/get-parent-page-titles-in-foreach-loop-of-all-resources-when-constructing-a-resource-list-for-a-tv#dis-post-558387
I have a snippet that gets a list of all resources on the site (with some filtering) for use in a single-select TV. Everything works fine until I also try to get the parent page titles of all of those resources to help make selecting the correct page a little easier as some of my pages have identical pagetitles.

Below is the snippet that works:
<?php
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
$c->where(array(
    'modResource.parent:NOT IN' => $excludeParents,
    'modResource.id:NOT IN' => $excludeParents
));
$resources = $modx->getCollection('modResource',$c);

$pageList = [];
foreach ($resources as $resource) {
    $pageList[] = $resource->get('pagetitle') . '==' . $resource->get('id');
}

$output = implode("||",$pageList);

return $output;


Below is one of my attempts at getting the parent page titles, this results in a 500 error with no errors being logged in Modx or in server error_logs that I can find.
<?php
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
$c->where(array(
    'modResource.parent:NOT IN' => $excludeParents,
    'modResource.id:NOT IN' => $excludeParents
));
$resources = $modx->getCollection('modResource',$c);

$pageList = [];
foreach ($resources as $resource) {
    $parentID = $resource->get('parent');
    $parent = $modx->getObject('modResource', $parentID);
    
    $pageList[] = $resource->get('pagetitle') . ' ('. $parent->get('pagetitle') .')==' . $resource->get('id');
}

$output = implode("||",$pageList);

return $output;

If I just use $parentID in place of $parent->get('pagetitle'), it shows the parent ID's.

I thought maybe Modx didn't like trying to get page titles for ID's of 0, so I also tried adding an if statement to only try to get parent titles if the ID wasn't 0. However, this still results in a 500 error.
<?php
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
$c->where(array(
    'modResource.parent:NOT IN' => $excludeParents,
    'modResource.id:NOT IN' => $excludeParents
));
$resources = $modx->getCollection('modResource',$c);

$pageList = [];
foreach ($resources as $resource) {
    $parentID = $resource->get('parent');
    
    $parentTitle = '';
    if($parentID !== '0'){
      $parent = $modx->getObject('modResource', $parentID);
      $parentTitle = $parent->get('pagetitle');
    }
    
    $pageList[] = $resource->get('pagetitle') . ' ('. $parentTitle .')==' . $resource->get('id');
}

$output = implode("||",$pageList);

return $output;


I appreciate any help!]]>
https://forums.modx.com/thread/103405/get-parent-page-titles-in-foreach-loop-of-all-resources-when-constructing-a-resource-list-for-a-tv#dis-post-558387 Sat, 05 May 2018 03:46:51 +0000 https://forums.modx.com/thread/103405/get-parent-page-titles-in-foreach-loop-of-all-resources-when-constructing-a-resource-list-for-a-tv#dis-post-558387
<![CDATA[xPDO 3 w/o MODx]]> https://forums.modx.com/thread/103189/xpdo-3-w-o-modx#dis-post-558385
I'm working on a project where the front will be JS based (Angular or Vue). The back will be a PHP/JWT API but i'm completely addict to xPDO (not a big fan of Laravel since i have a lot of own classes). Usually i work with MODx but this time i won't need the CMS part so i'd like xPDO only.

Is there a getting started or something to start with for xPDO 3.0?

Thanks!]]>
https://forums.modx.com/thread/103189/xpdo-3-w-o-modx#dis-post-558385 Sat, 05 May 2018 03:40:26 +0000 https://forums.modx.com/thread/103189/xpdo-3-w-o-modx#dis-post-558385
<![CDATA[Create ressource and get id]]> https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558384
i try to create new ressource und then get the id:

this is my snippet:
  
// Part 1  
    $pagetitle = "my ressource";
    $parent = "0";
    $alias = "myalias";

$doc = $modx->newObject('modDocument');
    $doc->set('parent',$parent);
    $doc->set('pagetitle',$pagetitle);
    $doc->set('alias',$alias);
    $doc->save();
  
// Part
    $newdoc = $modx->getObject('modDocument', array('pagetitle'=>$pagetitle));
    $id = $newdoc->get('id');


problem: the second part get the id from ressource with pagetitle "my ressource". If i have many ressource with the same pagetitle, then it doesn't work. Know anybody better solution?]]>
https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558384 Sat, 05 May 2018 03:35:23 +0000 https://forums.modx.com/thread/103221/create-ressource-and-get-id#dis-post-558384
<![CDATA[xPDO schema visualizer demo]]> https://forums.modx.com/thread/76767/xpdo-schema-visualizer-demo?page=2#dis-post-558383 http://schemaviewer.dev.kenters.com/

Suggestions/comments welcome!]]>
https://forums.modx.com/thread/76767/xpdo-schema-visualizer-demo?page=2#dis-post-558383 Sat, 05 May 2018 03:32:16 +0000 https://forums.modx.com/thread/76767/xpdo-schema-visualizer-demo?page=2#dis-post-558383
<![CDATA[Does xPDO automatically manage closing db connections?]]> https://forums.modx.com/thread/103810/does-xpdo-automatically-manage-closing-db-connections#dis-post-558382
I am simply trying to avoid sleeping mySQL connections where the php is holding the connection open for no reason while it is processing data.

ideas?

]]>
https://forums.modx.com/thread/103810/does-xpdo-automatically-manage-closing-db-connections#dis-post-558382 Sat, 05 May 2018 03:28:08 +0000 https://forums.modx.com/thread/103810/does-xpdo-automatically-manage-closing-db-connections#dis-post-558382
<![CDATA[runSnippet getImageList inside getResources (for Collections imageRenderer)]]> https://forums.modx.com/thread/103464/runsnippet-getimagelist-inside-getresources-for-collections-imagerenderer#dis-post-556684 SOLVED - SEE FIRST ANSWER

I am trying to set up a custom imageRenderer Snippet for my Collections view. I want to display the first image of a MIGX-TV which hosts some images.

So I made this Snippet:
$row = $modx->getOption('row', $scriptProperties, '');
$rowId = $row['id'];

$modx->setPlaceholder('getId', $rowId);

$gR = $modx->runSnippet('getResources', [
'parents' => '-1',
'resources' => $rowId,
'tpl' => 'ref-image-id'
]);

return '<a href="[[++site_url]]manager/?a=resource/update&id=' . $row['id'] . '" title="' . $row['pagetitle'] . '"><img src="' . $gR . '" width="60" ></a>';


In my Chunk ref-image-id I have this (only works cached):
[[getImageList?
&tvname=`ref-images`
&tpl=`ref-image-item`
&docid=`[[+getId]]`
&limit=`1`
]]


The ref-image-item Chunk:
[[++site_url]][[+image]]


On my Collections view all of the images have the path of the first resource. The image itself ([[+image]]) is correct.
What I get is:

path/of/resource-1/car.jpg
path/of/resource-1/flower.jpg
path/of/resource-1/house.jpg

when it should be:

path/of/resource-1/car.jpg
path/of/resource-2/flower.jpg
path/of/resource-3/house.jpg

I have tried this without the placeholder and inside my getImageList call using &docid=`[[+id]]` - same problem.]]>
https://forums.modx.com/thread/103464/runsnippet-getimagelist-inside-getresources-for-collections-imagerenderer#dis-post-556684 Fri, 02 Feb 2018 09:50:30 +0000 https://forums.modx.com/thread/103464/runsnippet-getimagelist-inside-getresources-for-collections-imagerenderer#dis-post-556684
<![CDATA[foreach single placeholder]]> https://forums.modx.com/thread/103306/foreach-single-placeholder#dis-post-555929
i try to get my pagetitles as single option-values (placeholder) like this:

<form method="POST" class="form" action="[[~[[*id]]]]">
<select>

    <option>Sport</option>

    <option>Cars</option>

</select>
<input type="submit" value="Send" /></form>


but my snippet put all pagetitle in one row like this:

<form method="POST" class="form" action="[[~[[*id]]]]">
<select>

    <option>SportCars</option>

</select>
<input type="submit" value="Send" /></form>


What ist wrong here? Please help.



My Snippet:

$collection = $modx->getCollection('modResource',array('template' => 4));
 
foreach ($collection as $collect){
    $mytitle.=$collect->get('pagetitle');
    $list = "<option>". $mytitle. "</option>";
    $modx->setPlaceholder('list',$list);
}

return;


HTML Form:

<form method="POST" class="form" action="[[~[[*id]]]]">
<select>
[[+list]]
</select>
<input type="submit" value="Send" /></form>
]]>
https://forums.modx.com/thread/103306/foreach-single-placeholder#dis-post-555929 Sun, 17 Dec 2017 09:39:41 +0000 https://forums.modx.com/thread/103306/foreach-single-placeholder#dis-post-555929
<![CDATA[Alias comparison. To long waiting period.]]> https://forums.modx.com/thread/103271/alias-comparison-to-long-waiting-period#dis-post-555765
I have written a snippet, to compare all alias in my db with the "check" variable. This snippet ist working, but the waiting period (if alias found) is very long. I have only 10 resource!

Is something wrong with this snippet? Does anyone have other, faster ideas?

$check = "home";
$resourses = $modx->getCollection('modResource',array(
   'alias' => $check
));
foreach ($resourses as $resourse) {
  $output = $resourse->get('alias');
}

if (isset($output)) {return "True";} else {return "False";}
]]>
https://forums.modx.com/thread/103271/alias-comparison-to-long-waiting-period#dis-post-555765 Wed, 06 Dec 2017 09:21:19 +0000 https://forums.modx.com/thread/103271/alias-comparison-to-long-waiting-period#dis-post-555765
<![CDATA[Placeholder as link doesn't work]]> https://forums.modx.com/thread/103228/placeholder-as-link-doesn-t-work?page=2#dis-post-555609
i cant set my placeholder as link in chunk:

Snippet:

$newsite = "24";
$modx->setPlaceholder('mysite', $newsite);

$mychunk = $modx->getChunk('site-tpl');
 
$res = $modx->getObject('modResource',$id);
$res->set('content',$mychunk);
$res->save();



Chunk site-tpl:
Here is my new site: <a href="[[~[[+mysite]]]]">My new Site</a><br>
TestID: [[+mysite]]


HTML:
Here is my new site: <a href="">My new Site</a><br>
TestID: 24



what is wrong here?]]>
https://forums.modx.com/thread/103228/placeholder-as-link-doesn-t-work?page=2#dis-post-555609 Fri, 01 Dec 2017 11:33:12 +0000 https://forums.modx.com/thread/103228/placeholder-as-link-doesn-t-work?page=2#dis-post-555609
<![CDATA[array question]]> https://forums.modx.com/thread/102865/array-question#dis-post-554011
Ive a maybe simple (but for me not) array question:

I try to show the groupmembers by a snippet which gets the groups out of a TV:

[[!grouplist? &group=`[[*Gruppe]]`]] (Inpupt fe: 2,3,4


I query it but it always only shows up the first group (2):
'UserGroupMembers.user_group:IN' => array($group)

So my assumption is that I represent the array wrong to the query, because when I do

'UserGroupMembers.user_group:IN' => array(2,3,4) it works!

Any ideas?]]>
https://forums.modx.com/thread/102865/array-question#dis-post-554011 Wed, 20 Sep 2017 09:40:17 +0000 https://forums.modx.com/thread/102865/array-question#dis-post-554011
<![CDATA[Rest API - Mutiple TV's for a collection of resources]]> https://forums.modx.com/thread/102762/rest-api---mutiple-tv-s-for-a-collection-of-resources#dis-post-553781
Was hoping one of the wise members of the modx community could help me, I have two TV's for a template (tv-eample1 and tv-example2). I Was wondering if anyone knows how to get these two different TV's from each resource, I am able to get the TV's using innerJoin but as not all resources will use these TV's, I need to be able to achieve this using left join, although left join does return the TV's the values are wrong, it would use exactly the same value for tv-eample1 and tv-example2 when there both would be different.

here is my code, maybe am going about this wrong, this is for my rest service so won't be using any modx extra as it does use custom tables, but I have removed them to simplify the issue:

Version:MODX Revolution 2.5.7-pl
		
		$myIds = $this->modx->getChildIds(2,2);
		$query = $this->modx->newQuery('modResource'); 
		$query->select(array(
		  'modResource.id','modResource.pagetitle','tvv.contentid as tvvcontentid','tvv.value as example1','tvv2.value as example2','tv.name as tvname','tv2.name as tvname2','tv.id as tvid','tv2.id as tvid2'
		));
		$query->leftJoin('modTemplateVarResource', 'tvv', array(
		    'tvv.contentid = modResource.id'
		));
		$query->leftJoin('modTemplateVarResource', 'tvv2', array(
		    'tvv2.contentid = modResource.id'
		));
		$query->leftJoin('modTemplateVar', 'tv', array('tv.id = tvv.tmplvarid','tv.name' => 'tv-eample1'));
		$query->leftJoin('modTemplateVar', 'tv2', array('tv2.id = tvv2.tmplvarid','tv2.name' => 'tv-example2'));	
		$query->where(array('id:IN' => $myIds));
		$query->groupby('id');
		$items = $this->modx->getCollection('modResource', $query);


Any help will be appreciated.

Thanks]]>
https://forums.modx.com/thread/102762/rest-api---mutiple-tv-s-for-a-collection-of-resources#dis-post-553781 Fri, 08 Sep 2017 10:24:54 +0000 https://forums.modx.com/thread/102762/rest-api---mutiple-tv-s-for-a-collection-of-resources#dis-post-553781
<![CDATA[Sorting by pagetitle]]> https://forums.modx.com/thread/101054/sorting-by-pagetitle#dis-post-553023
I cannot sort by pagetitle:

$resources[] = $res->get('pagetitle') . '==' .$res->get('pagetitle');
$resources->sortby('pagetitle', 'ASC'); 


2nd line doesnt work!
Any ideas?]]>
https://forums.modx.com/thread/101054/sorting-by-pagetitle#dis-post-553023 Sat, 05 Aug 2017 09:46:26 +0000 https://forums.modx.com/thread/101054/sorting-by-pagetitle#dis-post-553023
<![CDATA[Re: Class 'pdoMenu_br4in3x' not found]]> https://forums.modx.com/thread/102479/re-class-pdomenu-br4in3x-not-found#dis-post-552188 https://forums.modx.com/thread/102479/re-class-pdomenu-br4in3x-not-found#dis-post-552188 Fri, 30 Jun 2017 09:23:55 +0000 https://forums.modx.com/thread/102479/re-class-pdomenu-br4in3x-not-found#dis-post-552188