laravel·Ö±íµÄʵÏÖµÄ
laravelÊÇÒ»¿îÓÅÒìµÄphp¿ª·¢¿ò¼Ü£¬ËüÔÚʵÏÖorm£¨¹¤¾ß¹ØϵӳÉ䣩²Ù×÷ÖÐÌṩÁËÇ¿Ê¢µÄÖ§³Ö¡£¿ÉÊÇ£¬¹ØÓÚһЩ´óÊý¾ÝÇéÐÎϵÄÓ¦Óã¬ÎÒÃÇ¿ÉÄÜÐèÒª½«±í¾ÙÐÐÖ§½â£¬ÒÔ±ã¸üºÃµØÖÎÀíÊý¾Ý¡£ÄÇô£¬laravelÔõÑùʵÏÖ·Ö±íÄØ£¿
Ò»¡¢LaravelµÄ·Ö±íÔÀí
ÔÚLaravelÖУ¬Êý¾ÝÄ£×ӵĻù´¡ÊÇEloquent ORM¡£µ±ÎÒÃÇʹÓÃEloquentµÄÄ£×ÓÀ´»á¼ûÊý¾Ý¿âʱ£¬Laravel½«Æ¾Ö¤Ä£×ÓÃû³ÆÌìÉúÏìÓ¦µÄÊý¾Ý¿â±íÃû¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÄ£×ÓÀàUser£¬Laravel½«Ä¬ÈÏ»á¼ûÃûΪusersµÄÊý¾Ý¿â±í¡£
ÄÇô£¬·Ö±íµÄÔÀí¾ÍºÜ¼òÆÓÁË£¬ÎÒÃÇÖ»ÐèÒªÔÚÄ£×ÓÀàÖнç˵һ¸ö$tableÊôÐÔÀ´Ö¸¶¨±íÃû¼´¿É¡£¹ØÓÚ·Ö±íµÄÇéÐΣ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚÄ£×ÓÀàÖж¯Ì¬ÐÞ¸Ä$tableÊôÐÔÒÔ»á¼û²î±ðµÄ±í¡£
¶þ¡¢LaravelµÄ·Ö±íʵÏÖ
ÏÂÃ棬ÎÒÃÇÀ´¿´Ò»ÏÂÔõÑùÔÚLaravelÖÐʵÏÖ·Ö±í¡£
1.ÊÖ¶¯Ð޸ıíÃû
ͨ¹ýEloquent ORM£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÊÖ¶¯ÐÞ¸ÄÄ£×ÓÀàµÄ$tableÊôÐÔÀ´»á¼û²î±ðµÄÊý¾Ý¿â±í¡£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÃûΪOrderµÄÄ£×ÓÀಢÇÒÐèÒª½«¶©µ¥±í·Ö³Éorder_1Óëorder_2Á½ÕÅ±í¡£ÄÇô£¬ÎÒÃÇÖ»ÐèÔÚÄ£×ÓÀàÖнç˵ÈçÏ´úÂ룺
use IlluminateDatabaseEloquentModel; class Order extends Model { //ÅþÁ¬µ½Ä£×ÓµÄÊý¾Ý±í protected $table = 'order_1'; }
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬µ±Äã»á¼û¸ÃÄ£×Óʱ£¬½«»á×Ô¶¯Ö¸Ïòorder_1±í¡£
ÈôÊÇÏëÒª»á¼ûorder_2±í£¬ÎÒÃÇÖ»ÐèÒªÐÞ¸ÄÄ£×ÓÀàµÄ$tableÊôÐÔ¼´¿É¡£
use IlluminateDatabaseEloquentModel; class Order extends Model { //ÅþÁ¬µ½Ä£×ÓµÄÊý¾Ý±í protected $table = 'order_2'; }
µÇ¼ºó¸´ÖÆ
2.×Ô¶¯»¯·Ö±í
ÊÖ¶¯Ð޸ıíÃûËäȻʵÏÖ¼òÆÓ£¬µ«¹ØÓÚ·Ö±íÊýÄ¿¹ý¶àµÄÇéÐÎÏ£¬ÊÖ¶¯ÐÞ¸ÄÒ²±äµÃ·±ËöÇÒÒ×´í¡£Òò´Ë£¬ÎÒÃÇ¿ÉÒÔΪLaravel½¨ÉèÒ»¸ö·Ö±íÀàÀ´×Ô¶¯ÖÎÀí±íÃû¡£
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÄ£×Ó¹¤³§ÀàÀ´×Ô¶¯»¯·Ö±í¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÃûΪOrderµÄÄ£×ÓÀ࣬ÐèÒª½«¶©µ¥±íƾ֤Óû§ID·ÖΪÈô¸É¸öС±í¡£ÎÒÃÇ¿ÉÒÔ½¨ÉèÒ»¸öÃûΪOrderFactoryµÄÄ£×Ó¹¤³§À࣬ÓÃÓÚ¶¯Ì¬ÌìÉú±íÃû²¢ÇÒ×Ô¶¯ÖÎÀí±íÃû¡£
use IlluminateSupportFacadesDB; use IlluminateDatabaseEloquentModel; class OrderFactory { private $userId; private $tableName = 'orders_'; public function __construct($userId) { $this->userId = $userId; $this->tableName .= $this->getTableName(); } public function makeModel() { $model = new Model(); $model->setTable($this->tableName); return $model; } protected function getTableName() { $tableId = intval($this->userId) % 10 + 1; return $tableId; } }
µÇ¼ºó¸´ÖÆ
ÔÚ¹¤³§ÀàÖУ¬ÎÒÃÇƾ֤Óû§IDÅÌËã³öÏìÓ¦µÄ±íID£¬ÒÔ±ãÌìÉú׼ȷµÄ±íÃû¡£ÔÚÌìÉúÄ£×Óʱ£¬ÎÒÃÇÖ»ÐèŲÓÃmakeModelÒªÁì¼´¿É¡£
ÀýÈ磬ÎÒÃÇÐèÒª»ñÈ¡Óû§IDΪ123µÄ¶©µ¥ÁÐ±í£¬ÎÒÃÇ¿ÉÒÔÈçÏÂŲÓãº
$factory = new OrderFactory(123); $order = $factory->makeModel()->get();
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬ÎÒÃǾÍÀֳɵØʵÏÖÁËLaravelÖеķֱí²Ù×÷¡£
Èý¡¢Laravel·Ö±íµÄÓÅÈõµã
ËäÈ»ÔÚijЩÇéÐÎÏ·ֱí¿ÉÒÔ´øÀ´Öî¶àÓÅÊÆ£¬µ«·Ö±íÒ²±£´æһЩÈõµã¡£
1.Óŵã
£¨1£©ÐÔÄÜÌáÉý
µ±Êý¾ÝÁ¿´óʱ£¬·Ö±í¿ÉÒÔ½«±íÖеÄÊý¾ÝÁ¿¼õС£¬´Ó¶øÌá¸ßÅÌÎÊЧÂÊ¡£Í¬Ê±£¬ÓÉÓÚÊý¾Ý·ÖÀ࣬ÎÒÃÇ¿ÉÒÔʹÓøüÓÅÒìµÄË÷Òý·½·¨À´ÓÅ»¯ÅÌÎÊËÙÂÊ¡£
£¨2£©¿ÉÀ©Õ¹ÐÔ
µ±Êý¾ÝÁ¿ÔöÌíµ½¼«ÏÞʱ£¬ÎÒÃÇ¿ÉÒÔʹÓ÷ֱíÀ´À©Õ¹Êý¾Ý¿â£¬´Ó¶øʵÏÖ¸ü¸ßµÄ¿ÉÀ©Õ¹ÐÔ¡£
2.Èõµã
£¨1£©ÆÌÕÅ¿Õ¼ä
·Ö±í»á½«Êý¾ÝÊèÉ¢µ½¶à¸ö±íÖУ¬»á´øÀ´Ò»¶¨µÄ¿Õ¼äÆÌÕÅ¡£ÀýÈ磬ÈôÊÇÎÒÃÇƾ֤Óû§ID·Ö±í¾ÙÐÐÖ§½â£¬¶øij¸öÓû§µÄÊý¾ÝÏà¹ØÓÚÆäËûÓû§À´ËµºÜÉÙ£¬ÄÇôÆäËû·Ö±íÖоͻᱣ´æ´ó×ڵĿÕÖÃÊý¾Ý¡£
£¨2£©ÔöÌí¿ª¾ÙʶÈ
ʹÓ÷ֱí»á½«Êý¾ÝÊèÉ¢µ½¶à¸ö±íÖУ¬ÄÇô¹ØÓÚÊý¾Ý±íµÄά»¤»á´øÀ´Ò»¶¨µÄÖØ´óÐÔ¡£Í¬Ê±£¬Ê¹Ó÷ֱíÒ²ÐèÒª¶Ô´úÂë¾ÙÐÐÏìÓ¦µÄÓÅ»¯£¬ÔöÌíÁË¿ª¾Ùʶȡ£
×ÛÉÏ£¬LaravelµÄ·Ö±íʵÏÖ¹ØÓÚ´óÊý¾ÝÇéÐÎϵÄÓ¦ÓÿÉÒÔ´øÀ´¸ü¸ßµÄÐÔÄܺͿÉÀ©Õ¹ÐÔ£¬¿ÉÊÇÒ²´øÀ´ÁËһЩÖØ´óÐԺͿռäÆÌÕÅÎÊÌ⣬ÎÒÃÇÔÚʹÓÃʱÐèҪƾ֤ÏêϸÇéÐÎȨºâÀû±×¡£
ÒÔÉϾÍÊÇlaravel·Ö±íµÄʵÏֵĵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡