typo3 paginate widget starts not with uid 1, starts with the next uid after last uid - widget

I am using the paginate widget (copied the code from sysext, and change the path to my extension). I set it up an in my template like this:
<f:widget.paginate objects="{therapeuts}" as="paginatedtherapeuts" configuration="{itemsPerPage: 9, insertAbove: 1, insertBelow: 1, maximumNumberOfLinks: 10, addQueryStringMethod: 'POST,GET'}">
<f:for each="{paginatedtherapeuts}" as="therapeut">
...
</f:for>
</f:widget.paginate>
the frontend shows 9 "entries" like it should show. but these Entries are empty. Checking this with f:debug shows the following:
TYPO3\CMS\Extbase\Persistence\Generic\QueryResultprototypeobject (9 items)
0 => ...\Therapeutprototypepersistent entity (uid=17454, pid=0)
1 => ...\Therapeutprototypepersistent entity (uid=17455, pid=0)
2 => ...\Therapeutprototypepersistent entity (uid=17456, pid=0)
3 => ...\Therapeutprototypepersistent entity (uid=17457, pid=0)
4 => ...\Therapeutprototypepersistent entity (uid=17458, pid=0)
5 => ...\Therapeutprototypepersistent entity (uid=17459, pid=0)
6 => ...\Therapeutprototypepersistent entity (uid=17460, pid=0)
7 => ...\Therapeutprototypepersistent entity (uid=17461, pid=0)
8 => ...\Therapeutprototypepersistent entity (uid=17462, pid=0)
and this is wrong. I have got 17453 entries in the {therapeuts} object starting from uid 1 to 17453.
The widget.paginate now shows uids that doesn't exist. Why or what i am doing wrong, that the paginate widget does not start with uid 1 ??
thank you for any help

Related

laravel relationship with json column

//controller
$promotion = Promotion::findOrFail($id);
//return
Array
(
[id] => 2
[en_title] => promo1
[game_id] => Array
(
[0] => 3
[1] => 4
[2] => 5
)
[amount] => 100.00
[start_at] => 2021-02-22
[end_at] => 2222-02-22
[status] => 1
)
//model promotion
class Promotion extends Model
{
use HasFactory;
protected $guarded = [];
protected $casts = [
'game_id' => 'array'
];
public function getAllGames()
{
return $this->belongsTo(Game::class, 'game_id', 'id');
}
}
Question:
Currently, I have 2 tables which are games and promotion, but I get trouble when coming into a relationship because the column of game_id inside the promotion table is a JSON, so that is hard to join it. Is there any work around can easily join them together in order to retrieve games data?
make many-to-many relationship between your games and promotion tables,
put your game_ids in pivot table (game_promotion) instead of JSON field,
Many To Many Relationships

When using a groupby, how to use a different table's column as group key?

I have a table tariff_groups:
id
name
1
Green tariffs
2
Blue tariffs
3
Red tariffs
and I have a table things_usage_tariffs that define which things use which tariffs of a tariff group:
id
thing_id
tariff_group
description
delta
1
1
1
Nice tariff of the Green group.
0
2
1
1
Even nicer tariff of the Green group.
1
3
1
1
Best green tariff.
2
4
1
2
Cool blue tariff.
3
5
1
3
Amazing red tariff.
4
With this code I get all the tariffs that thing with id 1 has:
$tariffs = DB::table('things_usage_tariffs')
->where(
[
'thing_id' => $id,
]
)
->orderBy('delta', 'asc')
->get()
->groupBy('tariff_group')
;
Which returns:
Illuminate\Support\Collection {#1257 ▼
1 => Illuminate\Support\Collection {#1247 ▼
#items: array:4 [▶]
}
2 => Illuminate\Support\Collection {#1253 ▼
#items: array:1 [▶]
}
3 => Illuminate\Support\Collection {#1246 ▼
#items: array:1 [▶]
}
}
Which is cool, but I would like to know how to change the query that I directly get:
Illuminate\Support\Collection {#1257 ▼
'Green tariffs' => array(4)
'Blue tariffs' => array(1)
'Red tariffs' => array(1)
}
I know I could iterate through the result and replace the numeric keys with their respective tariff group name, but for that I would need to make another statement that gets the names and I feel this could be done with a join. Any ideas?
With eloquent and proper relationships:
Two models:
class TariffGroup extends Model{
public function thingsUsageTariffs(){
return $this->hasMany(ThingsUsageTariff::class, 'tariff_group', 'id');
}
}
class ThingsUsageTariff extends Model{
public function tariffGroup(){
return $this->belongsTo(TariffGroup::class, 'id', 'tariff_group');
}
}
Now in your controller:
$tariffs = TariffGroup::with(['thingsUsageTariffs' => function($query){
return $query->orderBy('delta', 'asc');
}])->get();
should yield something similar to what you want to achieve.
If you want to use tables without models, one approach would be to join the tables then apply group by and keyBy to define keys for the groups:
$tariffs = DB::table('things_usage_tariffs')
->join('tariff_groups', 'tariff_groups.id', '=', 'things_usage_tariffs.tariff_group')
->orderBy('things_usage_tariffs.delta', 'asc')
->get()
->groupBy('tariff_group')
->map(function ($group) {
return $group->keyBy('name');
});
This yield should be closer to the result you want.
Note: Name must be unique since we are assigning it as keys. Also, this code is not tested, but I hope this points you in the right direction.

How to have relationship in Laravel 5.7 based on a json data key saved in a table column?

I am using this Staudenmein package in Laravel and referring to this answer given by the package owner himself.
I have two tables, pets and notifications with models Pet and Notification.
The notifications table has a column called data which is of JSON dataType and stores JSON data
{"pet_id":"4","pet_type_id":1,"lost_report_id":3,"pet_sighting_id":21,"latitude":"22.676846","longitude":"88.338509"}
The key pet_id denotes the id column of the pets table. I need a relationship so that I can fetch the name of the pet from pets table.
By referring to the answer link given above, I wrote my Notification model like this:-
namespace App\Models;
use DB, App, Auth, Hash, Lang, Mail, Config, Exception, Validator, Globals;
use Illuminate\Database\Eloquent\Model;
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
use App\User;
use App\Models\AdminConfig;
use App\Models\ReceivedAppNotification;
class Notification extends Model
{
protected $casts = [
'data' => 'json'
];
public function notificationPet()
{
return $this->belongsTo('App\Models\Pet', 'data->pet_id');
}
}
When I am running the query like this:-
$notificationQuery = Notification::with('notificationPet')
->whereRaw("FIND_IN_SET('$userId', in_app_notification_receiver)")
->where(array(
'status' => Globals::SMALL_CHAR_ACTIVE,
'is_delete' => Globals::SMALL_CHAR_NO,
))->get()->toArray();
I get the notificationPet relationship as empty, ie. the data-set is like this:-
Array
(
[id] => 10
[title] =>
[message] =>
[data] => Array
(
[pet_id] => 4
[latitude] => 22.676846
[longitude] => 88.338509
[pet_type_id] => 1
[lost_report_id] => 3
[pet_sighting_id] => 34
)
[no_of_in_app_notification_receiver] => 2
[no_of_push_notification_receiver] => 1
[from_user] => 22
[in_app_notification_receiver] => 2,23
[push_notification_receiver] => 2
[status] => a
[is_delete] => n
[push_notification_sent] => n
[created_date] => 2020-03-19 13:23:17
[modified_date] => 2020-03-19 13:23:17
[created_at] => 2020-03-19 13:23:17
[updated_at] => 2020-03-19 13:23:17
[notification_pet] =>
)
However, I have a record in pets table with id = 4, already. So the relationship should not be empty.
What am I doing wrong?
Change:
protected $casts = [
'data' => 'json'
];
To:
protected $casts = [
'data' => 'array'
];

is it possible to get sub array from mysql query?

I have a query that prints record in an array, but I need one more array in the last field e.g
[0] = Array
[id] => 1
[name]=> test
[age]=> 25
[subjects]=> 3
[order_details] = array(
[0] => array(
'id' => 1
'company_id'=> 8
'purchase_invoice_id '=> 102
'model_id' => 1
'date' => 2015-12-1
'IMEI'=>7654367876
'color' => black
'purchase_price' => 500
'is_sold' => 1
'is_returned'=>1
)
can any one guide me ? I need order_details in sub array, but please make sure it shouldn't be from php I need one mysql query to sort it out.
Thanks

Why can I not update the foreign key of an object in a 1:1 association?

I have two models, User and Profile.
A User has_one Profile and a Profile belongs_to User.
Correspondingly, the Profile model has a user_id attribute.
The association works:
p = Profile.first
=> #<Profile id: 1, name: "Jack", ... , user_id: 1>
u = User.first
=> #<User id: 1, email: "jack#example.com", ... >
u.profile.id
=> 1
p.user.id
=> 1
p.user == u
=> true
u.profile == p
=> true
I can set the user_id field on a Profile directly:
p.user_id = 2
=> 2
p.save!
=> true
p.user_id
=> 2
But why can I not set the user_id like this:
u.profile.user_id = 2
=> 2
u.profile.save!
=> 2
u.profile.user_id
=> 1
You must refresh u.profile object. Try this:
u.profile.user_id = 2
=> 2
u.profile.save!
=> 2
u.profile.reload.user_id
=> 2
This is because original profile object is still loaded on memory in u.
Hope this help :)