ÔõÑùÔÚLaravelÖоÙÐйØÁªÄ£×ÓµÄɾ³ý²Ù×÷
ÔÚlaravel¿ò¼ÜÖУ¬¹ØÁªÄ£×ÓÊÇÒ»ÖÖÎÞа¡¢Ç¿Ê¢µÄ¹¤¾ß£¬¿ÉÒÔÓÃÀ´´¦Öóͷ£ÖØ´óµÄ¹ØϵÊý¾Ý¡£È»¶ø£¬ÔÚɾ³ý¹ØÁªÄ£×Óʱ£¬ÐèÒªÌØÊâ×¢ÖØ£¬ÒÔÃâ·ºÆð²»ÐëÒªµÄÎó²Ù×÷ºÍÊý¾ÝʧÎó¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚlaravelÖÐ׼ȷµØ¾ÙÐйØÁªÄ£×ÓµÄɾ³ý²Ù×÷¡£
Ò»¡¢Ã÷È·¹ØÁªÄ£×Ó
ÔÚLaravelÖУ¬¹ØÁªÄ£×ÓÊÇÖ¸ÔÚÁ½¸ö»ò¶à¸öÊý¾Ý±íÖ®¼ä½¨ÉèµÄ¹Øϵ¡£Í¨³£ÇéÐÎÏ£¬ÕâЩ¹Øϵ±»³ÆΪ¡°¹ØÁª¹Øϵ¡±£¬ÈçÒ»¶ÔÒ»¡¢Ò»¶Ô¶à¡¢¶à¶Ô¶àµÈ¡£Í¨¹ýLaravelµÄORM£¨¹¤¾ß¹ØϵӳÉ䣩¿ò¼Ü£¬ÎÒÃÇ¿ÉÒÔÇáËɵؽ¨ÉèÖÖÖÖ¹ØÁªÄ£×Ó£¬²¢ÇÒºÜÊÇÀû±ãµØ¾ÙÐÐÔöɾ¸Ä²éµÈÖݪֲÙ×÷¡£
ÀýÈ磬ÎÒÃÇÓÐÁ½¸ö±í¸ñ»®·ÖÊÇ¡°Óû§±í¡±ºÍ¡°¶©µ¥±í¡±£¬ËüÃÇÖ®¼äÓÐÒ»¸ö¡°Ò»¶Ô¶à¡±µÄ¹Øϵ£¬¼´Ò»¸öÓû§¿ÉÒÔÓµÓжà¸ö¶©µ¥¡£ÏÖÔÚ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýLaravelµÄORM¿ò¼Ü½ç˵ÕâÁ½¸öÄ£×ÓÖ®¼äµÄ¹ØÁª¹Øϵ£¬´úÂëÈçÏÂ:
class User extends Model { public function orders() { return $this->hasMany(Order::class); } } class Order extends Model { public function user() { return $this->belongsTo(User::class); } }
µÇ¼ºó¸´ÖÆ
ͨ¹ýÕâÑùµÄ¹ØÁª¹Øϵ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýÒ»¸öÓû§¹¤¾ß£¬ºÜÊÇÀû±ãµØ»ñÈ¡µ½¸ÃÓû§ËùÓµÓеÄËùÓж©µ¥£º
$user = User::find(1); $orders = $user->orders;
µÇ¼ºó¸´ÖÆ
¶þ¡¢¹ØÁªÄ£×ÓµÄɾ³ý
ÏÖÔÚ£¬ÎÒÃÇÀ´Ë¼Á¿ÏÂÃæÕâ¸öÎÊÌ⣺ÈôÊÇÎÒÃÇҪɾ³ýÒ»¸öÓû§£¬ÄÇôËûËùÓµÓеĶ©µ¥Ò²Ó¦¸ÃËæ֮ɾ³ý£¬²»È»¾Í¿ÉÄܱ£´æ¡°¹Â¶ùÊý¾Ý¡±µÄΣº¦¡£¸ÃÈçÄÇÀïÖÃÕâ¸öÎÊÌâÄØ£¿ÔÚLaravelÖУ¬Õë¶ÔÕâÖÖÎÊÌ⣬ÎÒÃÇ¿ÉÒÔʹÓá°¼¶ÁªÉ¾³ý£¨Cascade Delete£©¡±µÄ¹¦Ð§À´´¦Öóͷ£¡£
1¡¢Ê¹Óü¶ÁªÉ¾³ý
ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÔÚ¹ØÁªÒªÁìÖÐʹÓá°cascade¡±ÊôÐÔÀ´ÉèÖü¶ÁªÉ¾³ý£¬´úÂëÈçÏ£º
class User extends Model { public function orders() { return $this->hasMany(Order::class)->cascadeDelete(); } }
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬µ±ÎÒÃÇɾ³ýÒ»¸öÓû§Ê±£¬ËûËùÓµÓеÄËùÓж©µ¥Êý¾ÝÒ²»á±»×Ô¶¯É¾³ý£¬ºÜÊÇÀû±ã¡£
2¡¢ÊÖ¶¯É¾³ý¹ØÁªÄ£×Ó
³ýÁ˼¶ÁªÉ¾³ýÖ®Í⣬ÎÒÃÇÒ²¿ÉÒÔÊÖ¶¯É¾³ý¹ØÁªÄ£×Ó¡£ÔÚLaravelÖУ¬É¾³ý¹ØÁªÄ£×ÓµÄÒªÁìʹÓõÄÊÇ¡°delete£¨£©¡±ÒªÁì¡£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÊ×ÏÈ»ñȡij¸öÓû§£º
$user = User::find(1);
µÇ¼ºó¸´ÖÆ
È»ºó£¬¹ØÓÚ¸ÃÓû§ËùÓµÓеÄËùÓж©µ¥£¬ÎÒÃÇ¿ÉÒÔÊÖ¶¯Ö´ÐÐɾ³ý²Ù×÷£º
foreach ($user->orders as $order) { $order->delete(); }
µÇ¼ºó¸´ÖÆ
¿ÉÊÇ£¬ÕâÖÖÒªÁì±£´æһЩΣº¦£¬¿ÉÄÜ»áÔÚ²Ù×÷Àú³ÌÖе¼ÖÂÊý¾ÝµÄ²»ÍêÕûÐÔ¡£ÀýÈ磬ÈôÊÇÎÒÃÇÔÚÊÖ¶¯É¾³ýij¸ö¶©µ¥Ê±£¬±£´æÆäËûµØÆÓÖ±ÔÚʹÓÃÕâ¸ö¶©µ¥µÄÇéÐΣ¬ÄÇô¾Í¿ÉÄÜ·ºÆðÊý¾ÝɥʧºÍ·×ÆçÖµÄÎÊÌâ¡£
3¡¢Ê¹ÓÃÊÂÎñ¼àÌýÆ÷
ΪÁË×èÖ¹ÊÖ¶¯É¾³ý¹ØÁªÄ£×Óʱ¿ÉÄÜ·ºÆðµÄÎÊÌ⣬ÎÒÃÇ¿ÉÒÔͨ¹ýLaravelµÄÊÂÎñ¼àÌýÆ÷À´´¦Öóͷ£Õâ¸öÎÊÌâ¡£ÊÂÎñ¼àÌýÆ÷¿ÉÒÔÔÚÄ£×ÓµÄÌض¨ÊÂÎñ±¬·¢Ê±£¬×Ô¶¯Ö´ÐÐһЩ²Ù×÷£¬ÀýÈçÔÚɾ³ý¹ØÁªÄ£×Óʱ£¬×Ô¶¯É¾³ýÏà¹ØµÄÊý¾Ý¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚ×ðÁú¿Ê±Ä£×ÓÀàÖÐ×¢²áÒ»¸ö¡°deleting¡±ÊÂÎñ£¬ÔÚ¸ÃÊÂÎñÖÐÖ´ÐÐɾ³ý²Ù×÷£¬ÈçÏÂËùʾ£º
class User extends Model { protected static function boot() { parent::boot(); static::deleting(function($user) { foreach ($user->orders as $order) { $order->delete(); } }); } }
µÇ¼ºó¸´ÖÆ
µ±ÎÒÃÇҪɾ³ýij¸öÓû§Ê±£¬»á´¥·¢¸ÃÓû§¹¤¾ßµÄ¡°deleting¡±ÊÂÎñ£¬´Ó¶ø×Ô¶¯É¾³ýÓëÖ®¹ØÁªµÄ¶©µ¥Êý¾Ý¡£
ËÄ¡¢ÊµÀýÑÝʾ
½ÓÏÂÀ´£¬ÎÒÃǽ«ÑÝʾÔõÑùÔÚLaravelÖÐʵÏÖ¹ØÁªÄ£×ÓµÄɾ³ý²Ù×÷¡£¼ÙÉèÎÒÃÇÓÐÁ½¸ö±í¸ñ¡°users¡±ºÍ¡°orders¡±£¬ÆäÖС°orders¡±Óë¡°users¡±Ö®¼ä±£´æÒ»¶Ô¶àµÄ¹Øϵ£¬¼´Ò»¸öÓû§¿ÉÒÔÓµÓжà¸ö¶©µ¥¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÕâÁ½¸ö±í¸ñÖ®¼ä½¨Éè¹ØÁªÄ£×Ó£¬²¢Ê¹ÓÃLaravelµÄORM¿ò¼ÜÌṩµÄ¡°cascadeDelete¡±ÒªÁìÀ´ÊµÏÖ¼¶ÁªÉ¾³ý£¬ÈçÏÂËùʾ£º
class User extends Model { public function orders() { return $this->hasMany(Order::class)->cascadeDelete(); } } class Order extends Model { public function user() { return $this->belongsTo(User::class); } }
µÇ¼ºó¸´ÖÆ
ÔÚÕâÀÎÒÃǽ«¡°User¡±Óë¡°Order¡±Á½¸öÄ£×ÓÖ®¼ä½¨ÉèÁËÒ»¶Ô¶àµÄ¹Øϵ£¬²¢ÇÒʹÓÃÁË¡°cascadeDelete()¡±ÒªÁìÀ´ÊµÏÖ¼¶ÁªÉ¾³ý¡£
½ÓÏÂÀ´£¬ÎÒÃǽ«±àдһ¸ö²âÊÔ´úÂëÀ´ÑÝʾÔõÑùʹÓÃÕâ¸ö¹ØÁª¹Øϵ¡£Ê×ÏÈ£¬ÎÒÃǽ¨ÉèÒ»¸ö¡°User¡±¹¤¾ß²¢ÉúÑĵ½Êý¾Ý¿âÖУº
$user = new User; $user->name = 'test'; $user->email = 'test@example.com'; $user->save();
µÇ¼ºó¸´ÖÆ
È»ºó£¬ÎÒÃÇÍù¸ÃÓû§ÏÂÃæÌí¼ÓÈý¸ö¶©µ¥£º
$user->orders()->createMany([ ['product' => 'A', 'price' => 100], ['product' => 'B', 'price' => 200], ['product' => 'C', 'price' => 300], ]);
µÇ¼ºó¸´ÖÆ
ÏÖÔÚ£¬ÎÒÃÇʹÓÃLaravelµÄORM¿ò¼ÜÌṩµÄ¡°delete()¡±ÒªÁìÀ´É¾³ý¸ÃÓû§£º
$user->delete();
µÇ¼ºó¸´ÖÆ
ÕâÑù£¬¸ÃÓû§ËùÓµÓеÄËùÓж©µ¥Êý¾ÝÒ²»á±»×Ô¶¯É¾³ý£¬ºÜÊÇÀû±ã¡£
Îå¡¢×ܽá
ÔÚLaravel¿ò¼ÜÖУ¬´¦Öóͷ£¹ØÁªÄ£×ÓÊý¾ÝÊÇ¿ª·¢Öо³£Óöµ½µÄÎÊÌ⡣ͨ¹ýѧϰ±¾ÎÄÏÈÈݵÄÄÚÈÝ£¬ÎÒÃÇ¿ÉÒÔ׼ȷµØɾ³ý¹ØÁªÄ£×ÓÊý¾Ý£¬²¢×èÖ¹·ºÆðÊý¾ÝµÄ²»ÍêÕûÐÔ¡£ÈôÊÇÄãÏë¸üºÃµØÕÆÎÕLaravelµÄORMÊÖÒÕ£¬ÇëÎñ±Ø¶à¶àʵ¼ùºÍ˼Ë÷¡£
ÒÔÉϾÍÊÇÔõÑùÔÚLaravelÖоÙÐйØÁªÄ£×ÓµÄɾ³ý²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡