ʵÀý½â˵ThinkPHPÖеÄÔöɾ¸Ä²é²Ù×÷
thinkphpÊÇÒ»¿îÓÅÒìµÄphp¿ò¼Ü£¬ËüµÄ½¹µãÌصãÊÇÇáÁ¿¿ìËÙ£¬ÒÔ¼°Ç¿Ê¢µÄ¼ò»¯±à³Ì£¬Ìá¸ß¿ª·¢Ð§ÂÊ¡£ÆäÖУ¬×îΪÖ÷ÒªµÄËĸö²Ù×÷ÊÇÔö¡¢É¾¡¢¸Ä¡¢²é£¬Ò²¾ÍÊÇ crud£¬½ÓÏÂÀ´½«ÍŽáÏÖʵ¿ª·¢°¸ÀýÀ´ÏÈÈÝthinkphpÖеÄÔöɾ¸Ä²é²Ù×÷¡£
Ò»¡¢ÔöÌí
Êý¾ÝµÄÐÂÔöÊÇÎÒÃÇÔÚThinkPHPÖÐʹÓÃ×îƵÈԵIJÙ×÷Ö®Ò»¡£ÔÚTPÖУ¬ÐÂÔöÊý¾Ý¿ÉÒÔʹÓÃTPµÄmodelÀà¿âÖеÄÒ»¸öÒªÁ죬ͨ¹ýʵÀý»¯Ä£×ÓÀàÀ´¾ÙÐÐÊý¾Ý²åÈë¡£ÒÔÏÂÊÇÒ»¸öͨÀýÒªÁ죺
$data = [ 'name' => 'ÕÅÈý', 'age' => '18', 'gender' => 'ÄÐ' ]; $model = new UserModel; $res = $model->save($data);
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂëµÄ¼ÄÒåÊÇÏò¡°User¡±±íÖÐдÈëÒ»ÌõÊý¾Ý£¬Êý¾ÝΪnameΪ¡®ÕÅÈý¡¯£¬ageΪ¡®18¡¯£¬genderΪ¡®ÄС¯¡£ÆäÖУ¬UserModelÊÇÎÒÃÇÊÂÏȽ¨ÉèµÄÄ£×ÓÎļþ£¬Ëü¼ÌÐøÁËThinkPHPµÄModelÀ࣬Ȼºó¾ÙÐÐÁËÏà¹ØÉèÖúͽç˵¡£saveÒªÌå»á·µ»Ø²¼¶ûÖµ£¬´ú±íÊÇ·ñдÈëÀֳɡ£
ÔÚÏÖʵ¿ª·¢ÖУ¬ÎÒÃǾ³£»áÓöµ½Ò»´ÎÐÔ²åÈë¶àÌõÊý¾ÝµÄÇéÐΡ£ÔÚTPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃTPÌṩµÄÅúÁ¿²åÈëµÄÒªÁ죬Ïêϸ´úÂëÈçÏ£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
$data = [ [ 'name' => 'ÕÅÈý', 'age' => '18', 'gender' => 'ÄÐ' ], [ 'name' => 'ÀîËÄ', 'age' => '22', 'gender' => 'ÄÐ' ], [ 'name' => 'Lucy', 'age' => '20', 'gender' => 'Å®' ] ]; $model = new UserModel; $res = $model->saveAll($data);
µÇ¼ºó¸´ÖÆ
¶þ¡¢É¾³ý
Êý¾ÝµÄɾ³ýÊÇÎÒÃÇÔÚºǫ́ÖÎÀíϵͳÖо³£Óöµ½µÄ²Ù×÷Ö®Ò»¡£ÔÚTPÖУ¬É¾³ýÊý¾ÝÒ²ÊÇͨ¹ýÄ£×ÓÀàÀ´ÊµÏֵġ£ÎÒÃÇ¿ÉÒÔʹÓÃdeleteÒªÁìÀ´É¾³ýÒ»Ìõ»òÕ߶àÌõÊý¾Ý¡£deleteÒªÁìµÄʹÓ÷½·¨¿ÉÒÔÊÇÖ±½Óͨ¹ýÖ÷¼üÀ´²Ù×÷£¬Ò²¿ÉÒÔÊÇʹÓÃÌõ¼þ£¨Ò²¾ÍÊÇwhere£©À´É¸Ñ¡Êý¾Ý¡£
// ɾ³ýÒ»ÌõÊý¾Ý $model = new UserModel; $res = $model->where(['id' => 1])->delete(); // ɾ³ý¶àÌõÊý¾Ý $model = new UserModel; $ids = '1,2,3'; $res = $model->where(['id' => ['in', $ids]])->delete();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëµÄ¼ÄÒåÊÇ´ÓUser±íÖÐɾ³ýidΪ1µÄÊý¾Ý£¬»òÕßɾ³ýidΪ1¡¢2¡¢3µÄÊý¾Ý¡£
Èý¡¢ÐÞ¸Ä
Êý¾ÝµÄÐÞ¸ÄÊÇÎÒÃǾÙÐÐÓªÒµÂß¼´¦Àíʱ¾³£Óõ½µÄ²Ù×÷¡£TPÌṩÁËupdateÒªÁìÀ´ÊµÏÖÊý¾ÝµÄÐ޸ġ£updateÒªÁìÒ²¿ÉÒÔÖ±½Óͨ¹ýÖ÷¼üÀ´²Ù×÷£¬Ò²¿ÉÒÔʹÓÃÌõ¼þÀ´É¸Ñ¡Êý¾Ý¡£
// ÐÞ¸ÄÒ»ÌõÊý¾Ý $model = new UserModel; $data = [ 'name' => 'ÕÅÈý', 'age' => '20', 'gender' => 'ÄÐ' ]; $res = $model->where(['id' => 1])->update($data); // Ð޸ĶàÌõÊý¾Ý $model = new UserModel; $data = [ 'gender' => 'Å®' ]; $ids = '2,3,4'; $res = $model->where(['id' => ['in', $ids]])->update($data);
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÊǽ«User±íÖÐidΪ1µÄÊý¾ÝµÄname¸ÄΪ¡®ÕÅÈý¡¯£¬age¸ÄΪ¡®20¡¯£¬gender¸ÄΪ¡®ÄС¯¡£´Ëºó¶Î´úÂëµÄ¼ÄÒåÊǽ«idΪ2¡¢3¡¢4µÄÊý¾ÝµÄgender¸ÄΪ¡®Å®¡¯¡£
ËÄ¡¢ÅÌÎÊ
Êý¾ÝµÄÅÌÎÊÊÇÎÒÃÇ×î³£ÓõIJÙ×÷Ö®Ò»¡£ÔÚTPÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃmodelÀïµÄselectÒªÁì¡¢findÒªÁì¡¢getFieldÒªÁìµÈÒªÁì¾ÙÐÐÊý¾ÝµÄÅÌÎÊ¡£³£ÓõÄÅÌÎÊ·½·¨ÈçÏ£º
// ÅÌÎÊËùÓÐÊý¾Ý $model = new UserModel; $res = $model->select(); // ÅÌÎÊÒ»ÌõÊý¾Ý $model = new UserModel; $res = $model->where(['id' => 1])->find(); //ÅÌÎÊÖ¸¶¨×ֶΠ$model = new UserModel; $res = $model->getField('id,name,age');
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëµÄ¼ÄÒåÊÇÅÌÎÊUser±íÖеÄËùÓÐÊý¾Ý£¬»òÕßÅÌÎÊidΪ1µÄÊý¾Ý£¬»òÕßÅÌÎÊid¡¢nameºÍage×ֶΡ£ÕâÀïÐèҪעÖصÄÊÇ£¬ÔÚʹÓÃgetFieldÒªÁìʱ£¬·µ»ØµÄЧ¹ûÊÇÒ»¸öÒÔidΪkey£¬name¡¢ageΪvalueµÄÊý×é¡£ÈôÒªÐÞ¸Äkey»òÐÞ¸ÄÒÔÆäËû×Ö¶ÎΪvalue£¬ÐèҪͨ¹ýtpµÄÊý×麯Êý¾ÙÐмӹ¤´¦Àí¡£
×ܽ᣺
×ÛÉÏËùÊö£¬CRUDÔÚTPÖÐÊÇÒ»¸ö·Ç¾³£¼ûµÄ²Ù×÷¡£ÕÆÎÕÕâËĸö²Ù×÷¿ÉÒÔʹÎÒÃÇÔ½·¢Àû±ã¿ì½ÝµØ¾ÙÐкǫ́ӪҵÂß¼´¦Àí¡£ËäÈ»£¬TPÕë¶ÔÕ⼸¸ö²Ù×÷ÉÐÓÐÆäËû¸ü¶àµÄÒªÁ죬ϣÍû¸÷ÈËÔÚѧϰÀú³ÌÖÐÄܹ»½øÒ»²½Ì½ÌÖ£¬²¢ÉîÈëÃ÷È·Æäµ×²ãÔÀí¡£ÊÂʵ£¬ÕÆÎÕºÃÔöɾ¸Ä²é£¬²ÅÊÇÕæµÄÄܹ»°ÑTPÔËÓõ½µ½¼«ÖµÄÒªº¦¡£
ÒÔÉϾÍÊÇʵÀý½â˵ThinkPHPÖеÄÔöɾ¸Ä²é²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡