I use limit in GridView but show all record - yii

$Property = Property::find()->limit(1)->orderBy(['id' => SORT_DESC]);
I use this query with GridView but show all record
When I use this query
$Property = Property::find()->limit(1)->orderBy(['id' => SORT_DESC])->all();
show error
The "query" property must be an instance of a class that implements the QueryInterface e.g. yii\db\Query or its subclasses.

Read the documentatio here:
http://www.yiiframework.com/doc-2.0/guide-output-data-providers.html#active-data-provider
Any existing limit and offset clauses will be overwritten by the
pagination request from end users (through the pagination
configuration).
Maybe try something like this:
$dataProvider = new ActiveDataProvider([
'query' => $Property ,
'pagination'=>[
'limit'=>1
]
]);
Read also detailed about pagination:
http://www.yiiframework.com/doc-2.0/guide-output-pagination.html
And if you only want to display 1 row from a table, maybe you should check DetailView instead of GridView.

Related

Yii2 - Custom Pagination

What is the right way to have variable pagination in Yii2?
I mean by this, that I want the user to be able to give the number of items in a page while sending an API request.
I know about setting the pageSize in the dataProvider.
$dataProvider = new SqlDataProvider([
'sql' => 'SELECT * FROM user WHERE status=:status',
'params' => [':status' => 1],
'pagination' => [
'pageSize' => 20,
],
]);
But my question is about anything that is built in that allows the user to send the pageSize through the request? Is there anything built-in to perform this function?
Leave empty the pagination field in the dataProvider and just add the per-page GET parameter in your calls:
http://your_url/controlller/action?per-page=20
More info here.

Yii 1 Pagination takes every params user inserted

I have my pagination url set to
www...com/category/detail.html?page=2
For which my code is,
$dataProvider = new CActiveDataProvider('Page', array('criteria' => array('condition' => 'status=1', 'condition' => 'category_id=' . $categoryObject -> id, 'order' => 'postDate DESC'), 'pagination' => array('pageSize' => 4,'pageVar'=>'page'), ));
$dataProvider->getData();
var_dump $dataProvider->totalItemCount;
I am getting the exact data counts and my pagination url seems working. I have my URL rule configured as
'index'=>'site/index',
'contact'=>'site/contact',
'privacy'=>'site/privacy',
'sitemap.xml'=>'site/sitemap',
'<category:\w+>' => 'category/detail',
'<category>/page/<page:\d+>' => 'category/detail',
'<category>'=>'category/detail',
'<category:\w+>/<postTitle:.+>' => 'category/post',
'<category:\w.+>/<postTitle:.+>'=>'category/post',
my auto generated pagination URl are working fine but,
If I manually insert url something like
www......com/category/detail.html?Page_page&page=2
www......com/category/detail.html?Page_pizza&page=2
or any stupid things I can put they navigates to the page.
Now, here I want to remove these extra parameters or I want my pagination url to be strict to
www...com/category/detail.html?page=2
and if I put any additional params I want an error page.
I have been working in this for 2 weeks and tried every possible ways I could.
You should set the "params" property of the pagination to an empty array:
'pagination' => array('pageSize' => 4,'pageVar'=>'page', 'params' => array()),
This will avoid exclude any GET parameters from pagination links(if you need any included, add their name there).
To throw an error, you need to check for the parameters in your action.
// remove valid prameters from get array.
$arr = array_diff_key($_GET, array_flip(array('page', 'postTitle', 'category')));
// if still there are parameters left, throw an error.
if(count($arr) > 0) {
throw new CHttpException('invalid parameter');
}

Creating Prestashop back-office module with settings page

I'm creating a back-office module for Prestashop and have figured out everything except the best way to display the admin page. Currently I'm using the renderView() method to display the content of view.tpl.
I would like to display a table with values and an option to add a new row. Should I just create it in the view.tpl or is there a better way? I've seen the renderForm() method but haven't figured out how it works yet.
The biggest question I have is, how do I submit content back to my controller into a specific method?
ModuleAdminController is meant for managing some kind of records, which are ObjectModels. Defauly page for this controller is a list, then you can edit each record individually or view it's full data (view).
If you want to have a settings page, the best way is to create a getContent() function for your module. Besides that HelperOptions is better than HelperForm for this module configuration page because it automatically laods values. Define the form in this function and above it add one if (Tools::isSubmit('submit'.$this->name)) - Submit button name, then save your values into configuration table. Configuration::set(...).
Of course it is possible to create some sort of settings page in AdminController, but its not meant for that. If you really want to: got to HookCore.php and find exec method. Then add error_log($hook_name) and you will all hooks that are executed when you open/save/close a page/form. Maybe you'll find your hook this way. Bettter way would be to inspect the parent class AdminControllerCore or even ControllerCore. They often have specific function ready to be overriden, where you should save your stuff. They are already a part of execution process, but empty.
Edit: You should take a look at other AdminController classes, they are wuite simple; You only need to define some properties in order for it to work:
public function __construct()
{
// Define associated model
$this->table = 'eqa_category';
$this->className = 'EQACategory';
// Add some record actions
$this->addRowAction('edit');
$this->addRowAction('delete');
// define list columns
$this->fields_list = array(
'id_eqa_category' => array(
'title' => $this->l('ID'),
'align' => 'center',
),
'title' => array(
'title' => $this->l('Title'),
),
);
// Define fields for edit form
$this->fields_form = array(
'input' => array(
array(
'name' => 'title',
'type' => 'text',
'label' => $this->l('Title'),
'desc' => $this->l('Category title.'),
'required' => true,
'lang' => true
),
'submit' => array(
'title' => $this->l('Save'),
)
);
// Call parent constructor
parent::__construct();
}
Other people like to move list and form definitions to actual functions which render them:
public function renderForm()
{
$this->fields_form = array(...);
return parent::renderForm();
}
You don't actually need to do anything else, the controller matches fields to your models, loads them, saves them etc.
Again, the best way to learn about these controller is to look at other AdminControllers.

How to Send Additional Data in Kendo Grid's AJAX Create

I am able to send additional data to a Kendo grid's AJAX Read action using a pattern like so...
#(Html.Kendo().Grid<ModelClass>()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("Read", "Controller").Data("getAdditionalData"))
function getAdditionalData() {
return {
AdditionalData: 'data'
};
}
public ActionResult Read([DataSourceRequest] DataSourceRequest request, String additionalData)
{
}
... but how does one send additional data to the Create action? In my scenario, creation requires information that is not entered by the user, but is available both in the view and as the return from getAdditionalData(). I've tried passing it using the following, but it is not being received by the action method.
.Create(create => create.Action("Create", "Controller").Data("getAdditionalData"))
public ActionResult Create([DataSourceRequest] DataSourceRequest request, ModelClass model, String additionalData)
How is this done? Or how else can additional data be passed to the AJAX create action?
Ref Docs:
http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-binding#pass-additional-data-to-the-action-method
http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/ajax-editing
Update with Solution:
As it happens, there cannot be a field name (e.g. "additionalData") duplication between the grid's view model (e.g. "ModelClass") and the client-side data model. That was not apparent in the question as I sanitized the code to post. Not sure if this is happening at the javascript, MVC or Telerik layer.
The solution was to change the client-side data model's field name to be different that the field name in the grid's view model.
Update with Another Solution:
Another option is to set the default value of the field in the grid's view model for create operations like so:
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => {
model.Field(x => x.Id) // Set default value for grid model field
.DefaultValue(Model.DefaultId); // as provided by controller in view model.
})
I've got the same problem and I resolve it.
public ActionResult Create([DataSourceRequest] DataSourceRequest request, ModelClass model, String additionalData)
in this line, change String for string and additionalData for AdditionalData, the same word you have in your js.
One more things, in the clause .Events of DataSource, don't put at the end .RequestEnd("getAdditionalData")
Normally it will work.
Hope it helps!

JCarousel widget on Yii framework don't load image

I'm trying to use JCarousel widget on Yii framework.
My JCarousel must show images of specific user that belong to a specific gallery (for that user).
I retrieve the information about the images from a MySQL DB, in the specific from Photos table, where gallery_id have a determined value. The column from Photos table that contain the name of the photos is "path".
In the code I have written this:
$DataProvider = new CActiveDataProvider('Photos', array('criteria'=>array(
'condition'=>'gallery_id = :id',
'params'=>array(':id'=>$gallery->id),
),));
$this->widget('ext.JCarousel.JCarousel', array(
'dataProvider' => $DataProvider,
'thumbUrl' => '"/images/upload/".$data->id."_".$data->path',
'imageUrl' => '"/images/upload/".$data->id."_".$data->path',
'target' => 'big-gallery-item',
'vertical' => false,
));
With this code I get, on the div, the written message: "No result find"
Where I did i go wrong?
Check your dataProvider. Try get results via $DataProvider->getData() and check that you photos exists.
I do not think that the error in the extension, most likely you do not select data from the database.