本文整理匯總了PHP中DataList::stat方法的典型用法代碼示例。如果您正苦於以下問題:PHP DataList::stat方法的具體用法?PHP DataList::stat怎麽用?PHP DataList::stat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類DataList
的用法示例。
在下文中一共展示了DataList::stat方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: getSideloadData
/**
* Fetches and return all the data that need to be sideloaded
* for a specific source DataObject or DataList.
*
* @param DataObject|DataList $dataSource The source data to fetch sideloaded records for
* @return array A map of relation names with their data
*/
protected function getSideloadData($dataSource)
{
$data = array();
if ($dataSource instanceof DataObject) {
$has_one = $dataSource->stat('has_one');
$has_many = $dataSource->stat('has_many');
$many_many = $dataSource->stat('many_many');
$belongs_many_many = $dataSource->stat('belongs_many_many');
$relationsMap = array();
if (is_array($has_one)) {
$relationsMap = array_merge($relationsMap, $has_one);
}
if (is_array($has_many)) {
$relationsMap = array_merge($relationsMap, $has_many);
}
if (is_array($many_many)) {
$relationsMap = array_merge($relationsMap, $many_many);
}
if (is_array($belongs_many_many)) {
$relationsMap = array_merge($relationsMap, $belongs_many_many);
}
// if a single DataObject get the data for each relation
foreach ($this->sideloadedRecords[$dataSource->ClassName] as $relationName) {
$newData = $this->getEmbedData($dataSource, $relationName);
if ($newData !== null) {
// has_one are only simple array and we want arrays or array
if (is_array($has_one) && in_array($relationName, $has_one)) {
$newData = array($newData);
}
$relationClass = $relationsMap[$relationName];
$data[$relationClass] = $newData;
}
}
} elseif ($dataSource instanceof DataList) {
// if a list of DataObject, loop through each and merge all the data together
foreach ($dataSource as $dataObjectSource) {
$sideloadData = $this->getSideloadData($dataObjectSource);
//combine sideloaded records
foreach ($sideloadData as $class => $extraData) {
if (!isset($data[$class])) {
$data[$class] = array();
}
// merge for list of records, push for individual record
$arrayOfArrays = array_filter($extraData, 'is_array');
if (count($arrayOfArrays) == count($extraData)) {
//print_r('array_merge');
$data[$class] = array_merge($data[$class], $extraData);
} else {
//print_r('array_push');
array_push($data[$class], $extraData);
}
}
}
// remove duplicates
foreach ($data as $relationClass => $relationData) {
$data[$relationClass] = array_unique($relationData, SORT_REGULAR);
}
}
return $data;
}