【问题标题】:Eloquent ORM - Models RelationshipEloquent ORM - 模型关系
【发布时间】:2015-10-29 14:47:39
【问题描述】:

我对 OctoberCMS / Lavarel ORM 还很陌生,我在实施模型关系方面有点卡住了。我有两个数据库表/模型,其中一个代表有关程序A, B, C 的一般信息,第二个代表与这些程序相关的不同级别的一般信息..

目标:我想检索某个程序X的信息+检索与其关联的程序级别的所有信息。

型号

模型 1 - 表架构

    Schema::create('pgs_programs', function($table)
    {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->char('prog_code', 10); // Unique ID/SKU
        $table->string("prog_slug" )->nullable();
        $table->string("prog_title")->nullable();
        $table->text("prog_intro_title")->nullable();
        $table->text("prog_intro")->nullable();
        $table->text("prog_top_img" )->nullable();
        $table->timestamps();
    });

模型 2 - 表架构

    Schema::create('pgs_program_levels', function($table)
{
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->char('prog_code', 10); // Related to Parent Program ID/SKU
    $table->string('level_title')->nullable();
    $table->enum('prog_level', array(4,5,6,7))->nullable();
    $table->text('prog_duration')->nullable();
    $table->text("prog_desc")->nullable();
    $table->text("prog_assesments" )->nullable();
    $table->timestamps();
});

组件

namespace PGS\Program\Components;
use Cms\Classes\ComponentBase;
use PGS\Program\Models\Program;  // Model 1
use PGS\Program\Models\ProgramLevels; // Model 2

  public function onRun(){

    $Programmes = Program::all();
    $X = $this->param('programme');
    $Y = $this->param('disciplines');
    $slug = $X."/".$Y ;
    $arr = array();

       foreach($Programmes as $k){
                $arr[]= $k['prog_slug'];
            }
      if(in_array($slug, $arr) ){
          // here query both models
          // this query gets General info from table1
          $progInfo = Program::where('prog_slug', '=', $slug)->first(); 
                } else {
                    return $this->controller->run('404');
                }
}

我想了解/使用array Relations,以便我可以在一个查询中执行以下操作。我知道我可以做多个查询:

查询 1:

 $progInfo = Program::where('prog_slug', '=', $slug)->first(); 

然后查询2:

 $progLvlsInfo = ProgramLevels::where('prog_code', '=', $progInfo['prog_code'])->get();
 // returns array of all program levels

我在SO 上看到了这个帖子,它处理了同样的问题。我尝试在程序模型中添加$belongTo,反之亦然,但没有成功..

 public $belongsTo = [
        'program' =>       ['PGS\Program\Models\ProgramLevels',
            'foreignKey' => 'prog_code']
    ];

我应该坚持做上面的两个查询,还是使用 Join 语句或使用模型中提供的关系..

public $hasOne = [];
public $hasMany = [];
public $belongsTo = [];
public $belongsToMany = [];
public $morphTo = [];
public $morphOne = [];
public $morphMany = [];
public $attachOne = [];
public $attachMany = [];

非常感谢!

【问题讨论】:

  • 随着您的应用程序变得越来越复杂,您将需要弄清楚关系......它们将使您的生活更加理智。
  • @LuckyBurger 我知道!! :) 我只是似乎还不了解它们。我想我宁愿从一开始就尝试养成好习惯。我不确定以上是否被认为是错误的逻辑/代码..
  • 您在搞清楚语法时遇到了麻烦吗?您需要一个示例还是 2 个示例?
  • mmm 更多关于如何创建模型 A 和模型 B 之间的关系,所以当我查询模型 AI 时,也会从模型 B 中获取相关记录,在这种情况下,如果我查询一个程序,则会获取所有相关级别(学科)每个学科/记录(prog_code)代表父程序的唯一键..:/正如我上面提到的,我可以用2个查询来做到这一点,所以我想知道是否使用 $belongTo 将替代需要查询 2 ?或者关系的使用是为了不同的目的,我现在应该坚持这种方法..
  • belongsTo 不是唯一的关系...您可能想弄清楚 hasOne、hasMany、hasManyThrough ...

标签: php mysql orm eloquent octobercms


【解决方案1】:

定义类 UserAccount

定义类游戏

一个 UserAccount 可以拥有多个游戏。

//UserAccount 
function games() {
    return $this->hasMany('Games', 'user_id');
}

UserAccount::find(1)->games();

参考:

http://laravel.com/docs/5.0/eloquent#querying-relations

【讨论】:

    猜你喜欢
    • 2016-02-21
    • 1970-01-01
    • 2021-11-17
    • 2016-02-26
    • 2019-09-20
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多