I am currently developing a system to filter and display data that is used via Eloquent and saved to the database in the following way:
Catalog -(1)---(n)-> Product -(1)---(n)-> Group -(n)---(n)-> Part
Product -(n)---(n)-> Provider
I'm eager loading these models completely and then display their attributes in my view as a table, the columns I base on user settings. Now I'm trying to add a way to order (and search through) these columns, but I can't seem to figure out any way to be able to order the whole result (Catalog is ultimately the 'master' collection) by any nested attributes.
For parts and providers (n:n relationships) I only need to be able to order the result by either the first or the last one's attributes.
Can anyone help me out here? I tried joins, but I can't figure out how to join both the first element and the last element for those two models and somehow distinguish them.
Related
I have three main mySQL tables:
users
tags
news_articles
I also have many to many tables to connect them as needed.
I want to grab all the articles that share the same tags as users so I can personalise their experience.
(example): say a user has tags for "Trump" or "Brexit" I want to grab all the news articles that also have Trump or Brexit.
I could grab the whole data pool and make calculations but that might get unnecessarily server intensive so is there a way of doing it through Eloquent?
Also is Eloquent capable of tallying the user tags to determine which tags the user is most interested in before grabbing the articles or would that need a workaround after?
Thanks in advance.
Christophvh's response did work for me in the end. After some more time with Eloquent I found plenty more ways of doing this.
A way I would go about this now for anyone who happens to be stuck:
By creating a tags method in my User and Article models to return all related tags I can then do the following.
$userTags = $user->tags()->pluck('name')->get()->toArray();
$articles = Article::whereHas('tags', function($tag) use ($userTags) {
$tag->where('name', $userTags)->get();
})->get();
The above code should:
Get only the name field of all tags belonging to a user and store them in an array.
Grab all articles where their related tags name field contain any tag in the $userTags variable
To create a priority tags list I would just need to run array_count_values($userTags) and order it by the returned values, then pass the new ordered $userTags array into the Eloquent query.
I want to ask if its clean possible to change the mapping process.
For example, if i have an database, with products and offers, so the relation is 1 product n offers.
I want to get on fetch an product with multiple offers not an collection of offers, instead of i want to get an class named "AggregateOffers" which calculates the highest the lowest price and includes the offers collection.
Did anyone know an good/clean solution?
For an better understanding, schema.org defines the relation between an product an multiple offers, within an aggregateoffer, so i want to abstract them, to generate at the end an json-ld.
My actually design looks like schema, as you can see i have an 1-n relation from Product to Offer, what i actually want to get when i read from the DB, is an extended relation.
For example:
Product -> getOffers (should not return an collection of offers, instead it should return an "AggregateOffers" which hold the collection of offers).
The only idea, is to update the "getOffers" function and return die Aggregation class, but this acts not really good on me.
I'm wondering about solution in my recent project. Site has categories (nested set model) and products. Main problem is that I must implement many categories properties. These properties are different for each category. Of course products which belongs to specific category have specific properties values.
My problem is how to solve storing these properties. I read about EAV model but main con for these solution is basicly slow, needs many additional tables and not suitable for larger data sets. The more I read about it the more I'm not convinced.
My second thought is XML. Categories table would have xml column that keeps properties names and in product table also xml column but with properties values.
Now question - which solution will be better and more flexible? Another problem is to allow user search by specific categories properties. I'm assuming that db would have many products so performance of queries is very important.
I have a collection of Customer that have multiple Order.
The Order is an embedded array in the Customer collection.
Inside one Order there is an #ReferencedMany to the Product collection.
I now want to do some reporting on every Product that a Customer has ever bought.
What would be the MongoDB way to find all the products for this customer and add filters on this query. I don't want to loop every order and then fetch all the products and put them in a Doctrine ArrayCollection.
Is there a direct way to query these products?
Mongo DB does not fetch references automatically in single query. Maybe some drivers support such functionality and may produce sub-query/ies transparent for user.
You can aggregate the data required for report to one collection using map-reduce pattern, for example, or embed Product data to Customer. But as consequence the queries on Product will become more complex.
You can de-normalize data and duplicate some Product data required for reports in Customer collection. Then you'll have to deal with synchronization of those collections.
Working on same issue, I came to the conclusion that if you want to reflect relations between objects you should use relational database :)
So I have a User table and a History table with User hasMany Histories, and I'm trying to implement pagination on the user table.
My problem is that I have search, and some of the things one can search by are things in the History table. Is there a way to filter pagination results based on data in a table associated by hasMany? Containable, which initially seemed like a solution, allows such filtering but only in the retrieval of associated data, not the records themselves (unless I'm missing something?)
Has anyone had to solve this before?
Since it's a hasMany relationship, that means Cake will need to make 2 separate queries: 1 on the users table, and one on the histories table to retrieve all the associations. Since the History data isn't being retrieved until the 2nd query, then your 1st query cannot be filtered via WHERE conditions for fields found in the History model.
To resolve this, you can do one of two things:
Perform pagination on History using Containable (since History belongsTo User, meaning only 1 query will be performed).
Perform pagination on User the way you're already doing, except perform an ad-hoc join to History such that it's no longer a hasMany relationship.
e.g.:
$this->User->bindModel(array('hasOne' => array('History')));
$this->paginate['User']['contain'][] = 'History';
$this->paginate('User', array('History.some_field' => 'some_value'));