<?php
$value = !empty($default) ? $default : '';
if ($criteria = $modx->newQuery($class, $modx->fromJSON($where))) {
$pk = $modx->getPK($class);
if (!is_array($pk)) {
$pk = array($pk);
} else {
$pk = array_values($pk);
}
$criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, array($field))));
if ($object = $modx->getObject($class, $criteria)) {
$value = $object->get($field);
}
}
return $value;
?>[[$sidebox?
&title=`[[getValue? &class=`modResource` &field=`menutitle` &where=`{"id":[[UltimateParent]]}`]]`
&element=`Wayfinder`
&properties=`@SidebarMenu? &startId=`[[UltimateParent]]``
]]<div class="sidebox"> <h1>[[+title]]</h1> [[[[+element]][[+properties]]]] </div>
Authored by <span class="author">[[getValue? &class=`modUserProfile` &field=`fullname` &where=`{"internalKey":[[+createdby]]}` &default=`anonymous`]]</span>
$fields = trim($fields);
$fields = preg_replace('/\s/',$fields);
$fields_array = explode(',', $fields);
$criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, $fields)));
&where=`{"internalKey":[[+createdby]]}` &where=`{"internalKey":"[[+createdby]]"}` [[!getValue &class=`modUser` &field=`username` &where=``&default=`anonymous`]]
Nope, that would output the first modUser in the database by natural order. That is effectively the same as saying:
Shouldn’t this output ’anonymous’?
[[!getValue &class=`modUser` &field=`username` &where=``&default=`anonymous`]]
SELECT `id`, `username` FROM modx_users WHERE 1
I have another snippet in development, based on this, called getObject that allows selection of a specific set of columns and allows you to either save it to a set of prefixed placeholders, or use a Chunk to tpl the output per the typical MODx MO. In this case, the idea is to return a specific column value directly.
What if you wanted to get more than a single value from that record? Granted, that may be a bit out of scope for the Snippet, but it would represent a common use-case, e.g. return a couple more details about the author of a post, not just his username.
Why not trim and explode the $field (better name $fields?) right off the get go, e.g.
$fields = trim($fields); $fields = preg_replace('/\s/',$fields); $fields_array = explode(',', $fields); $criteria->select($modx->getSelectColumns($class, '', '', array_merge($pk, $fields)));
Not necessarily, since [[+createdby]] represents a numeric value: the user id.
Also... I’m slightly confused by the JSON format inferred here:
&where=`{"internalKey":[[+createdby]]}`
Shouldn’t it use double-quotes for the key and the value? :
&where=`{"internalKey":"[[+createdby]]"}`
I have another snippet in development, based on this, called getObject that allows selection of a specific set of columns and allows you to either save it to a set of prefixed placeholders, or use a Chunk to tpl the output per the typical MODx MO. In this case, the idea is to return a specific column value directly.
No Snippet can return anything but a string, so that’s a moot point IMO. No, I’m going to call it that because the whole purpose is to align it with the xPDO method and it’s functionality. This is just a Snippet form that provides utility for using an xPDOObject in MODx without having to use the API. That makes the name perfectly appropriate from my perspective. I will also be doing a getCollection snippet.
Quote from: OpenGeek at Apr 29, 2010, 12:43 PM
I have another snippet in development, based on this, called getObject that allows selection of a specific set of columns and allows you to either save it to a set of prefixed placeholders, or use a Chunk to tpl the output per the typical MODx MO. In this case, the idea is to return a specific column value directly.
Any chance of giving it another name? I think the Revolution object methods will be difficult for many users and having a snippet with the same name as a $modx object method might add to the confusion. From the sound of it, it’s not returning a standard MODx object, either.