Home >Backend Development >PHP Tutorial >yii ar addition, deletion, modification and query operation test record

yii ar addition, deletion, modification and query operation test record

WBOY
WBOYOriginal
2016-08-08 09:31:15957browse

Dear friends, I am a yii noob. Don’t laugh at me. Writing a management module during the day today involves the curd operation under Yii AR. I struggled with the update operation for a long time. I spent some time studying tonight and wrote down my test records below

The code is as follows:

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

I hope the ladies will be merciful

I’m still a novice

If there are any shortcomings, I hope you can point them out, thank you!

The above introduces the yii ar addition, deletion, modification and check operation test records, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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