thinkphp¿ò¼ÜÔõôÉèÖöà¸ö×Ö¶Î
thinkphp¿ò¼ÜÊÇÒ»¸öÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ËüÌṩÁËÇ¿Ê¢µÄ¹¦Ð§ºÍÒ×ÓÚʹÓõĽӿڣ¬×ÊÖú¿ª·¢Ö°Ô±¿ìËÙ¹¹½¨¸ßÖÊÁ¿µÄwebÓ¦ÓóÌÐò¡£ÔÚÓ¦ÓóÌÐò¿ª·¢ÖУ¬¾³£ÐèÒªÉèÖöà¸ö×Ö¶ÎÒÔÖª×ã²î±ðµÄÐèÇó¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùÔÚthinkphp¿ò¼ÜÖÐÉèÖöà¸ö×ֶΡ£
1.ÔÚÄ£×ÓÖÐÉèÖöà¸ö×Ö¶Î
ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÖÐÉèÖöà¸ö×ֶΣ¬ÒÔ±ãÔÚÊý¾Ý¿âÖд洢²î±ðÀàÐ͵ÄÊý¾Ý¡£¿ÉÒÔͨ¹ýÉèÖÃ$pkÊôÐÔÀ´Ö¸¶¨Ä£×ÓµÄÖ÷¼ü×ֶΣ¬¿ÉÒÔͨ¹ýÉèÖÃ$tableNameÊôÐÔÀ´Ö¸¶¨Ä£×Ó¹ØÁªµÄÊý¾Ý±íÃû¡£
˼Á¿ÒÔÏÂÄ£×Ó´úÂëʾÀý£º
namespace appindexmodel; use thinkModel; class User extends Model { protected $pk = 'id'; protected $tableName = "user"; protected $autoWriteTimestamp = true; protected $createTime = "create_time"; protected $updateTime = "update_time"; protected $userName = "user_name"; protected $userAge = "user_age"; }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏʾÀýÖУ¬ÎÒÃǽç˵ÁËÒ»¸öUserÄ£×Ó£¬ÆäÖаüÀ¨ÁË$pk¡¢$tableName¡¢$autoWriteTimestamp¡¢$createTime¡¢$updateTime¡¢$userNameºÍ$userAgeÊôÐÔ¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÆäÖУ¬$userNameºÍ$userAgeÊôÐÔÌåÏÖUserÄ£×Ó°üÀ¨µÄÁ½¸ö×ֶΡ£ÎÒÃÇ¿ÉÒÔʹÓÃÒÔÏ´úÂëÀ´ÉèÖúͻñÈ¡ÕâÁ½¸ö×ֶεÄÖµ£º
$user = new User; // ÉèÖÃuserNameºÍuserAge×ֶεÄÖµ $user->userName = 'Tom'; $user->userAge = 25; // ÉúÑÄÓû§ $user->save(); // »ñÈ¡Óû§ÃûºÍÄêËê $username = $user->userName; $userage = $user->userAge;
µÇ¼ºó¸´ÖÆ
2.ʹÓÃÊý¾Ý¿âǨáãÉèÖöà¸ö×Ö¶Î
ʹÓÃÊý¾Ý¿âǨáã¿ÉÒÔÇáËɵØÔÚÊý¾Ý¿âÖн¨Éè±íºÍ×ֶΡ£ÔÚThinkPHP¿ò¼ÜÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÊý¾Ý¿âǨáãʵÏÖÅúÁ¿µØÌí¼ÓºÍÐ޸ıíºÍ×ֶΡ£
ÒÔÏÂÊÇÒ»¸öʹÓÃÊý¾Ý¿âǨáãÉèÖöà¸ö×ֶεĴúÂëʾÀý£º
use thinkmigrationdbColumn; use thinkmigrationMigrator; class CreateUserTable extends Migrator { public function up() { $table = $this->table('user'); $table->addColumn('user_name', 'string', ['default' => '', 'comment' => 'Óû§Ãû']) ->addColumn('user_age', 'integer', ['default' => 0, 'comment' => 'ÄêËê']) ->addColumn('create_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'comment' => '½¨Éèʱ¼ä']) ->addColumn('update_time', 'timestamp', ['default' => 'CURRENT_TIMESTAMP', 'update' => 'CURRENT_TIMESTAMP', 'comment' => '¸üÐÂʱ¼ä']) ->create(); } public function down() { $this->dropTable('user'); } }
µÇ¼ºó¸´ÖÆ
ÔÚÒÔÉÏʾÀýÖУ¬ÎÒÃÇʹÓÃÁËthink-migrationÀ©Õ¹°üÀ´ÊµÏÖÊý¾ÝǨáã¡£
ʹÓÃaddColumnÒªÁì¿ÉÒÔÌí¼Ó¶à¸ö×ֶΣ¬ÆäÖÐÿ¸ö×ֶζ¼ÓÐÆäÀàÐÍ¡¢Ä¬ÈÏÖµºÍ×¢ÊÍ¡£Í¨¹ýcreateÒªÁì¿ÉÒÔ½¨ÉèÊý¾Ý±í¡£
×ܽá
ÔÚThinkPHP¿ò¼ÜÖÐÉèÖöà¸ö×Ö¶ÎÊǺÜÊǼòÆӵġ£ÎÒÃÇ¿ÉÒÔÔÚÄ£×ÓÖÐÖ±½ÓÉèÖöà¸ö×ֶΣ¬ÔÚÊý¾Ý¿âǨáãÖÐÅúÁ¿µØ½¨Éè±íºÍ×ֶΣ¬ÔÚÓ¦ÓóÌÐòÖÐʹÓÃÕâЩ×ֶΡ£Í¨¹ýÕâЩҪÁ죬ÎÒÃÇ¿ÉÒÔÇáËɵع¹½¨ÇкÏÐèÇóµÄÊý¾Ý½á¹¹£¬ÎªÎÒÃǵÄÓ¦ÓóÌÐòÌṩ¸üºÃµÄÖ§³Ö¡£
ÒÔÉϾÍÊÇthinkphp¿ò¼ÜÔõôÉèÖöà¸ö×ֶεÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡