thinkphp5Ä£×ÓÔõÑùʹÓÃ
Ëæ×Å»¥ÁªÍøµÄÉú³¤£¬ÖÖÖÖÓ¦ÓóÌÐòÒ»Ö±Ó¿ÏÖ²¢Ò»Ö±Éý¼¶¡£ÔÚÓ¦ÓóÌÐò¿ª·¢ÖУ¬±Ø²»¿ÉÉÙµÄÊÇÒ»¸öÓÅÒìµÄ¿ò¼Ü£¬Ëü¿ÉÒÔΪ³ÌÐòÔ±Ìṩ¿ìËÙ¿ª·¢¡¢¸ßЧÖÎÀíÓ¦ÓóÌÐòµÄÖ§³Ö¡£thinkphp5ÊÇÒ»¸öºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ËüµÄ¿ª·¢ËÙÂʺܿ졢ÐÔÄÜÓÅÒì¡¢¹¦Ð§Ç¿Ê¢£¬±»¿í´ó³ÌÐòÔ±Ëùϲ»¶¡£
ÔÚThinkPHP5¿ò¼ÜÖУ¬Ä£×ÓÊÇÒ»¸öºÜÊÇÖ÷ÒªµÄ¿´·¨£¬ÓÉÓÚËü¹ØÓÚ³ÌÐòÔ±À´ËµÌṩÁ˺ÜÊÇÖ÷ÒªµÄÖ§³Ö¡£ÏÂÃæÎÒÃÇÀ´ÏêϸÏÈÈÝÒ»ÏÂThinkPHP5Ä£×ÓµÄʹÓÃÒªÁì¡£
½¨ÉèÄ£×Ó
ÔÚThinkPHP5¿ò¼ÜÖУ¬Ä£×ӵĽ¨ÉèºÜÊǼòÆÓ¡£ÎÒÃÇÖ»ÐèÒªÔÚmodelĿ¼Ï½¨ÉèÒ»¸öphpÎļþ£¬È»ºóÔÚÕâ¸öÎļþÖнç˵¶ÔÓ¦µÄÄ£×Ó¼´¿É¡£Í¨³£ÇéÐÎÏ£¬ÎÒÃÇ»áÒÔÊý¾Ý±íΪµ¥Î»À´½¨ÉèÄ£×Ó£¬ÕâÑù¼ÈÀû±ãÖÎÀí£¬Ò²Ô½·¢Ö±¹Û¡£
Ä£×ӵĽ¨ÉèºÜÊǼòÆÓ£¬Æä´úÂëÄ£°åÈçÏÂ:
<?php namespace appmodel; use thinkModel; class UserModel extends Model { }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬Ê×ÏÈÎÒÃÇÐèÒªÖ¸Ã÷Ä£×ÓËùÔÚµÄÃüÃû¿Õ¼ä£¬Ò²¾ÍÊÇnamespaceÓï¾ä¡£ÕâÀïÎÒÃǽç˵ģ×ÓÔÚapp/modelÏ¡£È»ºóÎÒÃÇÐèÒª¼ÌÐøthinkModel»ùÀ࣬ÕâÊÇÓÉÓÚThinkPHP5Ä£×Ó»ùÓÚÕâ¸öÀà¾ÙÐпª·¢µÄ¡£×îºóÎÒÃǽç˵ÏêϸµÄÄ£×ÓÀàÃû£¬²¢ÔÚÕâ¸öÀàÖоÙÐÐÏà¹ØµÄÊý¾Ý²Ù×÷¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Êý¾Ý¿â²Ù×÷
ÔÚThinkPHP5¿ò¼ÜÖУ¬Æ¾Ö¤Êý¾Ý¿âÉè¼ÆµÄ²î±ð£¬ÎÒÃÇÐèÒª½ÓÄɲî±ðµÄ²Ù×÷·½·¨¡£ÏÂÃæÎÒÃÇÀ´ÏÈÈݼ¸ÖÖ³£¼ûµÄÊý¾Ý¿â²Ù×÷·½·¨¡£
¢Ù. ʹÓþ«Á·ÅÌÎÊ
ThinkPHP5ÌṩÁËÒ»ÖÖºÜÊǾ«Á·µÄÅÌÎÊ·½·¨£¬Ëü¿ÉÒÔÈÃÎÒÃÇ¿ìËÙµÄÅÌÎÊÊý¾Ý¿âÖеÄÊý¾Ý¡£Ïêϸ´úÂëÈçÏÂ:
// ÅÌÎÊËùÓÐÓû§ÐÅÏ¢ $userList = UserModel::select(); // Ìõ¼þÅÌÎÊ $userInfo = UserModel::where('user_id',1)->find();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýUserModelÄ£×ÓÀàµÄselect()ÒªÁìÅÌÎÊËùÓÐÓû§ÐÅÏ¢¡£ÎÒÃÇ»¹¿ÉÒÔͨ¹ýwhere()ÒªÁìÖ¸¶¨ÅÌÎÊÌõ¼þ£¬»ñµÃÏëÒªµÄЧ¹ûÜöÝÍ¡£
¢Ú. ʹÓÃÁ´Ê½ÅÌÎÊ
Á´Ê½ÅÌÎÊÊÇÒ»ÖÖºÜÊÇÊ¢ÐеÄÅÌÎÊ·½·¨£¬Ëü¿ÉÒÔÈÃÎÒÃÇͨ¹ýһϵÁÐÒ»Á¬µÄ²Ù×÷À´ÊµÏÖÅÌÎÊ¡£ÏêϸµÄ´úÂëÈçÏÂ:
// ¶ÔÊý¾Ý¼¯ÅÅÐò²Ù×÷ $userList = UserModel::order('user_id desc')->limit(5)->select(); // ×Ô½ç˵ÅÌÎÊÓï¾ä $userInfo = UserModel::where('user_id',1)->field('user_id,user_name')->find();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃorder()ÒªÁì¶ÔÅÌÎʵÄЧ¹ûÜöÝ;ÙÐÐÅÅÐò¡£ÎÒÃÇ»¹¿ÉÒÔͨ¹ýlimit()ÒªÁìÀ´É趨ÅÌÎÊЧ¹ûÜöÝ͵ÄÊýÄ¿¡£Óë´Ëͬʱ£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃfield()ÒªÁìÀ´ÉèÖÃÒªÅÌÎʵÄ×Ö¶ÎÁÐ±í£¬ÕâÑù¿ÉÒÔÔ½·¢ÎÞаµÄ¶¨ÖÆÊý¾ÝÅÌÎÊ¡£
¢Û. ʹÓÃÔÉúSQLÓï¾ä
ÔÚһЩÌØÊâµÄÇéÐÎÏ£¬ÎÒÃÇ¿ÉÄÜÐèҪʹÓÃÔÉúSQLÓï¾äÀ´¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£Õâʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃquery()ÒªÁì¡£ÏêϸµÄ´úÂëÈçÏÂ:
// Ö´ÐÐSQLÅÌÎÊ $userList = UserModel::query('select * from user limit 5'); // Ö´ÐÐSQLÓï¾ä $result = UserModel::execute('update user set user_name='²âÊÔ' where user_id=1');
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇʹÓÃquery()ÒªÁìÀ´Ö´ÐÐSQLÅÌÎʲÙ×÷£¬»ñµÃЧ¹ûÜöÝÍ¡£Óë´Ëͬʱ£¬ÎÒÃÇ»¹¿ÉÒÔʹÓÃexecute()ÒªÁìÀ´Ö´ÐÐSQLÓï¾ä²Ù×÷¡£
Á¬±íÅÌÎÊ
ÔÚThinkPHP5¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýʵÏÖ¶à¸öÊý¾ÝÄ£×ӵĹØÁª£¬À´ÊµÏÖÁ¬±íÅÌÎÊ¡£ÔÚÏÖʵÏîÄ¿¿ª·¢Àú³ÌÖУ¬ÕâÖÖÒªÁìºÜÊÇÊÊÓá£
ÏêϸµÄ´úÂëÈçÏÂ:
/** * Óû§Ä£×Ó */ class UserModel extends Model { // ½ç˵һ¶Ô¶à¹ØÁª£ºÒ»¸öÓû§¿ÉÒÔ¹Ø×¢¶à¸ö±êÇ© public function labels() { return $this->hasMany('appmodelUserLabelModel','user_id','user_id'); } } /** * Óû§±êÇ©Ä£×Ó */ class UserLabelModel extends Model { // ½ç˵һ¶Ô¶à¹ØÁª£ºÒ»¸ö±êÇ©Ö»±»Ò»¸öÓû§¹Ø×¢ public function user() { return $this->belongsTo('appmodelUserModel','user_id','user_id'); } } /** * Á¬±íÅÌÎÊ */ $userList = UserModel::with(['labels'])->select();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÊö´úÂëÖУ¬ÎÒÃÇͨ¹ýlabels()ÒªÁìÔÚUserModelºÍUserLabelModelÖ®¼ä½¨ÉèÁËÒ»¶Ô¶à¹ØÁªºó£¬¾Í¿ÉÒÔʹÓÃwith([‘labels’])ÒªÁìÀ´ÊµÏÖÁ¬±íÅÌÎÊÁË¡£ÔÚÏÖʵ¿ª·¢ÖУ¬ÈôÊÇÐèÒª»ñÈ¡¸ü¶àµÄ¹ØÁªÊý¾Ý£¬Ò²¿ÉÒÔÔÚwith()ÒªÁì²ÎÊýÖÐÌí¼Ó¸ü¶àµÄ¹ØÁªÄ£×Ó£¬ÊµÏÖÁª±íÅÌÎʹ¦Ð§¡£
×ܽá
ÒÔÉϾÍÊǹØÓÚThinkPHP5Ä£×ÓµÄʹÓÃÒªÁìµÄÏêϸÏÈÈÝ¡£ÎÒÃÇ¿ÉÒÔ·¢Ã÷£¬ThinkPHP5Ä£×ÓºÜÊǼòÆÓ¡¢ÊÊÓᢸßЧ£¬ÊÇÎÒÃÇÔÚÏÖʵÏîÄ¿¿ª·¢Öбز»¿ÉÉÙµÄÒ»¸öÀûÆ÷¡£ÔÚÈÕ³£¿ª·¢ÖУ¬ºÏÀíµÄʹÓÃThinkPHP5Ä£×Ó£¬Äܹ»Ìá¸ßÎÒÃǵĿª·¢Ð§ÂʺʹúÂëµÄÖÊÁ¿£¬ÈÃÎÒÃǵÄÓ¦ÓóÌÐòÔ½·¢ÖÇÄÜ¡¢Ô½·¢Ç¿Ê¢¡£
ÒÔÉϾÍÊÇthinkphp5Ä£×ÓÔõÑùʹÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡