cakephp上的内部联接3

时间:2017-04-20 05:58:06

标签: cakephp join

好吧,我需要做一个内部连接从2个表,但总是获得相同的错误,我从postman发送这个,我试图收到id一个执行内部连接与这两个表,现在使用这个例子来自documentation

{
  "message": "Failed calling Cake\\ORM\\Query::jsonSerialize()",
  "url": "/users/userdata",
  "code": 500
}

错误日志:

2017-04-20 01:50:24 Error: [Exception] Failed calling Cake\ORM\Query::jsonSerialize()
Request URL: /users/userdata
Stack Trace:
#0 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\View\JsonView.php(0): json_encode()
#1 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\View\SerializedView.php(98): Cake\View\JsonView->_serialize(Array)
#2 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\View\JsonView.php(107): Cake\View\SerializedView->render(NULL, NULL)
#3 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Controller\Controller.php(623): Cake\View\JsonView->render(NULL, NULL)
#4 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(125): Cake\Controller\Controller->render()
#5 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\ActionDispatcher.php(93): Cake\Http\ActionDispatcher->_invoke(Object(App\Controller\UsersController))
#6 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\BaseApplication.php(78): Cake\Http\ActionDispatcher->dispatch(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#7 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Http\BaseApplication->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#8 C:\xampp\htdocs\dekma_backend\vendor\ozee31\cakephp-cors\src\Routing\Middleware\CorsMiddleware.php(28): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#9 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cors\Routing\Middleware\CorsMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#10 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Routing\Middleware\RoutingMiddleware.php(59): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#11 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\RoutingMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#12 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Routing\Middleware\AssetMiddleware.php(88): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#13 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Routing\Middleware\AssetMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#14 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Error\Middleware\ErrorHandlerMiddleware.php(92): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#15 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\Runner.php(65): Cake\Error\Middleware\ErrorHandlerMiddleware->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response), Object(Cake\Http\Runner))
#16 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\Runner.php(51): Cake\Http\Runner->__invoke(Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#17 C:\xampp\htdocs\dekma_backend\vendor\cakephp\cakephp\src\Http\Server.php(80): Cake\Http\Runner->run(Object(Cake\Http\MiddlewareQueue), Object(Cake\Http\ServerRequest), Object(Cake\Http\Response))
#18 C:\xampp\htdocs\dekma_backend\webroot\index.php(38): Cake\Http\Server->run()
#19 {main}

控制器:

  public function userdata()
    {

        $query = $this->Users->find('all')
            ->join([
                'subsection' => [
                    'table' => 'subsections',
                    'type' => 'inner',
                    'conditions' => 'users.subsection_id = subsections.id',
                ],
                'city' => [
                    'table' => 'cities',
                    'type' => 'INNER',
                    'conditions' => 'users.city_id = cities.id',
                ]
            ]);

        //$user = $this->Users->get($id);

        $this->set([
            'success' => true,
            'data' => $query,
            '_serialize' => ['success', 'data']
        ]);
    }

我需要的Mysql是这样的:

SELECT * FROM mydb.users
INNER JOIN subsections on users.subsection_id = subsections.id
INNER JOIN cities on users.city_id = cities.id;

我更改了版本php nos work但是select不适用于表子部分和城市

enter image description here

1 个答案:

答案 0 :(得分:1)

请在查询后添加->toArray();,如下所示:

$query = $this->Users->find('all')
            ->join([
                'subsection' => [
                    'table' => 'subsections',
                    'type' => 'inner',
                    'conditions' => 'users.subsection_id = subsections.id',
                ],
                'city' => [
                    'table' => 'cities',
                    'type' => 'INNER',
                    'conditions' => 'users.city_id = cities.id',
                ]
            ])->toArray();

检查一下。我希望它会对你有所帮助。