Returns an array of data as the specified result type: object or array.
public result ( string $DatasetType = false ) | ||
$DatasetType | string | The format in which to return a row: object or array. The following values are supported. - DATASET_TYPE_ARRAY: An array of associative arrays. - DATASET_TYPE_OBJECT: An array of standard objects. - FALSE: The current value of the DatasetType property will be used. |
/** * Add user data to a result set. * * @param array|Gdn_DataSet $Data Results we need to associate user data with. * @param array $Columns Database columns containing UserIDs to get data for. * @param array $Options Optionally pass list of user data to collect with key 'Join'. */ public function joinUsers(&$Data, $Columns, $Options = []) { if ($Data instanceof Gdn_DataSet) { $Data2 = $Data->result(); } else { $Data2 =& $Data; } // Grab all of the user fields that need to be joined. $UserIDs = []; foreach ($Data as $Row) { foreach ($Columns as $ColumnName) { $ID = val($ColumnName, $Row); if (is_numeric($ID)) { $UserIDs[$ID] = 1; } } } // Get the users. $Users = $this->getIDs(array_keys($UserIDs)); // Get column name prefix (ex: 'Insert' from 'InsertUserID') $Prefixes = []; foreach ($Columns as $ColumnName) { $Prefixes[] = StringEndsWith($ColumnName, 'UserID', true, true); } // Join the user data using prefixes (ex: 'Name' for 'InsertUserID' becomes 'InsertName') $Join = val('Join', $Options, ['Name', 'Email', 'Photo']); foreach ($Data2 as &$Row) { foreach ($Prefixes as $Px) { $ID = val($Px . 'UserID', $Row); if (is_numeric($ID)) { $User = val($ID, $Users, false); foreach ($Join as $Column) { $Value = $User[$Column]; if ($Column == 'Photo') { if ($Value && !isUrl($Value)) { $Value = Gdn_Upload::url(changeBasename($Value, 'n%s')); } elseif (!$Value) { $Value = UserModel::getDefaultAvatarUrl($User); } } setValue($Px . $Column, $Row, $Value); } } else { foreach ($Join as $Column) { setValue($Px . $Column, $Row, null); } } } } }
/** * Add denormalized views to discussions. * * @param Gdn_DataSet|stdClass $Discussions */ public function addDenormalizedViews(&$Discussions) { if ($Discussions instanceof Gdn_DataSet) { $Result = $Discussions->result(); foreach ($Result as &$Discussion) { $CacheKey = sprintf(DiscussionModel::CACHE_DISCUSSIONVIEWS, $Discussion->DiscussionID); $CacheViews = Gdn::cache()->get($CacheKey); if ($CacheViews !== Gdn_Cache::CACHEOP_FAILURE) { $Discussion->CountViews += $CacheViews; } } } else { if (isset($Discussions->DiscussionID)) { $Discussion = $Discussions; $CacheKey = sprintf(DiscussionModel::CACHE_DISCUSSIONVIEWS, $Discussion->DiscussionID); $CacheViews = Gdn::cache()->get($CacheKey); if ($CacheViews !== Gdn_Cache::CACHEOP_FAILURE) { $Discussion->CountViews += $CacheViews; } } } }