0

In Yii 2.0, I was trying to encode or print a query result to XML format in order to access to these data by using REST, but when I push the result of the query (located it in an ActiveController) into de view, I get a huge html code and the query result inside of it.

This is my ActiveController, and my function actionSql() to render the sql.php view:

class EventController extends ActiveController{

    public $modelClass = 'app\models\Event';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ]);
    }


    public function actionSql(){
        $rows = (new Query())
        ->select(['ta.id', 'e.name', 'e.desc', 'u.place', 'u.date'])
        ->from(['ta' => 't_area'])
        ->innerJoin('event e', 'ta.id = e.t_area_id')
        ->innerJoin('ubic u', 'e.id = u.event_id')
        ->where([
            'ta.id' => 1,
            'dayname(u.date)' => 'Monday'
            ])
        ->all();

        return $this->render( 'sql', [
            'rows' => $rows,
        ]);
    }
}

===========================
sql.php (The View)

<?php
    use yii\helpers\Html;
    use yii\grid\GridView;
?>
<?php 
    foreach ($filas as $fila){
        foreach ( $fila as $key=>$value ){
            echo $key . ' => ' . $value . '<br>';
        } 
        echo '<br>';
    }
 ?>

Thank you so much. Hope to find the answer!

1
  • try to return rows without rendering them, return $rows Commented Jun 30, 2015 at 8:59

2 Answers 2

3

You could use renderPartial() to render your view without applying layout.

But the format you are using in your sql view is not XML... You should try instead :

public function actionSql()
{
    $rows = ....... ;

    \Yii::$app->response->format = \yii\web\Response::FORMAT_XML;
    return $rows;
}

Read more about response formats in Yii2 Cookbook.

And you should also read this : RESTful Web Services with Yii2

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you so much for your time buddies. The solution is to return rows variable:

public function actionSql(){
    $rows = (new Query())
    ->select(['ta.id', 'e.name', 'e.desc', 'u.place', 'u.date'])
    ->from(['ta' => 't_area'])
    ->innerJoin('event e', 'ta.id = e.t_area_id')
    ->innerJoin('ubic u', 'e.id = u.event_id')
    ->where([
        'ta.id' => 1,
        'dayname(u.date)' => 'Monday'
        ])
    ->all();
    //The solution is!: 
    return $rows;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.