Home > Backend Development > PHP Tutorial > Yii framework AR object data is converted into an array, yiiar_PHP tutorial

Yii framework AR object data is converted into an array, yiiar_PHP tutorial

WBOY
Release: 2016-07-13 10:19:13
Original
865 people have browsed it

Yii framework AR object data is converted into an array, yiiar

demo function function: Convert AR object data into an array

Limitations: Only used for multi-dimensional arrays of findAll. The one-dimensional array of find can be converted into an element of a multi-dimensional array before use

<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> }
Copy after login

(Urgent) Convert the result array of yii SQL query into AR mode

In addition to cactivedataprovider, you can also use CArrayDataProvider
I will give you an example:
In controller:
$rawdata = Yii::app()->db->createCommand('select * from {{post}} where id<100')->queryAll();

$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));

Then in the view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'somegrid' ,
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'title',
'content',
),
));
That’s ok~

(Urgent) Convert the result array of yii SQL query into AR mode

Just answered. For details, see:
zhidao.baidu.com/question/514860097.html?oldq=1

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/878336.htmlTechArticleYii framework AR object data is converted into an array, yiiar demo function function: Convert AR object data into an array Limitation: only Multi-dimensional array used for findAll, find one-dimensional array can be converted to multi-dimensional first...
Related labels:
yii
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template