or even more efficient, if all you want is the id and nothing else, would be:
<?php
$id = 0;
$alias = 'something';
/* build a new xPDOQuery object, similar to getObject() */
$query = $modx->newQuery('modResource', array('alias' => $alias));
/* indicate you only want to select the id column */
$query->select('id');
/* prepare the PDOStatement and execute it directly */
if ($query->prepare() && $query->stmt->execute()) {
/* use PDOStatement->fetchColumn() to just get the first column of
the current row of the result set */
$id = $query->stmt->fetchColumn();
}
This avoids all the overhead of loading all the other columns from the database and instantiating an object of the proper modResource class. Instead it still builds the query using the xPDOQuery methods and then uses the
PDOStatement created by calling
xPDOQuery->prepare() to
execute() the query and
fetchColumn() to get the first column of the first row in the result set. it’s a little more code, but the memory usage and processing time will be lower.
The more you know...