demo函数作用:将AR对象数据转化为数组
局限:仅用于findAll的多维数组,find一维数组可以先转化为多维数组的一个元素在使用
<span> 1</span> <span>function</span><span> actionIndex() </span><span> 2</span> <span>{ </span><span> 3</span> <span> 4</span> <span>$data</span> = Tableone::model()->findAll(<span>array</span><span>( </span><span> 5</span> "limit"=>"10", <span> 6</span> <span> )); </span><span> 7</span> <span> 8</span> <span>/*</span><span>* </span><span> 9</span> <span> * ar对象转化为数组 </span><span>10</span> <span> * 仅用于findAll </span><span>11</span> <span>*/</span> <span>12</span> <span>$return</span> = <span>array_map</span>(<span>function</span>(<span>$record</span><span>){ </span><span>13</span> <span>$return</span> = <span>$schma</span> = <span>array</span><span>(); </span><span>14</span> <span>if</span>(<span>isset</span>(<span>$record</span>['tableSchema']-><span>columns)) </span><span>15</span> <span> { </span><span>16</span> <span>foreach</span>(<span>$record</span>['tableSchema']->columns <span>as</span> <span>$k</span>=><span>$v</span><span>) </span><span>17</span> <span> { </span><span>18</span> <span>$schma</span>[] = <span>$k</span><span>; </span><span>19</span> <span> } </span><span>20</span> <span> } </span><span>21</span> <span>if</span>(<span>count</span>(<span>$schma</span>)>0<span>) </span><span>22</span> <span> { </span><span>23</span> <span>foreach</span>(<span>$schma</span> <span>as</span> <span>$k</span>=><span>$v</span><span>) </span><span>24</span> <span> { </span><span>25</span> <span>$return</span>[<span>$v</span>] = <span>$record</span>->attributes[<span>$v</span><span>]; </span><span>26</span> <span> } </span><span>27</span> <span> } </span><span>28</span> <span>return</span> <span>$return</span><span>; </span><span>29</span> },<span>$data</span><span>); </span><span>30</span> <span>31</span> <span>print_r</span>(<span>$return</span><span>); </span><span>32</span> <span>33</span> <span>34</span> }
除了cactivedataprovider,你还可以使用CArrayDataProvider
我给你写个例子:
controller中:
$rawdata = Yii::app()->db->createCommand('select * from {{post}} where idqueryAll();
$data = new CArrayDataProvider($rawdata, array(
'id' => 'user',
'sort' => array(
'attributes' => array(
'id', 'title', 'content'
),
),
'pagination' => array(
'pageSize' => 10,
),
));
$this->render('gridview_array', array('dataProvider' => $data));
然后view中:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'somegrid',
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'title',
'content',
),
));
这样就ok 了~
刚回答过。详见:
zhidao.baidu.com/question/514860097.html?oldq=1