尊龙凯时人生就是搏

ThinkPHP6ÖÐÔõÑù¾ÙÐÐORMÄ£×Ó¹ØÁª²Ù×÷ £¿

thinkphp6ÊÇÒ»¿îºÜÊÇÊ¢ÐеÄphp¿ª·¢¿ò¼Ü£¬ÌṩÁËÐí¶à±ã½ÝµÄorm£¨¹¤¾ß¹ØϵӳÉ䣩²Ù×÷ÒªÁìÒÔ¼ò»¯Êý¾Ý¿â²Ù×÷£¬²¢ÔÚ×îеİ汾ÖмÓÈëÁ˸ü¸»ºñµÄormÄ£×Ó¹ØÁªÒªÁ죬ʹµÃ¿ª·¢Ö°Ô±¿ÉÒÔ¸üÀû±ãµØ¾ÙÐÐÊý¾Ý¿â±íÖ®¼äµÄ¹ØÁªÅÌÎʲÙ×÷¡£

±¾ÎĽ«ÏÈÈÝÔÚThinkPHP6ÖÐÔõÑù¾ÙÐÐORMÄ£×Ó¹ØÁª²Ù×÷£¬°üÀ¨Ò»¶ÔÒ»¡¢Ò»¶Ô¶à¡¢¶à¶Ô¶à¹ØÁª²Ù×÷£¬Í¬Ê±Ò²½«Ú¹Ê͹ØÁª²Ù×÷µÄÏêϸʵÏÖ»úÖÆ¡£

Ò»¶ÔÒ»¹ØÁª²Ù×÷

Ò»¶ÔÒ»¹ØÁª²Ù×÷ÊÇÖ¸Á½¸ö±íÖ®¼äÖ»±£´æÖðÒ»¶ÔÓ¦¹ØϵµÄ²Ù×÷¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÓû§±í£¨users£©ºÍÒ»¸öÓû§ÏêϸÐÅÏ¢±í£¨user_details£©£¬Ã¿¸öÓû§¶¼¶ÔÓ¦Ò»¸öÓû§ÏêϸÐÅÏ¢¼Í¼£¬Óû§±íºÍÓû§ÏêϸÐÅÏ¢±íÖ®¼ä¾ÍÊÇÒ»¶ÔÒ»¹ØÁª¡£

Ê×ÏÈ£¬ÔÚÄ£×ÓÀàÖнç˵һ¶ÔÒ»¹ØÁª¹Øϵ£º

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»

namespace appmodel;

use thinkModel;

class User extends Model
{
    // ½ç˵һ¶ÔÒ»¹ØÁªÒªÁì
    public function detail()
    {
        return $this->hasOne('UserDetail');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚ½ç˵¹ØÁª¹Øϵʱ£¬ÎÒÃÇʹÓÃÁËhasOneÒªÁ죬¸ÃÒªÌå»á·µ»ØÒ»¸öBelongsTo¹¤¾ßʵÀý£¬ÌåÏÖÄ¿½ñÄ£×ÓÀà¡°ÓµÓÐÒ»¸ö¡±Ö¸ÏòUserDetailÄ£×ÓÀàµÄÒ»¶ÔÒ»¹ØÁª¹Øϵ¡£

½Ó×ÅÎÒÃÇÔÚUserDetailÄ£×ÓÀàÖнç˵·´Ïò¹ØÁªÒªÁ죺

namespace appmodel;

use thinkModel;

class UserDetail extends Model
{
    // ½ç˵·´Ïò¹ØÁªÒªÁì
    public function user()
    {
        return $this->belongsTo('User');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚUserDetailÄ£×ÓÀàÖУ¬ÎÒÃÇͬÑùʹÓÃÁËbelongsToÒªÁ죬¸ÃÒªÌå»á·µ»ØÒ»¸öhasOne¹¤¾ßʵÀý£¬ÌåÏÖÄ¿½ñÄ£×ÓÀà¡°¹éÊôÓÚ¡±UserÄ£×ÓÀàµÄÒ»¶ÔÒ»¹ØÁª¹Øϵ¡£

ÏÖÔÚÎÒÃǾͿÉÒÔͨ¹ýÒÔÏÂÒªÁì¾ÙÐÐÒ»¶ÔÒ»¹ØÁªÅÌÎÊ£º

// ÅÌÎÊÓû§ÐÅÏ¢£¬°üÀ¨ÆäÏêϸÐÅÏ¢
$user = User::with(['detail'])->find(1);

// ÅÌÎÊÓû§ÐÅÏ¢£¬Ö»°üÀ¨ÆäÏêϸÐÅÏ¢
$user = User::with(['detail' => function($query){
    $query->field('user_id, address');
}])->find(1);

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏʾÀýÖУ¬ÎÒÃÇͨ¹ýwithÒªÁìÀ´Ö¸¶¨¹ØÁªÄ£×Ó£¬²¢Ê¹ÓÃfindÒªÁìÅÌÎÊÓû§ÐÅÏ¢¡£¹ØÁªÅÌÎÊЧ¹û½«»áÒÔÊý×éÐÎʽ·µ»Ø£¬¿ÉÒÔƾ֤ÐèÒª¾ÙÐÐ×ֶιýÂË¡£

Ò»¶Ô¶à¹ØÁª²Ù×÷

Ò»¶Ô¶à¹ØÁª²Ù×÷ÊÇÖ¸Ò»¸ö±íÖеļͼ¿ÉÒÔ¶ÔÓ¦¶à¸öÁíÒ»ÕűíÖеļͼ¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸ö¿Î³Ì±í£¨courses£©ºÍÒ»¸öѧÉú±í£¨students£©£¬Ã¿¸ö¿Î³Ì¿ÉÒÔÓжà¸öѧÉúÑ¡ÐÞ£¬¿Î³Ì±íºÍѧÉú±íÖ®¼ä¾ÍÊÇÒ»¶Ô¶à¹ØÁª¡£

Ê×ÏÈ£¬ÔÚCourseÄ£×ÓÀàÖнç˵һ¶Ô¶à¹ØÁª¹Øϵ£º

namespace appmodel;

use thinkModel;

class Course extends Model
{
    // ½ç˵һ¶Ô¶à¹ØÁªÒªÁì
    public function students()
    {
        return $this->hasMany('Student');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚ½ç˵¹ØÁª¹Øϵʱ£¬ÎÒÃÇʹÓÃÁËhasManyÒªÁ죬¸ÃÒªÌå»á·µ»ØÒ»¸öHasMany¹¤¾ßʵÀý£¬ÌåÏÖÄ¿½ñÄ£×ÓÀà¡°ÓµÓжà¸ö¡±Ö¸ÏòStudentÄ£×ÓÀàµÄÒ»¶Ô¶à¹ØÁª¹Øϵ¡£

½Ó×ÅÎÒÃÇÔÚStudentÄ£×ÓÀàÖнç˵·´Ïò¹ØÁªÒªÁ죺

namespace appmodel;

use thinkModel;

class Student extends Model
{
    // ½ç˵·´Ïò¹ØÁªÒªÁì
    public function course()
    {
        return $this->belongsTo('Course');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚStudentÄ£×ÓÀàÖУ¬ÎÒÃÇͬÑùʹÓÃÁËbelongsToÒªÁ죬¸ÃÒªÌå»á·µ»ØÒ»¸öhasOne¹¤¾ßʵÀý£¬ÌåÏÖÄ¿½ñÄ£×ÓÀà¡°¹éÊôÓÚ¡±CourseÄ£×ÓÀàµÄÒ»¶Ô¶à¹ØÁª¹Øϵ¡£

ÏÖÔÚÎÒÃǾͿÉÒÔͨ¹ýÒÔÏÂÒªÁì¾ÙÐÐÒ»¶Ô¶à¹ØÁªÅÌÎÊ£º

// ÅÌÎʿγÌÐÅÏ¢£¬°üÀ¨ÆäÑ¡ÐÞѧÉúÐÅÏ¢
$course = Course::with(['students'])->find(1);

// ÅÌÎʿγÌÐÅÏ¢£¬Ö»°üÀ¨ÆäÑ¡ÐÞѧÉúÐÕÃûºÍÄêËêÐÅÏ¢
$course = Course::with(['students' => function($query){
    $query->field('name, age');
}])->find(1);

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏʾÀýÖУ¬ÎÒÃÇͨ¹ýwithÒªÁìÀ´Ö¸¶¨¹ØÁªÄ£×Ó£¬²¢Ê¹ÓÃfindÒªÁìÅÌÎʿγÌÐÅÏ¢¡£¹ØÁªÅÌÎÊЧ¹û½«»áÒÔÊý×éÐÎʽ·µ»Ø£¬¿ÉÒÔƾ֤ÐèÒª¾ÙÐÐ×ֶιýÂË¡£

¶à¶Ô¶à¹ØÁª²Ù×÷

¶à¶Ô¶à¹ØÁª²Ù×÷ÊÇÖ¸Á½¸ö±íÖ®¼ä±£´æ¶à¶Ô¶à¹ØϵµÄ²Ù×÷¡£ÀýÈ磬ÎÒÃÇÓÐÒ»¸ö¿Î³Ì±í£¨courses£©ºÍÒ»¸öÏÈÉú±í£¨teachers£©£¬Ã¿¸ö¿Î³Ì¿ÉÒÔÓɶà¸öÏÈÉúÊڿΣ¬Ã¿¸öÏÈÉúÒ²¿ÉÒÔÊÚ¶à¸ö¿Î³Ì£¬¿Î³Ì±íºÍÏÈÉú±íÖ®¼ä¾ÍÊǶà¶Ô¶à¹ØÁª¡£

Ê×ÏÈ£¬ÔÚCourseÄ£×ÓÀàÖнç˵¶à¶Ô¶à¹ØÁª¹Øϵ£º

namespace appmodel;

use thinkModel;

class Course extends Model
{
    // ½ç˵¶à¶Ô¶à¹ØÁªÒªÁì
    public function teachers()
    {
        return $this->belongsToMany('Teacher', 'course_teacher');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚ½ç˵¹ØÁª¹Øϵʱ£¬ÎÒÃÇʹÓÃÁËbelongsToManyÒªÁ죬¸ÃÒªÌå»á·µ»ØÒ»¸öBelongsToMany¹¤¾ßʵÀý£¬ÌåÏÖÄ¿½ñÄ£×ÓÀà¡°ÊôÓÚ¶à¸ö¡±Ö¸ÏòTeacherÄ£×ÓÀàµÄ¶à¶Ô¶à¹ØÁª¹Øϵ¡£ÎÒÃÇ»¹ÐèÒª´«ÈëµÚ¶þ¸ö²ÎÊý¡®course_teacher¡¯£¬ÌåÏÖÖÐÐıíµÄ±íÃû¡£

½Ó×ÅÎÒÃÇÔÚTeacherÄ£×ÓÀàÖнç˵·´Ïò¹ØÁªÒªÁ죺

namespace appmodel;

use thinkModel;

class Teacher extends Model
{
    // ½ç˵·´Ïò¹ØÁªÒªÁì
    public function courses()
    {
        return $this->belongsToMany('Course', 'course_teacher');
    }
}

µÇ¼ºó¸´ÖÆ

ÔÚTeacherÄ£×ÓÀàÖУ¬ÎÒÃÇͬÑùʹÓÃÁËbelongsToManyÒªÁ죬¸ÃÒªÌå»á·µ»ØÒ»¸öBelongsToMany¹¤¾ßʵÀý£¬ÌåÏÖÄ¿½ñÄ£×ÓÀà¡°°üÀ¨¶à¸ö¡±Ö¸ÏòCourseÄ£×ÓÀàµÄ¶à¶Ô¶à¹ØÁª¹Øϵ¡£Í¬ÑùÐèÒª´«ÈëµÚ¶þ¸ö²ÎÊý¡®course_teacher¡¯£¬ÌåÏÖÖÐÐıíµÄ±íÃû¡£

ÏÖÔÚÎÒÃǾͿÉÒÔͨ¹ýÒÔÏÂÒªÁì¾ÙÐжà¶Ô¶à¹ØÁªÅÌÎÊ£º

// ÅÌÎʿγÌÐÅÏ¢£¬°üÀ¨ÆäÊÚ¿ÎÏÈÉúÐÅÏ¢
$course = Course::with(['teachers'])->find(1);

// ÅÌÎʿγÌÐÅÏ¢£¬Ö»°üÀ¨ÆäÊÚ¿ÎÏÈÉúÐÕÃûºÍÖ°³ÆÐÅÏ¢
$course = Course::with(['teachers' => function($query){
    $query->field('name, title');
}])->find(1);

µÇ¼ºó¸´ÖÆ

ÔÚÒÔÉÏʾÀýÖУ¬ÎÒÃÇͨ¹ýwithÒªÁìÀ´Ö¸¶¨¹ØÁªÄ£×Ó£¬²¢Ê¹ÓÃfindÒªÁìÅÌÎʿγÌÐÅÏ¢¡£¹ØÁªÅÌÎÊЧ¹û½«»áÒÔÊý×éÐÎʽ·µ»Ø£¬¿ÉÒÔƾ֤ÐèÒª¾ÙÐÐ×ֶιýÂË¡£

ÖÁ´Ë£¬ÎÒÃÇÒѾ­ÏàʶÁËÔÚThinkPHP6ÖÐÔõÑù¾ÙÐÐORMÄ£×Ó¹ØÁª²Ù×÷£¬°üÀ¨Ò»¶ÔÒ»¡¢Ò»¶Ô¶à¡¢¶à¶Ô¶à¹ØÁª²Ù×÷¡£ORMÄ£×Ó¹ØÁª²Ù×÷¿ÉÒÔ¾«Á·µØÍê³ÉÊý¾Ý¿â±íÖ®¼äµÄ¹ØÁªÅÌÎÊ£¬´ó´óÌá¸ßÁË¿ª·¢Ð§ÂÊ£¬Í¬Ê±Ò²°ü¹ÜÁËÊý¾Ý¿â²Ù×÷µÄ׼ȷÐÔ¡£

ÒÔÉϾÍÊÇThinkPHP6ÖÐÔõÑù¾ÙÐÐORMÄ£×Ó¹ØÁª²Ù×÷ £¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是搏ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是搏ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是搏

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ