2016-05-07 6 views
0

私は、クエリの結果を取得した後、私は問題私は私のテーブルクラス デバッグ($クエリ)に参加し、関連テーブルへのアクセスがあります。今、私はアクセスすることができます知っている異なる方法

src\Model\Table\ComlibsTable.php (line 30) 
     [ 
     (int) 0 => object(App\Model\Entity\Comlib) { 

    'id' => (int) 1, 
    'question' => 'how to kill someone?', 
    'answer' => (int) 2, 
    'asked' => (int) 90, 
    'tags' => 'kill,proffesional killer', 
    'created' => null, 
    'modified' => null, 
    'answers' => [ 
     (int) 0 => object(App\Model\Entity\Answer) { 

      'id' => (int) 1, 
      'question_id' => (int) 1, 
      'answer' => 'the crackpot will attack the will of the darkness and then i starve', 
      'rate' => (float) 11.2, 
      'view' => (int) 22, 
      'helpful' => '11|22', 
      '[new]' => false, 
      '[accessible]' => [ 
       '*' => true 
      ], 
      '[dirty]' => [], 
      '[original]' => [], 
      '[virtual]' => [], 
      '[errors]' => [], 
      '[invalid]' => [], 
      '[repository]' => 'Answers' 

     } 
    ], 
    '[new]' => false, 
    '[accessible]' => [ 
     '*' => true 
    ], 
    '[dirty]' => [], 
    '[original]' => [], 
    '[virtual]' => [], 
    '[errors]' => [], 
    '[invalid]' => [], 
    '[repository]' => 'Comlibs' 

} 
] 

をComlibエンティティは次のようになります:$ query [0] ['question']; しかし、私は$ query-> questionのようなエンティティにアクセスする別の方法があるのだろうか。 と私は答えエンティティにアクセスする方法を、 私のコントローラのコード:任意の助けOK

+0

あなたのモデルの関連性とあなたのComlibs-> LFM関数 – rrd

+0

をモデルアソシエイトといいますか?回答エンティティを意味する場合は、その中に何も関数のないエンティティのケーキPHP構造だけはありません。Comlibモデルは、モデル内にinitialize関数を持っています。それでおしまい –

答えて

0

ため

<?php 
namespace App\Controller; 

use App\Controller\AppController; 

class ComlibsController extends AppController { 

    public function index() { 

    } 

    public function getResult(){ 

     $this->viewBuilder()->layout('comlib'); 
     $live_req = $this->request->data['searchBox']; 
     $query = $this->Comlibs->LFM($live_req); 

     $this->set('question',$query[0]['question']); // does work 
     $this->set('answer',$query['answers'][0]['id']); // does not work 

    } 
} 

LFM機能

public function LFM($live_req) { 

$query = $this->find()->where(['question' => $live_req])->contain(['Answers']); 
$query = $query->toArray(); 
debug($query); 
//include 5 answer 
return $query; 

    } 
} 

TNX私は以来、さまざまな方法を試した後、それを考え出しましたそれは正しい配列で配列にアクセスすることでした。それは次のような配列でした:

$this->set('answer',$query[0]['answers'][0]['id']); 

これで値にアクセスできますが、これは配列プロパティにアクセスする正しい方法ではないようですが、別の方法がありますか?どうすれば$ query [0]から[0]を削除できますか?

関連する問題