Yii2 render two models on one view - yii2

Yii is the best but I'm having a wee bit of trouble replicating some of the code from yii1 to yii2, below I use the code in yii to render two models in one view and would like to replicate this using yii2. Grateful for any help. thanks
VwContractDetailsController
public function actionView($id)//create new dataprovider and pass param from url
{
$events=$dataProvider=new CActiveDataProvider('VwContractEvents', array(
'criteria'=>array(
'condition'=>'Contractkey_id=:aid',
'params'=>array(':aid'=>$id)
),
'pagination'=>array(
'pageSize'=>2
),
'sort' => array(
'defaultOrder' => 'EventDate DESC',
),
));
$this->render('view',array(
'model'=>$this->loadModel($id),
'events'=>$events,
));
}
vwContractEvents Index //provide full path for itemview
<?php $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'/vwcontractevents/_view',
'enablePagination' => true,
)); ?>
vwContractDetails view //render VwcontractEvents/index on vwContractDetails view
<?php
$this->renderPartial('/VwContractEvents/index',array('dataProvider'=>$events));
?>

The equivalent of renderPartial() in yii2 view is:
yii\base\View::render()
In controller you render with:
yii\base\Controller::render()

Related

Why my listview not found any record in yii2?

I have a dataprovider to search a world but my listview does not display any record? How can i send this query object to listview?
every thing in my webpage is worked very good but at the output my list view show "not found any result" this mean my list view code is no have problem . problem is in my dataprovider and this query beacuse i customize that
my controller:
$query = new Query();
$dataProvidersearch=new ActiveDataProvider([
'query'=>$query->from('tbl_post')->Where(['like', 'title', $search])-
>andWhere(['like', 'en_title', $search])->andWhere(['like', 'content', $search])->andWhere(['like', 'en_content', $search]),
]);
this is my list view in my view:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
]);
I'm not sure you have enough code here for someone to help. Even something simple like a listview could consist of a view, a controller, and two model files and your code could be failing at any of these points. You may have simply forgot to include the listview library at the top of your view, but we can't see that in your current example.
What I would recommend is using Gii to generate a listview. It is simple to do and once you have it created, you can study the code to see where you went wrong. You can see how to get started generating code with Gii here: http://www.yiiframework.com/doc-2.0/guide-start-gii.html
ANSWER FROM COMMENTS: Replace andWhere with orWhere, no results are found because no record can match 'title' and 'en_title' and 'content' and 'en_content'.
You are submitting $posts as 'dataProvider' while it should be dataProvidersearch
Instead of:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$posts,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];
Should be:
$posts = $model->getModels();
echo ListView::widget([
'dataProvider'=>$dataProvidersearch,
'itemView'=>'search',
'summary' => '',
'itemOptions' => [
'tag' => false
],
];

Customize prestashop web service

I want to customize Prestashop web service for my own usage but I don't know how and I can't find any tutorial. I have a mobile application that want to retrieve data from website but the default web service is useless.
For example I want the list of categories (in a language) with they're pictures but It seems I should call two different service to retrieve categories and images separately.
Assume I want to have a JSON array of categories that a category is a JSON object that have these fields {id,title,imageUrl} but It seems I should get {id,title} with a method and after that I can get images on by one by another method!
I couldn't find any guide for extending or customizing web service in the documentation.
I'm a bit late but:
Prestashop version requier : > 1.6
If you want to customize a web service and return specific fields with a JSON format output. You need to do it this way:
First
In override :
Create a class : myClassForWs that extends myClassCore
Override WebserviceRequest in webservice/WebserviceRequest.php
In webservice/WebserviceRequest.php : Add myClassForWs to
ressources:
public static function getResources()
{
$resources = parent::getResources();
$resources['myClassForWs'] = array('description' => 'The class','class' => 'myClassForWs');
ksort($resources);
return $resources;
}
}
In myClassForWs : redefine $webserviceParameters and $definition with the fields you need:
protected $definition = array(
'table' => 'category',
'primary' => 'id_category',
'multilang' => true,
'multilang_shop' => true,
'fields' => array(
'name' =>array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCatalogName', 'required' => true, 'size' => 128),
'link_rewrite' =>array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isLinkRewrite', 'required' => true, 'size' => 128),
'description' =>array('type' => self::TYPE_HTML, 'lang' => true, 'validate' => 'isCleanHtml'),
),
);
protected $webserviceParameters = array(
'objectsNodeName' =>'categories',
'hidden_fields'=>array('nleft', 'nright', 'groupBox'),
'fields' => array(
'level_depth' => array('setter' => false),
),
'associations' => array(
'images' => array(
'resource' => 'image',
'fields' => array('id' => array())
),
),
);
Then
Go in your admin tab :
in performance : clear cache
in advanced settings > Web service: active a api Key and set myClassForWs for this key
Finally
Access to your web service with url :
my.prestashop/api/myClassForWs/{id_class}?output_format=**JSON**
And it returns your datas
I hope it helps.

html to pdf converter in yii2 with pagination in table

view:
<p>
<?= Html::a('Download This page', ['report'], ['class' => 'btn btn-danger']) ?>
</p>
controller:
public function actionReport()
{
// setup kartik\mpdf\Pdf component
$pdf = new Pdf([
'content' => $content,
'options' => ['title' => 'Krajee Report Title'],
'methods' => [
'SetHeader' => ['Krajee Report Header'],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();
}
This function works perfectly but my html table has pagination . so i am confused how to deal with table that has pagination.
You should disable the pagination. it all depends on how you define your data provider (read more about data providers here http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html). Probably you should do something like this
************* = new ActiveDataProvider([
'pagination' => false,
..............
]);
I think you can also call it like
$dataProvider->pagination =false;
Just in case you need to disable it in a specific case.

RESTful webservice with Zend Framework 2.1.* - JsonModel is not shown

I read several Tutorials to create a restful webservice with ZF2. I saw that the last changes how ZF2 handles restful webservices happened in version 2.0.4. The most promising article to get me started was this:
http://dustint.com/post/543/getting-started-with-abstractrestfulcontroller
Anyway, I can't get it done, it seems to me, that in RestController.getList() my returned JsonModel is not working like expected. Due to my Debug-call I can recognize that my RestController.getList()-method will be invoked. All related code is in my github-repository here:
https://github.com/Jochen1980/EhcServer/blob/master/module/Application/src/Application/Controller/RestController.php
class RestController extends AbstractRestfulController{
public function indexAction(){
Debug::dump("indexAction()");
return new ViewModel();
}
public function getList() {
Debug::dump("getList()");
return new JsonModel(array(
array('name' => 'test'),
array('name' => 'second')
));
}
...
Currently I got this error message:
Fatal error: Uncaught exception 'Zend\View\Exception\RuntimeException' with message 'Zend\View\Renderer\PhpRenderer::render: Unable to render template "application/rest/get-list"; resolver could not resolve to a file' in C:\xampp\htdocs\EhcServer\vendor\zendframework\zendframework\library\Zend\View\Renderer\PhpRenderer.php on line 499
Thanks in advance!
Your strategies need to be inside view_manager in module.config.php
ie., the view manager section should look like this
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
// let the view manager know which strategies to use
'strategies' => array(
'ViewJsonStrategy',
),
),
If you are working on the Abstract RestfulConroller, simply
'view_manager' => array(
// let the view manager know which strategies to use
'strategies' => array(
'ViewJsonStrategy',
),
),
will make right, because the json itself enough to show in the rest methods,
$array = array();
return new JsonModel($array);
Thanks,

How can I pass output of a widget as content in CJuiTabs in Yii?

How can I pass output of a widget as content in CJuiTabs in Yii?
Here the code I tried and get error:
$this->widget('zii.widgets.jui.CJuiTabs',array(
'tabs'=>array(
'Tab1'=> array('content' => $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner' )),
'id' => 'tab1'),
'tab2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>true,
),
));
it gives this error:
Object of class CListView could not be converted to string
Edited: As well as Stu's answer I found this : http://yiibook.blogspot.nl/2012/09/handle-cjuitabs-in-yii.html
Yeah, content expects a string and the widget doesn't return a string. I found this blog piece here: http://mrhandscode.blogspot.com/2012/03/insert-widget-to-another-widget-in-yii.html
The owner found a pretty innovative way round this issue, using output buffering to collect the output of the one widget and then inserting that into the second.
You might be able to achieve it with something like this:
ob_start();
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner'
));
$tab1Content=ob_get_contents();
ob_end_clean();
$this->widget('zii.widgets.jui.CJuiTabs',array(
'tabs'=>array(
'Tab1'=> array('content' => $tab1Content,'id' => 'tab1'),
'tab2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>true,
),
));
I've not tested, and may need tinkering!
You can set the second parameter of $this->widget() to true, so the method will return the content of the widget istead of echoing it.
$this->widget('zii.widgets.jui.CJuiTabs',array(
'tabs'=>array(
'Tab1'=> array('content' => $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner' ), true),
'id' => 'tab1'),
'tab2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
),
// additional javascript options for the tabs plugin
'options'=>array(
'collapsible'=>true,
),
below is ok.
'Tab1'=> array('content' => $this->widget('zii.widgets.CListView', array(
'dataProvider'=>$vulnerdataProvider,
'itemView'=>'_latest_vulner' ), true)