【问题标题】:Querying Many to One to Relation in October CMS as a Join在十月 CMS 中查询多对一关系作为联接
【发布时间】:2015-01-07 17:19:38
【问题描述】:

鉴于以下 10 月 CMS 插件模型:

Schema::create('iaff106_cellphone_cellphones', function($table){
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->integer('user_id')->unsigned()->nullable()->index();
    $table->string('label');
    $table->string('phone')->nullable()->index();
    $table->integer('provider_id')->nullable();            
    $table->boolean('is_txtable')->default(true);
    $table->boolean('is_published')->default(false);
    $table->timestamps();
    });


Schema::create('iaff106_cellphone_providers', function($table){
    $table->engine = 'InnoDB';
    $table->increments('id');
    $table->string('name');
    $table->string('slug')->index();
    $table->string('mail_domain')->nullable();
    $table->timestamps();
    }); 

提供者的关系:

public $belongsToMany = ['IAFF106\CellPhone\Models\Cellphone', 'foreignKey' => 'provider_id'];

手机关系:

public $hasOne = ['IAFF106\CellPhone\Models\Provider'];

我想选择一个手机和相关的服务提供商来获取“电话”和“姓名”字段。

在我尝试过的组件中:

public function onRun()
{
    $this->cellphone = $this->page['cellphone'] = $this->loadCell();
}

protected function loadCell()
{
    $slug = $this->propertyOrParam('idParam');
    $cellphone = Cellphone::isPublished()->where('id', '=', $slug)->first();
    $this->provider = $this->page['provider'] = $cellphone->provider;

    return $cellphone;
}

$this->cellphone 有我想要的手机数据,但我不知道如何访问 Provider 'name' 信息。

如何从视图中的两个模型加载和访问数据?

在我看来我已经尝试过了:

<ul>
    <li>{{ cellphone.phone }}  {{ provider.name }} </li>
    <li>Try Again </li>
    <li>{{ cellphone.phone }}  {{ cellphone.provider.name }} </li>
</ul>

{{ cellphone.phone }} 显示正常,但我无法获取提供商名称。

【问题讨论】:

  • 您想在哪里访问提供者名称?意味着在 PHP 或视图中?
  • 我编辑了我的帖子以添加我想要实现的视图。

标签: laravel-4 eloquent inner-join octobercms


【解决方案1】:

好的,我得到了解决方案,它是你模型中的一个问题

你需要放

public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];

而不是

public $hasOne = ['IAFF106\CellPhone\Models\Provider'];

这是 CellPhone.php 的型号代码

<?php namespace IAFF106\CellPhone\Models;

use Model;

/**
 * CellPhone Model
 */
class CellPhone extends Model
{

    /**
     * @var string The database table used by the model.
     */
    public $table = 'iaff106_cellphone_cell_phones';

    /**
     * @var array Guarded fields
     */
    protected $guarded = ['*'];

    /**
     * @var array Fillable fields
     */
    protected $fillable = [];

    /**
     * @var array Relations
     */

    public $belongsTo = [
        'provider' => ['IAFF106\CellPhone\Models\Provider', 'foreignKey' => 'provider_id']
    ];

    public function scopeIsPublished($query)
    {
        return $query
            ->whereNotNull('is_published')
            ->where('is_published', '=', 1);
    }

}

请查看文档以了解 10 月 CMS 中的关系 https://octobercms.com/docs/database/model#relationships

如果您指定手机有一个提供商,那么 cellphone_id 必须在您的提供商表中,您可以按照自己的方式进行管理,但您定义关系的方式必须与文档中指定的方式相同。

组件和查看代码就好了。

【讨论】:

    【解决方案2】:

    感谢 Anand Patel 发现关系中的主要问题。

    为了方便其他人,我在这里再次发布我的简单但关键的代码片段。 我确实让它像这样工作。

    在手机型号中:

    /**
     * @var array Guarded fields
     */
    protected $guarded = [];
    
    
    /**
     * @var array Relations
     */
    public $belongsTo = [
        'user' =>       ['RainLab\User\Models\User', 
                        'foreignKey' => 'user_id'],
        'provider' =>   ['IAFF106\CellPhone\Models\Provider', 
                        'foreignKey' => 'provider_id']
        ];
    

    在提供者模型中:

    public $hasMany = ['cellphone' =>   ['IAFF106\CellPhone\Models\Cellphone', 
                        'foreignKey' => 'provider_id']
        ];
    

    在组件中我做了:

    $cellphones = Cellphone::where('user_id', '=', $this->userid)->get();
    

    在我所做的视图中:

    <ul>
    {% for cellphone in cellphones  %}
        <li>
            {{ cellphone.label }} : <strong>{{ cellphone.phone }} </strong>  
            <sub>{{ cellphone.provider.name }}</sub>
        </li>
    {% endfor %}
    </ul>
    

    我最初的主要问题是我格式化关系数组的方式。我需要为我的关系数组指定键。比较我的第一篇文章和这篇文章,看看我的意思。

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 1970-01-01
      • 2012-12-07
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多