I'm new at yii and trying to find my way around. I have done a tutorial or two. I then decided to start editing/changing the example to allow me to learn more. I created a page that does a simple PING. That gets validated. On success, it loads a static page. This all works.
What I wanted to do next is to see how I can utilize a grid to populate that with some data. My real life example is the same. I will get a array of data coming in.
It seems that CArrayDataProvider is what I need. So, I am trying to get a very simple example to work. If I get this to work, I can move on.
I have tried a whole bunch of examples. The error is the same every time. It seems that I do not have CArrayDataProvider installed? If that is even possible.
I did a standard basic install:
composer create-project --prefer-dist yiisoft/yii2-app-basic basic
I have the following at the beginning of my controllers file:
use yii2\data\ArrayDataProvider;
I get no error here.
I searched for the file itself on the file system, could not find it. I did find ArrayDataProvider, so I tried that, same result:
use vendor\yiisoft\yii2\data\ArrayDataProvider;
The error is:
PHP Fatal Error – yii\base\ErrorException
Class 'CArrayDataProvider' not found
This is on line 24:
"dataProvider = new CArrayDataProvider($fruits);"
Here is my example code. Not that I think the issue is in here, but to show what I am trying to do:
$fruits = array(
array('id' => 1, 'name'=>'apple', 'color' => 'green'),
array('id' => 2, 'name'=>'orange', 'color' => 'orange'),
array('id' => 3, 'name'=>'banana', 'color' => 'yellow'),
array('id' => 4, 'name'=>'pineapple', 'color' => 'brown')
);
$dataProvider = new CArrayDataProvider($fruits);
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'fruits-grid',
'dataProvider' => $dataProvider ,
'columns' => array(
array(
'name' => 'ID',
'value' => '$data["id"]',
),
array(
'name' => 'Name',
'value' => '$data["name"]'
),
array(
'name' => 'Color',
'value' => '$data["color"]'
),
)
));
On the file-system (linux) itself I did a update:
composer update
I have been Googling for the last 2 days and I am finding nothing.
I tried adding a date picket. That worked. I used:
https://www.tutorialspoint.com/yii/yii_extensions.htm
So in short, the static page that I call, now displays a DateTimePicker.
At the start of the file I added:
use kartik\datetime\DateTimePicker;
And in the body:
<?php
echo DateTimePicker::widget([
'name' => 'dp_1',
'type' => DateTimePicker::TYPE_INPUT,
'value' => '23-Feb-1982 10:10',
'pluginOptions' => [
'autoclose'=>true,
'format' => 'dd-M-yyyy hh:ii'
]
]);
?>
How do I get yii2 to allow me to use ArrayDataProvider. Or how do I install the extension? Or who do I reference it?
In Yii2 there's not a CArrayDataProvider. Use ArrayDataProvider, like described in docs:
$provider = new yii\data\ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
Pretty well documented here.
Yii2 supports ArrayDataProvider and yii 1.* supports CArrayDataProvider, so as per your code you are using Yii2, so just replace the below line with
$dataProvider = new CArrayDataProvider($fruits);
With this:
$dataProvider = new ArrayDataProvider($fruits);
Thx to everyone's help! I now have a working example.
In case anyone else has the same issues, I will post my working code here:
use kartik\grid\GridView;
$resultData = [
array("id"=>1,"name"=>"Cyrus","email"=>"risus#consequatdolorvitae.org"),
array("id"=>2,"name"=>"Justin","email"=>"ac.facilisis.facilisis#at.ca"),
array("id"=>3,"name"=>"Mason","email"=>"in.cursus.et#arcuacorci.ca"),
array("id"=>4,"name"=>"Fulton","email"=>"a#faucibusorciluctus.edu"),
array("id"=>5,"name"=>"Neville","email"=>"eleifend#consequatlectus.com"),
array("id"=>6,"name"=>"Jasper","email"=>"lectus.justo#miAliquam.com"),
array("id"=>7,"name"=>"Neville","email"=>"Morbi.non.sapien#dapibusquam.org"),
array("id"=>8,"name"=>"Neville","email"=>"condimentum.eget#egestas.edu"),
array("id"=>9,"name"=>"Ronan","email"=>"orci.adipiscing#interdumligulaeu.com"),
array("id"=>10,"name"=>"Raphael","email"=>"nec.tempus#commodohendrerit.co.uk"),
];
$dataProvider = new \yii\data\ArrayDataProvider([
'key'=>'id',
'allModels' => $resultData,
'sort' => [
'attributes' => ['id', 'name', 'email'],
],
]);
echo GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'attribute' => 'name',
'value' => 'name',
],
[
"attribute" => "email",
'value' => 'email',
]
]
]);
Related
I need to create mongodb collection while executing code, by checking whether the collection is exists or not.
I tried below things
Yii::$app->db->createCommand()-> createCollection("collection_name");
but collection not created.
Please help.
Issue resolved..
Yii::$app->db->createCommand()->createCollection("collection_name")->execute();
and index will added for collection as,
$collectionName = Yii::$app->db->getCollection("collection_name");
$collectionName->createIndexes([
'key' => ['id' => 'int'],
'name' => 'id_index'
],
[
'key' => ['id' => 'int', 'category' => 'text'],
'name' => 'id_category_index',
]);
How can I sort with a customized gridview header?
Please give the difference between label and header in the Yii2 gridview widget dataprovider.
Here is my code:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'header' => 'Page Title',
'label' => 'Title'
],
]); ?>
Do header and label perform the same function?
How can I perform sorting in $data->myTitle?
Here my Output Screen:
I want Page Title, Status, Date Modified should be active.
Thanks in advance.
Found the answer.
Please add attributes to ActiveDataProvider in your Search Model.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => 5,
],
'sort' => ['attributes' => ['myTitle']],
]);
Add the attribute option in widget:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'class' => 'yii\grid\DataColumn',
'value' => function ($data) {
return $data->myTitle;
},
'headerOptions' => ['style'=>'text-align:center'],
'attribute' => 'myTitle',
'label' => 'Page Title'
],
]); ?>
Since myTitle is a field from the database and not a custom value, you can just use attribute. The rest may be unnecessary e.g the default class is DataColumn
'columns' => [
[
'attribute' => 'myTitle',
'label' => 'Label',
]
I am not very sure I understand your question, but sorting option can be included in your modelsearch.php. So in your case you have to do like this.
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort'=> ['defaultOrder' => ['your_column'=>SORT_ASC]]
]);
if myTitle is a field in the database, why you are using such a long syntax. Just
'Columns'=>[
..
'myTitle',
..
],
should work fine and should be active for sorting as you want
if you want a different header/label for the column, use label instead of header as header is only a cell content and cannot be used for sorting, while label can. details
[
..
'attribute'=>'myTitle',
'label' => 'Page Title'
..
],
I have an array of data very similar to this
[
'name'=>'mark',
'age'=> '21'
'height'=> '190 cm'
]
I searched Google and all the results i found were using an active record object.
How do i use the gridview with an array of this sort?
You should use ArrayDataProvider (https://github.com/yiisoft/yii2/blob/master/framework/data/ArrayDataProvider.php)
$provider = new ArrayDataProvider([
'allModels' => $yourArray,
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
I want to sort a column in TbJSONGridView by date , I have this code
$this->widget(
'bootstrap.widgets.TbJsonGridView', array(
'dataProvider' => $model->searchPending(),
'type' => 'striped bordered condensed',
'summaryText' => false,
'cacheTTL' => 10, // cache will be stored 10 seconds (see cacheTTLType)
'cacheTTLType' => 's', // type can be of seconds, minutes or hours
'enablePagination' => true,
'columns' => array(
array(
'name' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
))
but the column I get it is from a related model, and the sorting is not working, when I clicked on the column header it does nothing.
what's wrong with the code???
Gabriel
$data->carShippeds[0]->
This shows us that the current model has many cars shipped. You are only showing the first one in there. While that works, Yii has no way of knowing that you are only showing the first value so your sorting has no chance of working. Create a view that only selects your first pickup date, make a join in your criteria with the main model, show the field without [0]-> and it will work.
Change the below section:
array(
'name' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
To this:
array(
'name' => 'pickup_date',
'header' => 'Pickup Date',
'value' => '$data->carShippeds[0]->pickup_date',
'type'=>'date',
),
Then in your 'form' when you want to return DataProvider write the specific sorting code for this association like below:
return new DataProvider($query, [
'pagination' => [
'pagesize' => 20,
],
'sort' => [
'sortVar' => 'sort',
'defaultOrder' => ['t.carShippeds.pickup_date asc'],
'attributes' => [
'pickup_date' => [
'asc' => 't.carShippeds.pickup_date',
'desc' => 't.carShippeds.pickup_date DESC',
],
'*',
],
],
]);
Please notice that I did not know the join alias you have for your t.carShippeds or even whether it is t or another entity. replace it or give me more information about your DataProvider and I can help more.
i am trying to validate a user registration form in Zend Framework 2.
More specifically how to validate the email, ZF1 i could do:
$email->setValidators( array(new Zend_Validate_EmailAddress()) );
I'm wondering if i can just call something similar like this.
Also I'm wondering how to validate two fields that need to be the same like the password field and the password verification.
I guess that when i say if($form->isValid()).. this will check the getInputFilter() method for all validation.
I've been taking a look at ZfcUser module but, right now, i can't understand much since i don't have a full grasp on how ZF2 works
Any ideas, maybe a simple example?
thanks
Have you read the official tutorial to see how the new ZF2 Form component works?
At a very high level, you need a Form object and a Filter object working together. The Filter object is where you place your filters and validators. However, if you use a form element of type EmailAddress in your Form, then it will automatically add the correct validator. There is more information in the manual.
I recently did a webinar on forms for Zend which you should be able to find on this page.
i've figure it out.
the validators are multidimensional arrays and each array has a name and some options. It might be a bit wired in the beginning to notice it, but much configuration in zf2 is in this way
see an example for the password:
$inputFilter->add($factory->createInput([
'name' => 'password',
'required' => true,
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => [
[
'name' => 'StringLength',
'options' => [
'encoding' => 'UTF-8',
'min' => 6,
'max' => 128,
],
],
],
]));
$inputFilter->add($factory->createInput([
'name' => 'password_verify',
'required' => true,
'filters' => [ ['name' => 'StringTrim'], ],
'validators' => [
array(
'name' => 'StringLength',
'options' => array( 'min' => 6 ),
),
array(
'name' => 'identical',
'options' => array('token' => 'password' )
),
],
]));
note, in php 5.3 > an array could be written like array() or [], in the above example i mix them up for no particular reason.