我正在使用CakePHP 3.x开展项目。
我有UserAddress
,ServiceRequests
,Service
型号。
service/view/$id
上有一个按钮,点击后会要求用户从service-requests/serviceArea
中选择一个地址列表,其中包含用户添加的地址列表。 service-requests/serviceArea
视图将包含select
按钮,点按此按钮会在add
控制器中调用ServiceRequests
操作并传递两个参数serviceId
和userAddressId
这是我创建的serviceArea函数。
public function serviceArea($id = null)
{
public $uses = array('UserAddress');
$service = $id;
$query = $userAddresses->find('all')
->where(['UserAddresses.user_id =' => $this->Auth->user('id')]);
$this->set(compact('userAddresses'));
$this->set('_serialize', ['userAddresses']);
}
如何显示地址并将$service
参数传递到serviceArea
视图。
我是CakePHP的新手,所以如果您认为问题不完整,我们将不会对其进行任何编辑,而不是进行投票。
谢谢。
编辑2
感谢您的回答@jazzcat
根据您的代码更改我的代码并访问http://domain.com/service-requests/service-area/$id
。它显示错误为
Record not found in table "service_requests"
并指向ServiceRequestsController
行 33
包含第33行的ServiceRequestController
是
<?php
namespace App\Controller;
use App\Controller\AppController;
/**
* ServiceRequests Controller
*
* @property \App\Model\Table\ServiceRequestsTable $ServiceRequests
*/
class ServiceRequestsController extends AppController
{
/**
* isAuthorized method
*
*/
public function isAuthorized($user)
{
$action = $this->request->params['action'];
// The add and index actions are always allowed.
if(in_array($action, ['index', 'add', 'serviceRequests'])) {
return true;
}
// All other actions require an id.
if (empty($this->request->params['pass'][0])) {
return false;
}
// Check that the service request belongs to the current user.
$id = $this->request->params['pass'][0];
$serviceRequest = $this->ServiceRequests->get($id); // line : 33
if($serviceRequest->user_id == $user['id']) {
return true;
}
return parent::isAuthorized($user);
}
/* Other actions */
}
?>
答案 0 :(得分:1)
这对我有用。
刚刚在serviceArea
方法
isAuthorized
操作名称
if(in_array($action, ['index', 'add', 'serviceArea'])) {
return true;
}
它按预期正常工作。
答案 1 :(得分:0)
您的代码有很多错误。请阅读docs
表名为user_addresses还是user_address? 你好像混在一起。
假设您的表名为 user_addresses
,以下是正确的方法public function serviceArea($id = null)
{
$this->loadModel('UserAddresses');
$userAddresses = $this->UserAddresses->find('all')
->where(['UserAddresses.user_id =' => $this->Auth->user('id')]);
// If you want to filter on the serviceArea ID aswell
if($id)
$userAddresses->andWhere(['id' => $id]);
// Setting SerivceArea ID to compact makes it available in view.
$serviceAreaId = $id;
$this->set(compact('userAddresses', 'serviceAreaId'));
$this->set('_serialize', ['userAddresses']);
}
此片段:
$id = $this->request->params['pass'][0];
$serviceRequest = $this->ServiceRequests->get($id); // line : 33
只检查传递给方法的第一个参数是否存在于ServiceRequests中。
(那个参数可以是任何东西,你必须在创建该控制器中的所有方法时记住这一点,也就是说至少......坏)
我假设表示service_requests表与users表相关联,而service_requests表中存在user_id列。
如果是这种情况,这应该有效:
public function isAuthorized($user)
{
$action = $this->request->params['action'];
// The add and index actions are always allowed.
if(in_array($action, ['index', 'add'])) {
return true;
}
// Is not authorized if an argument is not passed to the method.
// Don't know why you'd want this , but sure.
if (empty($this->request->params['pass'][0])) {
return false;
}
// Check that the service request belongs to the current user.
$user_id = $this->Auth->user('id');
$serviceRequest = $this->ServiceRequests->find()->where(['ServiceRequests.user_id' => $user_id])->first();
if(!empty($serviceRequest)) {
return true;
}
return parent::isAuthorized($user);
}