如何在查找列表中使用相同的 table 两次,但在 cakePHP 3.x 中使用不同的显示字段

How to use the same table twice with find list but with different displayFields in cakePHP 3.x

我有一个国家Table,我可以在其中找到字段 'name' 和 'currency'。

目前,我有以下 Tables 定义:

class ProducsTable extends Table {

    public function initialize(array $config) {
        $this->table('products');
        $this->displayField('name');
        $this->primaryKey('id');
        $this->addBehavior('Timestamp');

        $this->hasMany('Productprices', [
            'foreignKey' => 'product_id',
        ]);

        $this->belongsTo('Countries', [
            'foreignKey' => 'country_id',
        ]);

    }
}

class ProductpricesTable extends Table {

    public function initialize(array $config) {
        $this->table('productprices');
        $this->displayField('name');
        $this->primaryKey('id');
        $this->addBehavior('Timestamp');

        $this->belongsTo('Products', [
            'foreignKey' => 'product_id',
        ]);

    }
}

class 国家/地区Table 扩展 Table {

    public function initialize(array $config) {
        $this->table('countries');
        $this->displayField('name');
        $this->primaryKey('id');
        $this->addBehavior('Timestamp');

        $this->hasMany('Products', [
            'foreignKey' => 'product_id',
        ]);

        $this->hasMany('Productprices', [
            'foreignKey' => 'product_id',
        ]);

    }
}

所以我有一个以国家为原产地和与货币相关的价格列表的产品。

当我创建产品时,我有以下表格:

<?= $this->Form->input('name', ['label' => __("Name :")]); ?>

<?= $this->Form->input('country_id', ['label' => __("Origin :")]); ?>

<?= $this->Form->input('pricelist.0.value', ['label' => __("Price :")]); ?>

<?= $this->Form->input('pricelist.0.country_id', ['label' => __("Currency :")]); ?>

当然,两者 input('country_id') 都显示名称列表,而我希望第二个显示货币列表。

怎么办?

我基本上想创建一个名为 CurrenciesTable 的 CountriesTable 的副本,指向相同的 table 'countries' 并将 Productprices 关联到 CurrenciesTable ....但听起来很奇怪...不是吗?

如果要创建下拉菜单,必须将其类型设置为 select

<?= $this->Form->input('country_id', ['type' => 'select, 'label' => __("Origin :")]); ?>

要为其添加选项,请使用:

<?= $this->Form->input('country_id', ['type' => 'select, 'options' => $list, 'label' => __("Origin :")]); ?>

$list 必须是数组。这可以从模型调用:$this->Model->find('list');。在您的控制器中使用此代码。我猜你知道怎么做。

祝你好运!