Doctrine 2 mapping process manipulation - php

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.

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.

Retrieve nested relationships for a model instance in Laravel's Eloquent

My scenario is this:
I have a Course model
Each Course can have many CourseTopics, through a topics() relationship
Each CourseTopic can have many Lessons, through a lessons() relationship
Is there a compact way to retrieve and handle all the Lessons associated with a single Course (for example, to list them, or to count their total number)?
My aim would be to have a very brief syntax to use in Blade templates; I don't want to involve logic (or at least, keep it to a bare minimum) or raw SQL queries into my template.
What I've tried:
$course->with("topics.lessons")
where $course is the current instance of the course in a template, doesn't work (gives to me all the courses with all their topics and lessons).
EDIT:
A solution is to define a hasManyThrough() relationship like:
$this->hasManyThrough("Lessons", "CourseTopics");
This solves the problem for a 2-level nested relationship. How about a 3-level instead?

Doctrine2 and MongoDB Query on embedded array of an embedded array of referenced documents in a Document

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 :)

MongoDB design [one collection and similar documents]

I'm building app which is using MongoDB. In my app each user could have many different car models (Porshe, ferrari etc).
I need to keep them all in one collection called - car. So in one collection i would like to store similar documents type. Why ? because these all models are cars and they all has from 50 till 90% the same fields.
question: am I right ? or i should have separated collection for each new car type (model) ?
Why I need it ? Because I have to show user some table with list of all his cars, and this table (GRID) has to be sorted and filtered as well.
filter by fields like number of doors, number of gears etc... it would be easier to rake these data from one collection.
Yes, using one collection is the way to go here. But name it cars instead.

CakePHP how to use static values (categories)

I would like to have categories, and rankings for my content and users respectively, and I am not sure how to go about implementing this using CakePHP conventions?
Do I need to create a model?
That depends entirely on what these categories are supposed to do and not do. You could simply define a number of constants that you use for categorizing stuff. But, are categories...
subject to change? Do you want to add more eventually?
editable? May you want to change their names?
nested?
supposed to have more attributes than just their id? Names, descriptions?
If you answered Yes to any of the above, you'll want to store them as data in the database. This is independent of Cake, it's just sane data modeling. For Cake that means you'll need to create a model. The same goes for ratings.
So you'll have these tables:
users
hasMany ratings
categories
hasMany contents
contents
belongsTo categories
hasMany ratings
ratings
belongsTo users (polymorphic)
belongsTo contents (polymorphic)
You may want to separate user ratings and content ratings into two tables instead of using a combined polymorphic table (which just means that you have an extra column keeping track of whether a rating is for a user or for content).
i guess you are looking for something like this IF you dont want to use a model:
http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/
one possible approach to use "enums" for things that maybe only have 1-5 states.
if you have more than 10 or you want to be able to dynamically modify them (label, active/inactive) you will need a separate table and model relation.

Categories