yii2.0 - CURD操作 -杂项

  1 # insert
  2 //$row = Yii::app()->getDb()->createCommand()->insert('goods', array(  
  3 \Yii::$app->db->createCommand()->insert('pur_purchase_discount', [
  4             'pur_number' => '654321',
  5             'buyer' => 11111,
  6         ])->execute();
  7         
  8 #数组的键为数据表的字段,可以直接新增数据
  9 $status = \Yii::$app->db->createCommand()->insert('数据表名', '数组形式的数据')->execute();
 10 $return_id = \Yii::$app->db->getLastInsertID(); //返回ID
 11         
 12 # select
 13 //单表查询
 14 $row = \Yii::$app->db->createCommand()
 15             ->setSql('select * from pur_purchase_discount WHERE  pur_number="123456"')
 16             ->queryAll();
 17               ->getRawSql(); //返回sql语句  ->getSql();
 18 //            ->execute(); //返回查询数据条数
 19 //            ->queryOne(); //返回一个值
 20 //            ->queryAll(); //返回所有值
 21 
 22 //查询
 23 $sql= 'SELECT * FROM ' . $data_table . ' WHERE id=' . $id;
 24 //        $rows=\Yii::$app->db->createCommand($sql)->query();
 25 $rows=\Yii::$app->db->createCommand($sql)->queryOne();
 26 
 27 
 28 
 29 //有误??????
 30 //$goodsTypes = Yii::app()->getDb()->createCommand()  
 31 $row = \Yii::$app->db->createCommand()
 32         ->select('type_id, type_name')  
 33         ->from('goods_type')  
 34         ->where('status=:status', [  
 35             ':status' => 1  
 36         ])  
 37         ->queryAll();  
 38         
 39 //连表查询
 40 //有误???
 41 $row = \Yii::$app->db->createCommand()->from('goods g')
 42         ->select('g.good_id, g.good_name, gt.type_name, g.price, g.buy_nums, g.commit_nums, g.create_time')  
 43         ->join('goods_type gt', 'g.good_type=gt.type_id')  
 44         ->where('g.`status`=:status1 and gt.`status`=:status2', [  
 45             ':status1' => 1,  
 46             ':status2' => 2  
 47         ])  
 48         ->order('g.create_time desc')  
 49         ->queryAll();  
 50         
 51 #delete
 52 $row = \Yii::$app->db->createCommand()
 53             ->delete('pur_purchase_discount', "pur_number=:pur_number", array(
 54                 ':pur_number' => 'sss',
 55             ))->execute();
 56             
 57 //删除
 58 方法一:
 59 $status = \Yii::$app->db->createCommand()
 60     ->delete($data_table, 'id = ' . $id)
 61     ->execute();
 62 $return_id = \Yii::$app->db->getLastInsertID(); //返回ID
 63 
 64 方法二:
 65 WarehouseMin::findOne(2)->delete();
 66     
 67     
 68 # update
 69 //$row = Yii::app()->getDb()->createCommand()->update('goods', [  
 70 $row = \Yii::$app->db->createCommand()->update('pur_purchase_discount',
 71             ['buyer' => 'sss',],  //修改的数据
 72             "pur_number=:pur_number", [':pur_number' => 123456] //条件
 73         )->execute();
 74 
 75 ----------------------------------------
 76 #insert 新增数据
 77 $goods= new PurchaseDiscount();
 78         $attr=['pur_number'=>'123456','buyer'=>'www','discount_price'=>11.1];
 79         $goods->setAttributes($attr);
 80         return $goods->save(false);
 81         
 82 #update 更新数据
 83 $command = \Yii::$app->db->createCommand('UPDATE pur_purchase_discount SET buyer=123456543454 WHERE pur_number=123456');
 84 $command->execute();
 85 -------------------------
 86 #获取插入数据时的id
 87 $return_id = \Yii::$app->db->getLastInsertID();
 88 
 89 #
 90 当你在使用了$model->save();之后
 91 $model->id 就是上一次保存的id
 92 
 93 
 94 --------------------------------------
 95 //插入:待验证??
 96 $model = new AlibabaAccount();
 97 if ($model->load(Yii::$app->request->post()) && $model->save()) {
 98     return $this->redirect(['index']);
 99 } else {
100     return $this->render('create', [
101         'model' => $model,
102     ]);
103 }
104     =================== post接收到的值 ==========
105     Array
106     (
107         [_csrf] => dkhLVDVlcXkefSE1XBYVN0YsMWdgMws8FzsEJnhUPQAaPgFgQwMcFw==
108         [AlibabaAccount] => Array
109             (
110                 [account] => 12345
111                 [app_key] => 12345
112                 [secret_key] => 23456
113                 [redirect_uri] => http://www.purchase.com/alibaba-account/create
114                 [bind_account] => Array
115                     (
116                         [0] => 265
117                     )
118 
119                 [status] => 1
120             )
121 
122     )
123     ==========================================================
124 
125 //判断数据是否存在,存在就修改
126 方法一:
127 $model = WarehouseMin::findOne(3);
128 $model->setAttribute('warehouse_code','shd');
129 $a = $model->save();
130 
131 方法二:
132 //修改
133 public function actionUpdate($id)
134 {
135     $model = $this->findModel($id);
136 
137     if ($model->load(Yii::$app->request->post()) && $model->save()) {
138         return $this->redirect(['view', 'id' => $model->id]);
139     } else {
140         return $this->render('update', [
141             'model' => $model,
142         ]);
143     }
144 }
145 //删除
146 public function actionDelete($id)
147 {
148     $this->findModel($id)->delete();
149 
150     return $this->redirect(['index']);
151 }
152 protected function findModel($id)
153 {
154     if (($model = BasicTactics::findOne($id)) !== null) {
155         return $model;
156     } else {
157         throw new NotFoundHttpException('The requested page does not exist.');
158     }
159 }
160 
161 #方法三:修改
162 WarehouseMin::updateAll(['warehouse_code'=>'修改的字段'],['id'=>3]);
163 
164 方法四:
165 $model = WarehouseMin::findOne(['warehouse_code' => 11]);
166 $model->is_handler    = $data['is_handler']; // 是否处理
167 $model->handler_type  = $data['handler_type'];   // 处理类型
168 $result = $model->save();
169 
170 #查询
171 方法一:返回查询的字段的内容
172 $map['id']     = 3;
173 $a    = WarehouseMin::find()
174     ->select('warehouse_code',)
175     ->where($map)
176     ->asArray()
177     ->scalar();
178     
179 方法二:返回数组和对象
180 $a    = WarehouseMin::find()->all();
181 
182 方法三:判断数据是否找到
183 $model = PurchaseWarehouseAbnormal::findOne(['defective_id' => $defective_id]);
184 if(!$model) {
185     return '没有查到数据';
186 }
187 方法四:
188 $rows = PurchaseWarehouseAbnormal::find()
189     ->select(['defective_id', 'handler_type'])
190     ->where(['is_handler' => 1, 'is_push_to_warehouse' => 0])
191     ->limit(20)
192     ->asArray()
193     ->all();
194 
195 -----------------------------
196 #数组和对象之间的转换
197 #方法一:
198 //定义函数
199 function array2object($array) {
200   if (is_array($array)) {
201     $obj = new StdClass();
202     foreach ($array as $key => $val){
203       $obj->$key = $val;
204     }
205   }
206   else { $obj = $array; }
207   return $obj;
208 }
209 function object2array($object) {
210   if (is_object($object)) {
211     foreach ($object as $key => $value) {
212       $array[$key] = $value;
213     }
214   }
215   else {
216     $array = $object;
217   }
218   return $array;
219 }
220 //用法示例如下:
221 $array = array('foo' => 'bar','one' => 'two','three' => 'four');
222 $obj = array2object($array);
223 print $obj->one; // output's "two"
224 $arr = object2array($obj);
225 print $arr['foo']; // output's bar
226 #方法二:
227 //调用这个函数,将其幻化为数组,然后取出对应值
228 function object_array($array)
229 {
230    if(is_object($array))
231    {
232     $array = (array)$array;
233    }
234    if(is_array($array))
235    {
236     foreach($array as $key=>$value)
237     {
238      $array[$key] = object_array($value);
239     }
240    }
241    return $array;
242 }
243 ---------------------------
244 PHP从数组中删除元素的四种方法实例
245 #方法一:使用 unset 函数
246 //删除一个元素,且保持原有索引不变
247 $array = array(0 => "a", 1 => "b", 2 => "c");
248 unset($array[1]);
249 //使用 unset 并未改变数组的原有索引。如果打算重排索引(让索引从0开始,并且连续),可以使用 array_values 函数:
250 $array = array_values($array);
251 
252 #方法二:使用 array_splice 函数
253 #删除一个元素,不保持索引
254 $array = array(0 => "a", 1 => "b", 2 => "c");
255 array_splice($array, 1, 1); //↑ 你想删除的元素的Offset
256 
257 #方法三:使用 array_diff 函数
258 //按值删除多个元素,保持索引(与 unset 类似,array_diff 也将保持索引。)
259 $array = array(0 => "a", 1 => "b", 2 => "c");
260 $array = array_diff($array, ["a", "c"]); //你想删除的数组元素值values
261 
262 #方法四:使用 array_diff_key 函数
263 //按键删除多个元素,保持索引(与 unset 类似,array_diff_key 也将保持索引。)
264 $array = array(0 => "a", 1 => "b", 2 => "c");
265 $array = array_diff_key($array, [0 => "xy", "2" => "xy"]);//你想删除的数组键keys
266 ----------------------------
267 获取变量类型:gettype($a);

 

posted @ 2018-04-12 14:12  liuweipcs  阅读(289)  评论(0编辑  收藏  举报