Heim >php教程 >php手册 >yii ar 增删改查 操作测试记录,yiiar

yii ar 增删改查 操作测试记录,yiiar

WBOY
WBOYOriginal
2016-06-13 09:17:031028Durchsuche

yii ar 增删改查 操作测试记录,yiiar

亲们, 我是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>     }

希望大婶们嘴下留情

我还是小白呢

有不足之处希望大家指出,谢谢!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn