>php教程 >php手册 >Yii框架AR对象数据转化为数组,yiiar

Yii框架AR对象数据转化为数组,yiiar

WBOY
WBOY원래의
2016-06-13 09:25:362193검색

Yii框架AR对象数据转化为数组,yiiar

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> }

 

(急)yii SQL查询出的结果数组转换成AR模式

除了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 了~
 

(急)yii SQL查询出的结果数组转换成AR模式

刚回答过。详见:
zhidao.baidu.com/question/514860097.html?oldq=1
 

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.