pimcore related object offset - php

I am working on a pimcore project (version 4.4.3) but still pretty new to pimcore itself.
First I made an Object called 'Event' in the admin panel and added a data component -> relation -> Object called 'speakers'.
Now I have a controllerAction which needs to return these speakers, but I don't want them all at once so I wish to add a limit and offset.
The result of $eventClass->getSpeakers() returns an array with objects on which I don't seem to be able to put any filters.
Of course I can filter them after I retrieved all of them, but if possible I would like to filter them in my request.
So my question is, how do I filter the related objects on my object?

I'm afraid that currently you can do it only using SQL. It will look something like this:
SELECT dest_id FROM object_relations_5 where fieldname = 'speakers' and src_id = 123 LIMIT 10;
Where 5 should be your class' id and 123 your object's id. You can join other tables to do more filtering, but it's getting complicated.
Commonly if you have to write custom SQL code, something is wrong with your data model. Maybe your "speaker" class should have a single href relation to "event" - this way you could get speakers listing easily with all filtering you want.

You can use the Listing object for that
$speakerId = 123;
$list = new \Pimcore\Model\Object\Event\Listing();
$list->setCondition("speakers like '%,".$speakerId.",%'");
But you can only filters the speakers with their IDs only. If you want to filter them with some other attributes then you have to make a join with object_relations_ClassID table.
Also have a look at the following link
https://pimcore.com/docs/4.6.x/Development_Documentation/Objects/Object_Classes/Data_Types/Relation_Types.html#page_Filtering-for-relations-via-PHP-api

Related

Get Laravel collection's item using collection value

Suppose we got a collection from the database like this:
$projects = Projects::all();
Now, for example, I want to get the specific project using the specified column's value. For example, suppose any project has a unique pr_code, Now I want to get an item of collection that pr_code is 1234.
Note = I Know using Projects::where('pr_code', 1234)->first() But I didn't want this. I want use inside collection
How I could do that?
Collections also have where and first (and whereFirst ) functions. So you just need to change the order of functions in your chain: Projects::all()->firstWhere('pr_code', 1234)
Projects::all() queries all records in projects and returns them as a Collection of Projects models, this could be quite a performance hit.
Projects::where('pr_code', 1234)->get() will query for the specific projects and return a Collection of Projects models that have pr_code of 1234.
Projects::where('pr_code', 1234)->first() will do the same, but return the first as a Projects model.
I recommend naming the model Project rather than the plural Projects. The Laravel model is smart enough to know to use the plural name of the database table.

GroupBy data with a specific column using Laravel 8 [duplicate]

My goal is to retrieve all of a user's 'items' and display then in my view grouped by their 'status'. There are 4 possible statuses, each with their own <div> on the page containing the items' info. After some poking around I believe I need to use the groupBy() method like so:
$items = Item::ownedBy( Auth::id() )->groupBy('status')->get();
This does seem to do some sort of grouping, but when I iterate over the collection I get a max of 4 results, one for each status. This doesn't really solve my problem as I need to display all of a user's items for each status, not just one. I must be missing something here, I'd really like to avoid making a query for each status and displaying them that way. I suppose I could filter the collection by status and create 4 new collections, but isn't this what groupBy() is supposed to do?
You can do that very easy with laravel and Collections. Collections have powerful API and a lot of handy methods, to easily achieve what you want.
Your mistake here is that you are calling groupBy on the QueryBuilder object which returns only groupped by records from the database. Instead you must select all of the records you need, then they will be returned as a collection. After that you can manipulate the collection as you wish. So what you need is:
$items = Item::ownedBy( Auth::id() )->get()->groupBy('status');
You can view all of the Colletion class useful methods here.

How to use groupBy() to group a Collection object

My goal is to retrieve all of a user's 'items' and display then in my view grouped by their 'status'. There are 4 possible statuses, each with their own <div> on the page containing the items' info. After some poking around I believe I need to use the groupBy() method like so:
$items = Item::ownedBy( Auth::id() )->groupBy('status')->get();
This does seem to do some sort of grouping, but when I iterate over the collection I get a max of 4 results, one for each status. This doesn't really solve my problem as I need to display all of a user's items for each status, not just one. I must be missing something here, I'd really like to avoid making a query for each status and displaying them that way. I suppose I could filter the collection by status and create 4 new collections, but isn't this what groupBy() is supposed to do?
You can do that very easy with laravel and Collections. Collections have powerful API and a lot of handy methods, to easily achieve what you want.
Your mistake here is that you are calling groupBy on the QueryBuilder object which returns only groupped by records from the database. Instead you must select all of the records you need, then they will be returned as a collection. After that you can manipulate the collection as you wish. So what you need is:
$items = Item::ownedBy( Auth::id() )->get()->groupBy('status');
You can view all of the Colletion class useful methods here.

PHP/MySQL Activerecord, get relationship row count

I have two tables:
media_folders
id, title
media
id, folder_id, title
They each have their own model
Media, MediaFolder. Nothing special about them except they have the static $table_name property set. Now I can assign a $has_many to the MediaFolder class which will find all associated Media. However, All i need is to get the number of media within a mediafolder and not the actual objects themselves. I want to end up with an attribute in a MediaFolder object called files which has a count of how many media rows has a folder_id of the current folder.
How would i do this?
Understand your case, but not your exact problem so can you provide some more details like:
1. does your problem is with building SQL query?
2. does your problem with building query using active records.. (if yes which framework you are using)
Need some more description.
If it's just some SQL :
$sql = "SELECT mf.folder_id, count(m.id) as files
FROM media_folders mf, media m
WHERE mf.id = m.folder_id
GROUP BY mf.folder_id"

Reference tables dynamically in MySQL / Codeigniter?

Here is the usecase my team is stuck with:
There are different types of objects in the system, (ex: user, photo, link, video, tag, phototag, etc). Now each object has its own table. When looking up objects for any purpose (say live feed, activity tracking, tagging different object types, etc) ideally the system needs to know three things:
1) The object ID
2) The object type
3) Object details from the object's depending on the usecase
For 1 & 2, i am handling it by adding two columns: object_id, object_type - this will always tell me the ID and what object the Id is referring to. But for step 3 the problem is to get object details I need to know which tables each object relates to. So how do i do that? I am using MySQL and codeignitor php.
One way i can think of is to have a table that has a relation between object and the schema tables. But the downside is then i have to always join this to get the table name then lookup that table. I am hoping to skip any join. Is there anything that can be done within codeignitor for this or any application logic to add which can detect which table to reference based on object type dynamically? And maybe i dont even need the object_Type column and the system can find the table on its own just from the object_id and the page_id or something else?
You are talking about [polymorphic associations][1] which Rails currently supports. I don't think Codeigniter supports this by default (use another layer[ORM]). However, the only thing I can think of is having a library handle this but this will all be conditional statements (ifs).

Categories