2

我有这个 Laravel/OctoberCMS 模型来与我的 ExchangeModel 交互。

这是与我的 ExchangeModel 的片面关系。目前它通过 ExchangeModel.id 存储在 Customer 数据库表中。但我希望它存储为 ExchangeModel.currencyISO,这是一个 3 个字母的国家代码。

我需要在关系中设置什么才能让 laravel 通过我尝试通过 otherKey 或 key 而不是 primaryKey(id) 定义的其他字段来存储它?

<?php namespace PhunTime\Client\Models;

use Model;
/**
 * Customer Model
 */
class Customer extends Model {
    /**
     * @var string The database table used by the model.
     */
    public $table = 'CUSTOMER';
    public $primaryKey = 'customerNumber';

    /**
     * @var array Relations
     */
    public $belongsTo = ['currency' => ['PhunTime\ExchangeRate\Models\ExchangeModel',
            'key' => 'currencyISO',// EUR,USD,YEN
            'otherKey' => 'currencyISO'

    ]];
}

结构说明:

- Customer
   - id
   - name
   - currency(varchar(3)) <-- relation

 - ExchangeModel
   - id
   - currencyISO

目前 ExchangeModel.id 存储在 Customer.currency

我希望 ExchangeModel.currencyISO 存储在 Customer.currency Customer.currency 已经是一个适合接受它的 varchar 字段。

目前,如果我type:relation在 fields.yaml 中指定,ExchangeModel.id无论我指定如何存储它,它都会存储它。使用 otherKey、foreignKey、key,没有什么能帮助改变 octobers 的想法。

目前我正在使用type: dropdown我填充的一个,getcurrencyOptions()但在我看来,这是一个不太理想的情况。

4

0 回答 0