yii ar add, delete, modify and check operation test record, yiiar_PHP tutorial

WBOY
Release: 2016-07-13 10:09:46
Original
1183 people have browsed it

yii ar addition, deletion, modification and check operation test record, yiiar

Dear friends, I am a yii novice, don’t laugh at me. During the day today, I wrote a management module that involved 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>     }
Copy after login

I hope the ladies will be merciful

I am still a novice

I hope you can point out any deficiencies, thank you!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/942277.htmlTechArticleyii ar Add, delete, modify and check operation test records, yiiar Dear friends, I am a yii novice, don’t laugh at me. Writing a management module during the day today involves the curd operation under yii ar and the update operation...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!