I'm currently writing an application using Kohana 3.2, which is great, however the documentation leaves something to be desired.
I have a recipe model and a rating model, which are related by a has_many and belongs_to. Ratings belong to a recipe, and a recipe has many ratings. I've been able to successfully retrieve a recipe's rating, by finding the rating that corresponds to both the id of the current user and the recipe with a $recipe->ratings->where('user_id', '=', '$user->id'). Once I've retrieved the rating, I'm able to update it successfully.
$recipe = ORM::factory('recipe')
->where('id', '=', $recipe_id)
->find();
$my_rating = $recipe->ratings
->where('user_id', '=', $user->id)
->find();
$my_rating->rating = $rating;
$my_rating->save();
The problem comes in when a rating doesn't already exist. In everything I've searched, I've found that I should probably be using the $recipe->add() function to add a rating, however I continue to get errors. When I try to load the rating, should I run a check that it has found an entry? I feel like there is a way of the ORM to know whether it exists, and if doesn't, it should create it.
The above code will create the new rating, however it will not automatically add the recipe_id to the rating table. Should I be adding the rating to the recipe first?
You should have 'raiting' coumn in your 'recipe' table.
You are doing everithing right.
If you have 'raiting' coumn in your 'recipe' table raiting property ('raiting' coumn) for selected row will be filled with the value of $rating/.
Related
i am using sequelize and when i want to set products for an order (M2M Relation)
i must put all the product object not only the id
EX:
Order.setProducts([1,2,3]) // dont work
Order.setProducts([{name: "1",price: 1},{name: "2",price: 2}]) // work
so i was wondering is that good for performance
and should i always deal with whole object or i should deal only with the ID's and after getting the ID's i do this ..
Product.findById(id)
for each productid i have
SetProducts is a function add to an sequelize instance because of your associations in the model. So calling it with the ids dont work, because your are writing the ids directly in your product table. If you want to push only the ids. Which at the the end is a better structure, you need an addiontal table, where your store id of the order and ids of the products.
In your case, in your posted question above, you are adding, the product details each time you create an order.
I have four tables vehicle_parts, part_pricing, labour_pricing and paint_pricing. Vehicle_parts table is having one to many relationship with remaining tables. Each table is having a field is_active indicating whether record is active or not. So ideally for every part in vehicle_parts table there will be only one active price in part_pricing.
I am using Objection.js to build my model as shown below -
I have a function where i am querying the vehicle part model to fetch vehicle parts along with associated prices, as shown below -
I am using withGraphFetched() method to get relational data and i am getting that.
The problem i am facing is when i am getting vehicle parts, i am getting active parts only, however i am getting non-active prices as well along with active price in relational data.
I know this can be solved using modifiers but i am not sure how to use that. In simple words i need to check is_Active flag in every relation when fetching data using withGraphFetched().
Thanks for sharing your wisdom.
You should apply filters in the model when setting up the relationship.
Like,
for example,
part_pricing: {
relation: BaseModel.HasManyRelation,
modelClass: path.join(__dirname, '/PartPricing'),
filter: (builder) => builder.where('is_active', true),
join: {
from: 'vehicle_parts.id',
to: 'part_pricing.vehicle_part_id',
},
}
I have a problem with my app.
Im trying to set filters working, so im using mongoose find method.
I have something like that:
Campsite
.find({
name: req.query.name,
country: req.query.country
})
How i can force my query to check if user gave all the data?
For example i want user to search only by name and get results, search only by country and get results and search by both queries and also get result by combining both of values
Right now the code above works like that:
When user types name and leaves empty country it wont find anything because it kinda sends an empty country even if its disabled as an input and when i remove country property from my query and user search for the name he gets correct results.
How i can fix that?
In the Mongoose documentation there is this little snippet:
Person
.find({ occupation: /host/ })
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
.limit(10)
.sort('-occupation')
.select('name occupation')
.exec(callback);
I am having a hard time understanding what the .find({ occupation: /host/ }) does different than the .select('name occupation'). Does find add conditions like where? or does it control the fields returned?
UPDATE
Ok, so i see that select only controls the fields from the final result of the queries, but now I do not understand how Find and Where are different. Am I not able to create the same queries using Find and using Where? Is the following snippet the same?
Person
.where('occupation').equals('host')
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['vaporizing', 'talking'])
.limit(10)
.sort('-occupation')
.select('name occupation')
.exec(callback);
From the API docs on select:
Query#select(arg)
Specifies which document fields to include or exclude
.select('name occupation') says that results should only include the name and occupation fields. You do not wish to see any other fields in your results.
find describes which documents to include in the results. select indicates which fields of those documents should be visible in the results.
find is the actual query. In this example you are getting all rows that have occupation equal to host. Now each of the object that matches that query has several attributes. Lets assume it has the attributes name, age, email and occupation. When you specify that you want to select name and occupation you say that you just want those attributes. So in our case age and email will not be sent back from the query.
where in this case is used to specify more than one constraint against which to query. Usually, where is used because it provides greater flexibility than find such as passing in javascript expressions
I have two models Users and News. On the page which is written with Express framework are published news and under the news are comments. Inside News model is subdocument with comments which contains two fields - user (subfields:) { name, objectid } and comment. Because in addition to comment there is user's name, I would like to add some additional informations about it (like number of comments, link to website, ...).
And this is my question: How to get data of user (from Users model) for each comment from subdocument (from News model)?
Add a populate call to your find query to pull in the user details. I'm not quite clear on your schema, but something like:
News.find().populate('comments.userId').exec(...);
This relies on your schema defining userId as an ObjectId ref to Users.