thinkphpÄ£×ÓÔõÑùÉèÖÃ
ÔÚthinkphpÖУ¬½¨ÉèÄ£×ÓÐèÒª¼ÌÐøthink\ModelÀà¡£Ò»µ©¼ÌÐøÁËÕâ¸öÀ࣬¾ÍÄܹ»Ê¹ÓÃÆäÖÐÌṩµÄÖÖÖÖº¯ÊýÓëÊôÐÔ¶ÔÊý¾Ý¿â¾ÙÐн»»¥²Ù×÷¡£ÏÂÃ棬ÎÒÃǽ«´ÓÒÔϼ¸¸ö·½ÃæÀ´ÏÈÈÝthinkphpÄ£×ÓµÄÉèÖá£
Êý¾Ý¿âÅþÁ¬µÄÉèÖÃ
ÔÚthinkphpÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýapplicationĿ¼ÏµÄdatabase.phpÎļþ£¬ÉèÖÃÓëÊý¾Ý¿âµÄÅþÁ¬ÐÅÏ¢¡£¸ÃÎļþ°üÀ¨ÁËËùÓÐÓëÊý¾Ý¿âÏà¹ØµÄÉèÖÃÐÅÏ¢£¬ÀýÈçÖ÷»úÃû¡¢Óû§Ãû¡¢ÃÜÂë¡¢Êý¾Ý¿âÃûµÈ¡£Ïêϸ¿É²ÎÕÕÒÔÏ´úÂ룺
return [ // ĬÈÏÊý¾Ý¿âÉèÖà 'database' => [ // Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // ЧÀÍÆ÷µØµã 'hostname' => 'localhost', // Êý¾Ý¿âÃû 'database' => 'test', // Êý¾Ý¿âÓû§Ãû 'username' => 'root', // Êý¾Ý¿âÃÜÂë 'password' => '', // Êý¾Ý¿âÅþÁ¬¶Ë¿Ú 'hostport' => '', // Êý¾Ý¿âÅþÁ¬²ÎÊý 'params' => [], // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8 'charset' => 'utf8', // Êý¾Ý¿â±íǰ׺ 'prefix' => 'think_', ], ];
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔƾ֤ÏÖÕæÏàÐΣ¬ÐÞ¸ÄÒÔÉÏÉèÖÃÎļþµÄÄÚÈÝ£¬´Ó¶øʵÏÖÓëÊý¾Ý¿âµÄÅþÁ¬¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Ä£×ӵĽç˵
ÔÚthinkphpÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ý½ç˵ģ×ÓÀ´²Ù×÷Êý¾Ý¿â¡£ÎÒÃÇ¿ÉÒÔ½«Ä£×ÓÊÓΪÊý¾Ý±íµÄÓ³É䣬Ҳ¾ÍÊÇ˵£¬Ä£×ÓÖеÄÿ¸öÊôÐÔ¶¼¶ÔÓ¦ÓÚ±íÖеÄÿ¸ö×ֶΡ£
ÎÒÃÇ¿ÉÒÔͨ¹ýÒÔÏ´úÂ룬½ç˵¶ÔÓ¦µÄÄ£×Ó£º
namespace app\index\model; use think\Model; class User extends Model { // }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂë½ç˵ÁËÒ»¸öÃûΪUserµÄÄ£×Ó£¬ÊµÏÖÁ˶ÔÓ¦Êý¾Ý±íµÄ²Ù×÷¡£
Ä£×ÓÊôÐÔµÄÉèÖÃ
ÔÚthinkphpÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃÄ£×ÓÊôÐÔÀ´ÕÆ¿ØÆäÊÂÇé·½·¨¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÉèÖñíÃû¡¢Ö÷¼ü¡¢ÊÇ·ñ×Ô¶¯Ð´Èëʱ¼ä´ÁµÈµÈ¡£ÒÔÏÂÊÇһЩ³£ÓõÄÊôÐÔÉèÖÃÒªÁ죺
1) $table£ºÉèÖÃÄ£×Ó¶ÔÓ¦µÄ±íÃû¡£
protected $table = 'user';
µÇ¼ºó¸´ÖÆ
2) $pk£ºÉèÖñíµÄÖ÷¼ü¡£
protected $pk = 'id';
µÇ¼ºó¸´ÖÆ
3) $autoWriteTimestamp£ºÉèÖÃÊÇ·ñ×Ô¶¯Ð´Èëʱ¼ä´Á¡£
protected $autoWriteTimestamp = true;
µÇ¼ºó¸´ÖÆ
4) $createTime£ºÉèÖý¨Éèʱ¼ä×Ö¶ÎÃû¡£
protected $createTime = 'create_time';
µÇ¼ºó¸´ÖÆ
5) $updateTime£ºÉèÖøüÐÂʱ¼ä×Ö¶ÎÃû¡£
protected $updateTime = 'update_time';
µÇ¼ºó¸´ÖÆ
¿Éƾ֤ÏêϸӪҵÐèÇó¾ÙÐÐÉèÖá£
Ä£×Ó¹ØÁª²Ù×÷
ÔÚthinkphpÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÄ£×ÓÊôÐÔÖеĹØÁªÒªÁ죬¶Ô²î±ðÄ£×Ó¾ÙÐйØÁª²Ù×÷¡£ÒÔÏÂÊÇһЩ³£ÓõÄÄ£×Ó¹ØÁª²Ù×÷£º
1) Ò»¶ÔÒ»¹ØÁª
public function profile() { return $this->hasOne('Profile', 'user_id'); }
µÇ¼ºó¸´ÖÆ
2) Ò»¶Ô¶à¹ØÁª
public function comments() { return $this->hasMany('Comment', 'blog_id'); }
µÇ¼ºó¸´ÖÆ
3) ¶à¶Ô¶à¹ØÁª
public function roles() { return $this->belongsToMany('Role', 'user_role', 'role_id', 'user_id'); }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÖ÷ÒªÕë¶Ô¼òÆÓµÄһЩ¹ØÁª¹Øϵ£¬¿ÉÒÔƾ֤ӪҵÐèÇó¾ÙÐнøÒ»²½µÄѧϰºÍµ÷½â¡£
ÒÔÉϾÍÊÇthinkphpÄ£×ÓÔõÑùÉèÖõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡