我很难理解如何使用条件格式检索关联数据。我正在使用 CakePHP 3.7.9。
餐桌产品
id
name
customer_code
表订单
id
name
date
表 item_orders
id
order_id
name
product_id
description
在(另一个控制器的)视图中,我有两个select
控件。第一个填充id|name
了现有订单。当用户选择一个时,select
应按照以下条件填充第二个控件:
如果product_id
不为空 ->name (customer_code)
在products
表中检索
否则使用该description
值
我ajax
用来将电流发送order_id
到控制器,该控制器应该发回填充第二个所需的 html select
:
<?php $this->Html->scriptStart(['block' => 'script', 'inline' => false]); ?>
$(document).on('change', '#order-id', function() {
$.ajax({
type: "POST",
headers: { 'X-CSRF-Token': <?= json_encode($this->request->getParam('_csrfToken')); ?> },
url: '<?php echo Router::url(array('controller' => 'ItemDeliveryNotes', 'action' => 'getOrderItems'));?>',
data: { 'orderId': $('#order-id').val() },
success: function(response) {
$('#itemOrders').html(response.data.itemOrders);
}
});
});
<?php $this->Html->scriptEnd(); ?>
public function getOrderItems()
{
if ($this->request->is(['ajax', 'post']))
{
$id = $this->request->getData('orderId'); // <-- fixed
$items = $this->getItems($id);
$combo = [];
foreach ($items as $key => $value)
{
$combo[] = "<option value='" . $key . "'>" . $value . "</option>";
}
$data = ['data' => ['itemOrders' => $combo]];
return $this->json($data);
}
}
private function getItems($id = null)
{
$items = ???; // <-- here I need to retrieve the list as above
return $items;
}
我能够从单一来源获取数据,但在这种情况下,我不明白如何编写查询。
莫回无