[英]Yii 2 - Class 'CArrayDataProvider' not found
I'm new at yii and trying to find my way around. 我是yii的新手,正设法寻找自己的出路。 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. 我创建了一个执行简单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. 似乎CArrayDataProvider是我需要的。 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? 看来我没有安装CArrayDataProvider? 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: 我确实找到了ArrayDataProvider,所以尝试了同样的结果:
use vendor\yiisoft\yii2\data\ArrayDataProvider;
The error is: 错误是:
PHP Fatal Error – yii\base\ErrorException
Class 'CArrayDataProvider' not found
This is on line 24: 这是第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: 在文件系统(linux)本身上,我进行了更新:
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 我用过: https : //www.tutorialspoint.com/yii/yii_extensions.htm
So in short, the static page that I call, now displays a DateTimePicker. 简而言之,我调用的静态页面现在显示一个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. 我如何获得yii2以允许我使用ArrayDataProvider。 Or how do I install the extension? 或如何安装扩展程序? Or who do I reference it? 还是我参考谁?
In Yii2 there's not a CArrayDataProvider
. 在Yii2中,没有CArrayDataProvider
。 Use ArrayDataProvider
, like described in docs: 使用ArrayDataProvider
,如文档中所述:
$provider = new yii\data\ArrayDataProvider([
'allModels' => $query->from('post')->all(),
'sort' => [
'attributes' => ['id', 'username', 'email'],
],
'pagination' => [
'pageSize' => 10,
],
]);
Yii2 supports ArrayDataProvider
and yii 1.* supports CArrayDataProvider, so as per your code you are using Yii2, so just replace the below line with Yii2支持ArrayDataProvider
而ArrayDataProvider
1. *支持CArrayDataProvider,因此根据您的代码,您正在使用Yii2,因此只需将以下行替换为
$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',
]
]
]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.