2017-04-20 20 views
0

私は内側の結合fron 2テーブルを行う必要がありますが、常に同じエラーを取得する、私は郵便配達員からこれを送信する、私はIDを受信しようとしていますこの2つのテーブル、今documentationcakephpの内部結合3

{ 
    "message": "Failed calling Cake\\ORM\\Query::jsonSerialize()", 
    "url": "https://stackoverflow.com/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; 

私は仕事NOSバージョンのPHPを変更するが、選択は、テーブルのサブセクションや都市

enter image description here

+0

あなたはあなたの最終的なSQLコマンドを検査できますか?また、指定した名前空間/パスをコントローラの中に入れましたか? – marmeladze

+0

郵便配達員からjsonデータを送信するときにsqlコマンドを検査できません。これは問題です。 – CoolLife

+0

いいえSQLコンソールの中でそのクエリを検査して、エラーがあるかどうかを調べることができますか? – marmeladze

答えて

0

のために働いていません。このようなクエリの後->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(); 

確認してください。それがあなたを助けることを願っています。