How to LIMIT ArrayDataProvider in yii2? - arrays

I am trying create ArrayDataProvider. I am calling dataProvider like this:
$dataProvider = new ArrayDataProvider([
'allModels' => $query->from('posts')->all(),
'pagination' => [
'pageSize' => 10,
],
]);
Here I am using pageSize. When I run this code in sql not showing LIMIT, but pagination is working. I have more than 100 posts and all of them loading in all time. Generated SQL Query: SELECT * FROMposts.
What I must do to solve this.
Thank you!

Try this
'pageSizeLimit' => [5,100],

Related

PHP Laravel Sage One API says invoice lines are required when they are there and not empty

I am currently integrating an application with the Sage One API and am having a problem. The API says that invoice lines may not be empty, but the data is there. What am I doing wrong?
Here is my method of getting the line items:
$lineItems = [];
foreach ($invoiceItems as $invoiceItem){
$lineItems[] =
[
"SelectionId" => "35175771",
"TaxTypeId" => "6194291",
"Description" => $invoiceItem->name,
"Quantity" => $invoiceItem->duration,
"UnitPriceExclusive" => $invoiceItem->total_excl_vat
];
}
foreach ($invoiceOtherItems as $invoiceOtherItem){
$lineItems[] =
[
"SelectionId" => "35175771",
"TaxTypeId" => "6194291",
"Description" => $invoiceOtherItem->otherItem->name,
"Quantity" => $invoiceOtherItem->quantity,
"UnitPriceExclusive" => $invoiceOtherItem->total_excl_vat
];
}
//dd($lineItems)
And here is the part in the post data to the API where I post the invoice items (removed majority items for the sake of brevity here):
$invoice = [
"Date" => Carbon::now()->toDateString(),
"Lines" => $lineItems,
"DueDate" => "2021-08-29"
];
Performing a dump and die where I commented the dd returns all the arrays, yet the API is telling me lines are required. Am I doing anything wrong? The code seems correct to me and I can't find anything to help on this matter.
For anyone that might run across this problem here is the solution:
In your response, define your headers like this:
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/json']
And do not use form_params, but use this instead:
'body' => json_encode($invoice)

Validate Laravel Nested Rules in Array

I'm with Laravel and I want to write elegant validation rules :) With this Framework its really easy, but I don't know how to approach this when facing 1:n relationships.
I have two Resources, User and Contact. An User can have multiple Contacts.
So, I want a Form where you can fill all User's fields AND all Contact's information.
To do that, I would like to write a Request like this:
UserRequest:
public function rules()
return [
'name' => 'required|string',
'email' => 'required|email|unique:exists:users,id',
'contacts' => 'array',
'contacts.*' => new ContactRequest() // This is the problem
]
My question is: How can I apply this type of validation? Specifically when using array, how can I make a Modular Validation to apply validations of nested Resources? Or should I develop a ContactRule instead?
Edit:
I want that front end send form like this:
` // POST: users
{
'name': 'UserName',
'email': 'user#mail.com'
'contacts': [
[
'email' => 'contac_1#mail.com',
'contact_type_id => 1
],
[
'email' => 'contac_2#mail.com',
'contact_type_id => 2
],
}
`
Thats all,
Thx!
We have an API with 100's of results in each request or perhaps post/patch.
We still use:
'data.relationships.users.data.*.id' => [
'string',
'unique:api_groups,name,' . ($this->route('group')->id ?? 0),
]
So for you
'contacts.*.email' => 'required|email|unique:exists:users,id'
Works perfectly. It doesn't get more complex or anything.

TYPO3 findAll() return empty data

I'm newbie with TYPO3.
I'm facing with this issue, can not get data from database.
I have a plugin with this configure
\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
'Locations.' . $_EXTKEY,
'Locationsfe',
array(
'Location' => 'list, show, showprice, listprice, listcategory, scan,sharedoffice',
'Category' => 'list, show',
'Pricing' => 'list, show, showprice, listprice, scan, test',
'Template' => 'list, show',
),
// non-cacheable actions
array(
'Location' => '',
'Category' => '',
'Pricing' => 'test',
'Template' => '',
)
);
And in my controller I have this function
public function testAction() {
// Get current language
$currentLanguage = $GLOBALS['TSFE']->sys_language_uid;
$pricing = $this->pricingRepository->findAll();
print_r($pricing);
die('Passed');
}
I also added this line to Constants
plugin.tx_locations.persistence.storagePid = 164
I also created a typo Script
plugin.tx_locations {
view {
templateRootPath = {$plugin.tx_locations.view.templateRootPath}
partialRootPath = {$plugin.tx_locations.view.partialRootPath}
layoutRootPath = {$plugin.tx_locations.view.layoutRootPath}
}
persistence {
storagePid = 164
}
features {
# uncomment the following line to enable the new Property Mapper.
# rewrittenPropertyMapper = 1
}
}
But all of above does not work. Just white page return.
I also read this
extbase repository findAll() returns result null
So, what happen? I don't know why. Can you help me to figure it out please.
Thanks in advance.
Ok i suppose you have created some records from model type pricing in your backend right ;).
Then please try this in your controller:
$this->pricingRepository->setDefaultQuerySettings($this->pricingRepository->createQuery()->getQuerySettings()->setRespectStoragePage(false)); /* here we ignore the storage pid to be sure that we look in the entire system, if this works we have some hints that somthing with the storage pid is wrong */
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->pricingRepository); /*please use this for debugging is much easier with this :D*/
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($this->pricingRepository->findAll());
can you post the debugs please.
sorry i can't comment i don't have the right reputation :(

How to get the json data by yii2

Two question:
1.I use the post method of yii2 to get the json data, sent by front end with ngResource in Angularjs. And I configure the yii2 as follow:
'parsers' => [
'application/json' => 'yii\web\JsonParser',
'text/json' => 'yii\web\JsonParser',
]
But it doesn't work when I use $post = Yii::$app->request->post() to get the data. Additionally, the data is not serialized.
2.I configure the yii2
'response' => [
'formatters' => [
\yii\web\Response::FORMAT_JSON => [
'class' => 'yii\web\JsonResponseFormatter',
'prettyPrint' => YII_DEBUG,
]
]
],
When I find that it is different from
Yii::$app->response->format = Response::FORMAT_JSON;
before return some value. The former doesn't transform the data to json format. I don't know why?
1.I force the contentTye to application/x-www-form-urlencoded;charset=utf-8, it is so silly.
2.Formatters is different from format.

Cakephp recursive find including top model again

I have this cakephp find query:
$results = $this->Instrument->find('all', array('recursive' => 2, 'conditions' => array('OR' => array('Instrument.name' => $tag_search, 'Instrument.nameEN' => $tag_search))));
The Model Instrument has and belongs to many Instrumentbox. I want to find an Instrumentbox with all it's instruments searching for one instrument name with $tag_search.
It returns the Insrtumentboxes that have that instrument properly but it does not include all the instruments agaion in Instrument. Is there a way to tell cakephp to load the "top model" again so that I get the structure
$results = array(
0 => [
'Instrument' => [
'name' => "$tag_search value",
'...' => ...,
'Instrumentbox' => [
0 => [
'...' => '...', //everything else is properly included
'Instrument' => [...] //this is NOT included, how can I include it?
]
]
]
]
The same thing happens when I search it with tags, so imagine in the structure above 'Tag' instead of 'Instrument', then the instruments are included in the Instrumentbox but the tags are not. So the "top model" is not included again in the structure. Do I have to make that by hand or can I make cakephp do it?
Thanks in advance!
You Could try to do this with Contain. Its much better than using recursive over 1 anyway. You can control exactly the results you want to get, which is good for this sort of thing, and generally better for performance.
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
So, untested code:
//model
public $actsAs = array('Containable');
// controller
$this->Instrument->recursive=1;
$this->contain(array('Instrument'=> array('InstrumentBox'=> array('Instrument'))));

Resources