gpt4 book ai didi

php - CakePHP:将DisplayField设置为虚拟字段

转载 作者:行者123 更新时间:2023-11-29 06:31:10 26 4
gpt4 key购买 nike

我正在遵循 CakePHP 3.7 的文档,以便在选择输入控件中显示虚拟字段。

DeliveryNotesTable.php

public function initialize(array $config)
{
parent::initialize($config);

$this->setTable('delivery_notes');
$this->setDisplayField('reference');
$this->setPrimaryKey('id');

$this->belongsTo('Customers', [
'foreignKey' => 'customer_id'
]);
$this->belongsTo('ShipmentReasons', [
'foreignKey' => 'shipment_reason_id'
]);
$this->belongsTo('ShipmentMethods', [
'foreignKey' => 'shipment_method_id'
]);
$this->hasMany('ItemsDeliveryNotes', [
'foreignKey' => 'delivery_note_id'
]);
$this->hasMany('ItemOrders', [
'foreignKey' => 'delivery_note_id'
]);
}

protected function _getReference()
{
return $this->deliveryNoteNumber . ' ' . $this->deliveryNoteDate;
}

我在 DeliveryNote.php 中公开虚拟字段:

class DeliveryNote extends Entity
{
protected $_accessible = [
'customer_id' => true,
'shipment_reason_id' => true,
'deliveryNoteNumber' => true,
'deliveryNoteDate' => true,
'shipment_method_id' => true,
'number_of_packages' => true,
'weight' => true,
'customer' => true,
'shipment_reason' => true,
'shipment_method' => true,
'items_delivery_notes' => true
];

protected $_virtual = [
'reference'
];
}

在我的 Controller 中,我加载关联的数据:

public function add($id)
{
$itemOrder = $this->ItemOrders->newEntity();
$itemOrder['order_id'] = $id;
if ($this->request->is('post')) {
$itemOrder = $this->ItemOrders->patchEntity($itemOrder, $this->request->getData());
if ($this->ItemOrders->save($itemOrder)) {
$this->Flash->success(__('The {0} has been saved.', 'Item Order'));

return $this->redirect(['controller' => 'orders', 'action' => 'index']);
}
$this->Flash->error(__('The {0} could not be saved. Please, try again.', 'Item'));
}
$orders = $this->ItemOrders->Orders->find('list', ['limit' => 200]);
$itemOrdersTypes = $this->ItemOrders->ItemOrdersTypes->find('list', ['limit' => 200]);
$products = $this->ItemOrders->Products->find('list', ['limit' => 200, 'order' => ['Products.code' => 'ASC']]);
$itemOrdersStates = $this->ItemOrders->ItemOrdersStates->find('list', ['limit' => 200]);
$deliveryNotes = $this->ItemOrders->DeliveryNotes->find('list');
$this->set(compact('itemOrder', 'orders', 'itemOrdersTypes', 'products', 'itemOrdersStates', 'deliveryNotes'));
}

最后,在模板中我创建选择控件:

<?= $this->Form->control('delivery_note_id', ['options' => $deliveryNotes, 'empty' => true]); ?>

但它是空的,没有显示任何项目。相反,如果我设置:

$this->setDisplayField('id');

或任何其他字段,选择控件已正确填充。我在创建这个虚拟领域时错过了一些东西吗?

最佳答案

您的 protected 函数_getReference()方法必须在DeliveryNote实体类中设置

阅读:https://book.cakephp.org/3.0/en/orm/entities.html#creating-virtual-fields

关于php - CakePHP:将DisplayField设置为虚拟字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56201136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com