CakePHP 3:显示来自其他模型的数据并通过操作在 url 中传递参数
CakePHP 3 : display data from other model and pass parameter in url from action
我正在使用 CakePHP 开发一个项目 3.x。
我有 UserAddress
、ServiceRequests
、Service
个模型。
service/view/$id
上有一个按钮,单击该按钮会要求用户从 service-requests/serviceArea
获取 select 地址,其中包含用户添加的地址列表。 service-requests/serviceArea
视图将包含一个 select
按钮,单击该按钮将在 ServiceRequests
控制器中调用 add
操作,并传递两个参数 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 的新手,所以如果您认为问题不完整,请对其进行任何编辑,而不是否决。
谢谢。
Edit 2
感谢@jazzcat 的回答
根据您的代码更改我的代码并访问 http://domain.com/service-requests/service-area/$id
。它显示错误为
Record not found in table "service_requests"
并指向第 33
行的 ServiceRequestsController
包含第 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 */
}
?>
你的代码有很多错误。请阅读 docs
table 是 user_addresses 还是 user_address ?
你似乎把两者混为一谈了。
假设您的 table 被命名为 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 table 与用户 table 关联并且存在 user_id 列在 service_requests table.
如果是这种情况,这应该可行:
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);
}
这对我有用。
刚刚在 isAuthorized
方法中添加了 serviceArea
操作名称
if(in_array($action, ['index', 'add', 'serviceArea'])) {
return true;
}
它按预期工作正常。
我正在使用 CakePHP 开发一个项目 3.x。
我有 UserAddress
、ServiceRequests
、Service
个模型。
service/view/$id
上有一个按钮,单击该按钮会要求用户从 service-requests/serviceArea
获取 select 地址,其中包含用户添加的地址列表。 service-requests/serviceArea
视图将包含一个 select
按钮,单击该按钮将在 ServiceRequests
控制器中调用 add
操作,并传递两个参数 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 的新手,所以如果您认为问题不完整,请对其进行任何编辑,而不是否决。
谢谢。
Edit 2
感谢@jazzcat 的回答
根据您的代码更改我的代码并访问 http://domain.com/service-requests/service-area/$id
。它显示错误为
Record not found in table "service_requests"
并指向第 33
行的ServiceRequestsController
包含第 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 */
}
?>
你的代码有很多错误。请阅读 docs
table 是 user_addresses 还是 user_address ? 你似乎把两者混为一谈了。
假设您的 table 被命名为 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 table 与用户 table 关联并且存在 user_id 列在 service_requests table.
如果是这种情况,这应该可行:
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);
}
这对我有用。
刚刚在 isAuthorized
方法中添加了 serviceArea
操作名称
if(in_array($action, ['index', 'add', 'serviceArea'])) {
return true;
}
它按预期工作正常。