친애하는 친구 여러분, 저를 비웃지 마세요. 오늘 낮에는 Yii AR에서 커드 작업에 관련된 관리 모듈을 작성했습니다. 오늘 밤에는 업데이트 작업에 시간을 투자하여 아래에 테스트 기록을 적어보았습니다.
코드는 다음과 같습니다.
<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 튜토리얼에 관심이 있는 친구들에게 도움이 되길 바랍니다.