1

私はyii+extjsでプロジェクトを作成しています。pollidとpollQuestionを含むポーリングテーブルがあります。オプションテーブルにはpollidとオプションがあります。現在、質問の公開中に、ポーリングテーブルから質問を取得し、オプションテーブルからその質問のオプションを取得しています。このデータをjson_encoded形式で送信します。私は次のように機能を設計していました-

public function actionCreate()
{
    $model=new poll();
    $model->pollId=4;
    $record1=poll::model()->findByPk($model->pollId); 
    //$data = $record1->getAttributes();
    $data= $record1->getAttributes(array('pollId','pollQuestion'));
     foreach ($record1->polloptions as $option) 
     { 
        $data = array_merge($data, $option->with('pollId')-                      >getAttributes());
     }
      //echo $data;
      echo CJSON::encode($data);
}

オプションテーブルには、同じ質問に対して複数のオプションがあります。しかし、上記の方法では、同じ質問のすべてのオプションを表示するのではなく、オプションテーブルに挿入された最後のオプションのみを表示しています。同じ質問のすべてのオプションを表示する方法。助けてください。

4

1 に答える 1

0

あなたが試すことができるかもしれません:

public function actionCreate()
{
    $model=new poll();
    $model->pollId=4;
    $record1=poll::model()->findByPk($model->pollId); 
    //$data = $record1->getAttributes();
    $data= $record1->getAttributes(array('pollId','pollQuestion'));
     foreach ($record1->polloptions as $option) 
     { 
        $optionArray = $option->getAttributes()
        //if the 2 arrays havn't the same indexes
        //this way u keep the key indexes
        $data = $data + $optionArray;
        //if the 2 arrays could have the same indexes
        $data = array_push($data, $optionArray);
     }
      //echo $data;
      echo CJSON::encode($data);
}
于 2012-12-07T10:39:02.063 に答える