本文整理汇总了PHP中yii\data\ActiveDataProvider::prepare方法的典型用法代码示例。如果您正苦于以下问题:PHP ActiveDataProvider::prepare方法的具体用法?PHP ActiveDataProvider::prepare怎么用?PHP ActiveDataProvider::prepare使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\data\ActiveDataProvider
的用法示例。
在下文中一共展示了ActiveDataProvider::prepare方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: init
public function init()
{
if (is_array($this->remap)) {
$this->isClosure = false;
} elseif (is_callable($this->remap)) {
$this->isClosure = true;
} else {
throw new ErrorException('remap is wrong type!.');
}
//init dataProvider
$this->dataProvider->prepare();
return parent::init();
}
示例2: actionIndex
/**
* Lists all MyClick models.
* @return mixed
*/
public function actionIndex()
{
$query = (new \yii\db\Query())->select('article.*')->from('my_click')->where(['user_id' => \Yii::$app->user->id])->orderBy('my_click.id DESC');
$click_query = $query->limit(50)->join('LEFT JOIN', 'article', 'article.id = my_click.article_id');
$provide = new ActiveDataProvider(['query' => $click_query, 'pagination' => ['pageSize' => 10]]);
$provide->prepare();
$pagination = $provide->getPagination();
return $this->render('index', ['data' => $provide->getModels(), 'pageCount' => $pagination->pageCount, 'page' => $pagination->page, 'links' => $pagination->getLinks()]);
}
示例3: actionIndex
public function actionIndex()
{
if (\Yii::$app->user->isGuest) {
$this->redirect(['user/sign-in/login']);
}
$article = (new Query())->from('article')->all();
$query = Article::find();
$provide = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10], 'sort' => ['defaultOrder' => ['created_at' => SORT_DESC, 'title' => 'simon']]]);
$provide->prepare();
$pagination = $provide->getPagination();
return $this->render('index', ['article' => $provide->getModels(), 'pageCount' => $pagination->pageCount, 'page' => $pagination->page, 'links' => $pagination->getLinks()]);
}
示例4: actionIndex
/**
* Lists all words.
* @param string $sort
* @param string $search
* @return mixed
*/
public function actionIndex($sort = null, $search = null)
{
$query = Word::find()->userId(Yii::$app->user->id);
if ($search === null) {
$query->sort($sort);
} else {
$query->search($search);
}
$provider = new ActiveDataProvider(['query' => $query, 'sort' => false, 'pagination' => ['pageSize' => Yii::$app->params['wordPerPage']]]);
$provider->prepare(true);
return $this->render('index', ['provider' => $provider, 'search' => $search]);
}
示例5: search
public function search($params)
{
$query = Url::find();
$dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => []]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere(['user_id' => $this->user_id]);
$query->andFilterWhere(['like', 'link', $this->link])->andFilterWhere(['like', 'name', $this->name]);
$dependency = new \yii\caching\DbDependency(['sql' => 'SELECT MAX(updated_at) FROM url', 'reusable' => true]);
Yii::$app->db->cache(function ($db) use($dataProvider) {
$dataProvider->prepare();
}, Yii::$app->params['cacheExpire'], $dependency);
return $dataProvider;
}
示例6: actionCategory
/**
// * @param integer|null $category
* @return array - dataProvider and pagination.
*/
public function actionCategory()
{
// controller->action->id: e.g table-lights
$categoryID = $this->findCategoryBySlug(\Yii::$app->controller->action->id);
// $productsQuery = Product::find();
// $productsQuery->joinWith('category');
// $productsQuery
// ->orFilterWhere(['category.parent_id' => $categoryID])
// ->orWhere(['category.id' => $categoryID]);
$productsQuery = Product::find()->joinWith('category')->orFilterWhere(['category.parent_id' => $categoryID])->orWhere(['category.id' => $categoryID]);
$dataProvider = new ActiveDataProvider(['query' => $productsQuery, 'pagination' => ['pagesize' => 3]]);
$dependency = new DbDependency();
$dependency->sql = 'SELECT count(id) FROM product';
\Yii::$app->db->cache(function ($db) use($dataProvider) {
$dataProvider->prepare();
}, 3600, $dependency);
return $dataProvider;
}
示例7: buildPages
/**
* Create sitemap files
* @return array
*/
public function buildPages()
{
/** @var \yii\db\ActiveRecord $owner */
$owner = $this->owner;
$query = $owner::find();
$basePath = $this->module->getFilesPath();
preg_match('/([A-Za-z]+)$/', get_class($owner), $matchResult);
$fileSuffix = strtolower($matchResult[0]);
// Apply scopes
if (is_callable($this->scope)) {
call_user_func($this->scope, $query);
}
// Build data provider for separated on pages
$dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => $this->module->perPage, 'pageSizeLimit' => [10, 50000]]]);
// Build pages
$pages = [];
$dataProvider->prepare();
$pageCount = $dataProvider->pagination->getPageCount();
for ($page = 0; $page < $pageCount; $page++) {
$dataProvider->pagination->setPage($page);
$dataProvider->prepare(true);
// Processing one page
$pageUrls = [];
$n = 0;
foreach ($dataProvider->getModels() as $model) {
$urlData = call_user_func($this->dataClosure, $model);
$pageUrls[$n]['loc'] = $urlData['loc'];
$pageUrls[$n]['lastmod'] = $urlData['lastmod'];
if (isset($urlData['changefreq'])) {
$pageUrls[$n]['changefreq'] = $urlData['changefreq'];
} elseif ($this->defaultChangefreq !== false) {
$pageUrls[$n]['changefreq'] = $this->defaultChangefreq;
}
if (isset($urlData['priority'])) {
$pageUrls[$n]['priority'] = $urlData['priority'];
} elseif ($this->defaultPriority !== false) {
$pageUrls[$n]['priority'] = $this->defaultPriority;
}
if (isset($urlData['news'])) {
$pageUrls[$n]['news'] = $urlData['news'];
}
if (isset($urlData['images'])) {
$pageUrls[$n]['images'] = $urlData['images'];
}
++$n;
}
$xmlData = Yii::$app->view->renderPhpFile($this->module->viewPath . '/default/page-template.php', ['urls' => $pageUrls]);
$fileUrl = "sitemap_files/{$fileSuffix}_{$page}.xml";
if (file_put_contents($basePath . '/' . $fileUrl, $xmlData)) {
$pages[] = ['loc' => $fileUrl, 'lastmod' => time()];
echo "OK: {$basePath}/{$fileUrl}\n";
}
}
return $pages;
}
示例8: init
/**
* init
*/
public function init()
{
//init dataProvider
$this->dataProvider->prepare();
return parent::init();
}
示例9: getDataProvider
/**
* @param $params
* @return ActiveDataProvider
* @throws \yii\base\InvalidConfigException
*/
public static function getDataProvider($params)
{
$query = Post::find()->with('user', 'topic')->orderBy(['created_at' => SORT_ASC]);
if ($params['topic_id']) {
$query->andWhere(['topic_id' => $params['topic_id']]);
}
$dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['route' => '/topic/default/view', 'params' => ['id' => $params['topic_id'], 'page' => $params['page']], 'forcePageParam' => false, 'pageSizeLimit' => false, 'defaultPageSize' => Yii::$app->config->get('display_posts_count')]]);
$dataProvider->prepare();
return $dataProvider;
}
示例10: exportToFile
/**
* Вывод в файл
* @param ActiveDataProvider $dataProvider
* @param string $sFileName
* @return boolean
*/
public function exportToFile($dataProvider, $sFileName)
{
$dataProvider->prepare();
$nMaxCount = $dataProvider->pagination->totalCount;
$objPHPExcel = new PHPExcel();
$oSheet = $objPHPExcel->getSheet(0);
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3;
$bCache = PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
$oDefaultStyle = $objPHPExcel->getDefaultStyle();
$oDefaultStyle->getFont()->setName('Arial');
$oDefaultStyle->getFont()->setSize(8);
// $oSheet->getPageSetup()
// ->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT) // ORIENTATION_LANDSCAPE
// ->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4)
// ->setFitToPage(true)
// ->setFitToWidth(1)
// ->setFitToHeight(0);
//
// $oSheet->getPageMargins()
// ->setTop(0.5)
// ->setRight(0.35)
// ->setLeft(0.35)
// ->setBottom(1);
// $oSheet->getHeaderFooter()
// ->setEvenFooter('&CСтраница &P [&N]')
// ->setOddFooter('&CСтраница &P [&N]');
$nPageCount = $dataProvider->pagination->pageCount;
if ($dataProvider->pagination->totalCount > $nMaxCount) {
$nPageCount = floor($nMaxCount / $dataProvider->pagination->pageSize);
}
$cou = 0;
$nRow = $this->nStartRow;
foreach ($this->columnWidth as $k => $v) {
if ($v !== null) {
$oSheet->getColumnDimension($this->colIndexToName($k + 1))->setWidth($v);
}
}
$sLastCol = $this->colIndexToName(max(count($this->columnWidth), count($this->columnTitles), count($this->columnValues)));
$n = $nRow;
$sTit = 'A' . $nRow++;
$oSheet->setCellValue($sTit, 'Выгрузка от ' . date('d.m.Y H:i'));
$objPHPExcel->getActiveSheet()->mergeCells($sTit . ':' . $sLastCol . $n);
$sdataTitle = $this->dataTitle === null ? Yii::$app->name : $this->dataTitle;
if ($sdataTitle != '') {
$n = $nRow;
$sTit = 'A' . $nRow++;
$oSheet->setCellValue($sTit, $sdataTitle);
$objPHPExcel->getActiveSheet()->mergeCells($sTit . ':' . $sLastCol . $n);
}
if (count($this->columnTitles) > 0) {
$nRow++;
$oSheet->fromArray($this->columnTitles, null, 'A' . $nRow++);
}
for ($page = 0; $page < $nPageCount; $page++) {
$dataProvider->pagination->setPage($page);
$dataProvider->refresh();
foreach ($dataProvider->getModels() as $model) {
$aData = [];
foreach ($this->columnValues as $v) {
$aData[] = $v instanceof Closure ? call_user_func($v, $model, $cou) : $model->{$v};
}
$oSheet->fromArray($aData, null, 'A' . $nRow);
$cou++;
$nRow++;
}
}
$oSheet->getPageSetup()->setPrintArea('A' . $this->nStartRow . ':' . $sLastCol . ($nRow - 1));
$format = $this->getFileExt($sFileName, 'xls');
if ($format == 'xls') {
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
} else {
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
}
$objWriter->save($sFileName);
return true;
}