首頁  >  文章  >  後端開發  >  yii ar 增刪改查 操作測試記錄

yii ar 增刪改查 操作測試記錄

WBOY
WBOY原創
2016-08-08 09:31:15937瀏覽

親們, 我是yii小白 不要笑我奧。今天白天寫一個管理模組涉及到 yii ar 下的  curd 操作,做 update 操作時糾結了好久,今天晚上花點時間學習, 下面寫下我的測試記錄

程式碼如下:

<span> 1</span>     <span>public</span> <span>function</span><span> actionIndex(){
</span><span> 2</span> 
<span> 3</span>         <span>//</span><span> 写入数据
</span><span> 4</span> <span>        //yii  ar  curd  之   insert</span>
<span> 5</span>         <span>$_POST</span>['Users']['sex'] = 2<span>;
</span><span> 6</span>         <span>$_POST</span>['Users']['username'] = 'xiaohua'<span>;
</span><span> 7</span>         <span>$model</span> = <span>new</span><span> Users;
</span><span> 8</span>         <span>$model</span>->attributes = <span>$_POST</span>['Users'<span>];
</span><span> 9</span>         <span>$rt</span> = <span>$model</span>-><span>save();
</span><span>10</span> 
<span>11</span>         <span>if</span>(<span>$rt</span><span>) {
</span><span>12</span>             Yii::app()->user->setFlash('success','写入成功了'<span>);
</span><span>13</span>             <span>$this</span>->redirect(<span>array</span>('index/test'<span>));
</span><span>14</span>         } <span>else</span><span> {
</span><span>15</span>             Yii::app()->user->setFlash('error','失败了'<span>);
</span><span>16</span>             <span>$this</span>->redirect(<span>array</span>('index/test'<span>));
</span><span>17</span> <span>        }
</span><span>18</span> 
<span>19</span>     
<span>20</span>         <span>//</span><span> 查询数据
</span><span>21</span> <span>        //get one data by field</span>
<span>22</span>         <span>$hh</span> = Users::model()-><span>find(
</span><span>23</span>               'username=:name',<span>array</span>(':name'=>'fzb'<span>)
</span><span>24</span> <span>            );
</span><span>25</span>         <span>print_r</span>(<span>$hh</span>['username'<span>]);
</span><span>26</span> 
<span>27</span>         <span>//</span><span>get one data by id</span>
<span>28</span>         <span>$hh</span> = Users::model()->findByPk(1<span>);
</span><span>29</span>         <span>print_r</span>(<span>$hh</span>['username'<span>]);
</span><span>30</span> 
<span>31</span>         <span>//</span><span>get all</span>
<span>32</span>         <span>$hh</span> = Users::model()-><span>findAll();
</span><span>33</span>         <span>print_r</span>(<span>$hh</span><span>);
</span><span>34</span> 
<span>35</span>         <span>//</span><span>  修改数据  
</span><span>36</span> <span>        // update  one</span>
<span>37</span>         <span>$model</span> = <span>new</span><span> Users;
</span><span>38</span>         <span>$count</span> = <span>$model</span>->updateByPk(5,<span>array</span>('username'=>'admin','sex'=>1<span>));
</span><span>39</span>         <span>if</span>(<span>$count</span><span>) {
</span><span>40</span>             Yii::app()->user->setFlash('success','修改成功了'<span>);
</span><span>41</span>             <span>$this</span>->redirect(<span>array</span>('index/test'<span>));
</span><span>42</span>         } <span>else</span><span> {
</span><span>43</span>             Yii::app()->user->setFlash('error','修改失败了'<span>);
</span><span>44</span>             <span>$this</span>->redirect(<span>array</span>('index/test'<span>));
</span><span>45</span> <span>        }
</span><span>46</span> 
<span>47</span> 
<span>48</span>     }

希望大嬸們嘴下留情

我還是小白呢

有不足之處希望大家指出,謝謝!

以上就介紹了yii ar 增刪改查 操作測試記錄,包含了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn