Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,195,362 members, 7,957,969 topics. Date: Wednesday, 25 September 2024 at 06:10 AM |
Nairaland Forum / Science/Technology / Programming / Yii2 Rest API Question (2737 Views)
[php Tutorial] - Building A Rest Api / How To Develop Android And iOS Mobile App In Magento2 With Rest API / Yii2 Development Visualization (2) (3) (4)
Yii2 Rest API Question by nollyj: 12:31am On May 13, 2016 |
According to Yii 2 Rest APi documentation, I have a CountriesController that extends \yii\rest\ActiveController and a corresponding Countries model. This is the code for my Controller class.
When I send a get request, it returns all the countries in my database. My Question is it possible to return my own result from action methods. Like in the actionIndex(), I will like to limit the result to 20 records. I did something like this but it is not working. public function actionIndex(){ I know that I can get all the countries from my database and loop through it and obtain only 20 results but I want to just query for 20 records from database. @pcguru1, @dhtml and other Yii2 gurus |
Re: Yii2 Rest API Question by Nobody: 7:39am On May 13, 2016 |
nollyj:After the find why not call limit and pass the 20 as argument , also you can use a Cdbcriteria I will check the documentation once I reach office. |
Re: Yii2 Rest API Question by tosinhtml: 9:40am On May 13, 2016 |
well, as far as i can remember, i use findAll instead of findOne $model=new Countries; $criteria = new CDbCriteria(); $count=Countries::model()->count($criteria); $pages=new CPagination($count); //Optional - if you choose to add conditions if column value is greater than another value $example_value = //value $criteria->condition = 'example_column > =:example_value'; $criteria->params = array(':example_value'=>$example_value ); // results per page $pages->pageSize=20; $pages->applyLimit($criteria); $models=Countries::model()->findAll($criteria); return $this->render('index', array( 'models'=>$models, 'pages' => $pages, 'count' => $count )); |
Re: Yii2 Rest API Question by Nobody: 12:44pm On May 13, 2016 |
tosinhtml: OP asked for Yii2 not Yii1 |
Re: Yii2 Rest API Question by Nobody: 12:46pm On May 13, 2016 |
Countries::find()->limit(20); Should work |
Re: Yii2 Rest API Question by tosinhtml: 1:04pm On May 13, 2016 |
pcguru1: ok, so Yii2 totally eliminates the CDbCriteria ?? |
Re: Yii2 Rest API Question by nollyj: 1:14pm On May 13, 2016 |
Thanks all. In Yii 2, when you controller class extends \yii\rest\ActiveController for Restful Api not app\controllers, the following methods are created like default
This will map to the corresponding API end-points GET /countries: list all countries HEAD /countries: show the overview information of country listing POST /countries: create a new country GET /countries/AU: return the details of the country AU HEAD /countries/AU: show the overview information of country AU PATCH /countries/AU: update the country AU PUT /countries/AU: update the country AU DELETE /countries/AU: delete the country AU OPTIONS /countries: show the supported verbs regarding endpoint /countries OPTIONS /countries/AU: show the supported verbs regarding endpoint /countries/AU. If you are extending app\controllers, I know you can use Countries::find()->limit(20); but I am working with \yii\rest\ActiveController |
Re: Yii2 Rest API Question by Nobody: 1:19pm On May 13, 2016 |
tosinhtml: Yep much more easier and less syntax look Query yii\db\ActiveQuery |
Re: Yii2 Rest API Question by foldl: 1:57am On May 15, 2016 |
When you extend \yii\rest\ActiveController you do not need to render your result but return it directly. In your example change '$this->render($model);' to 'return $model;'. Also note that since you are using findOne it will return the first record in that table only. In general, you should return any value or object instead of rendering when using rest controllers. You don't need to serialize the data into json as that is done implicitly by the \yii\rest\ActiveController class. When in doubt read the source code! I built a production system with yii2 when it was still in beta. I even found and fixed a bug and sent a pull request which was merged. But I haven't written any code in months so I might be a bit rusty. |
Re: Yii2 Rest API Question by CCsurplus007(m): 12:44pm On May 18, 2016 |
nollyj:Pleasant greetings to you. I sent you a PM , can you kindly respond. I recently got a SI scholarship to study at Lulea and would need few clarifications from you.. . thanks. |
Re: Yii2 Rest API Question by nollyj: 3:18pm On May 18, 2016 |
CCsurplus007: Lulea is Northern Sweden I live in Southern Sweden. I have not been to the Northern Part of Southern. Know also the weather in the North is very harshe during winter than in the south. LuleƄ University of Technology is a nice school and I hope you will enjoy your time there You can ask me question through email if the question is personal. |
(1) (Reply)
Java And Oracle Which Is Best? / Codewars.com Coding Challenges / A Problem With Java Gui Design
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 28 |