-
Notifications
You must be signed in to change notification settings - Fork 376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom condition #94
Comments
Can you post the SCHEMA and some dummy data so I can try it locally and see how I can create a simple and clean syntax to the query? Maybe a link on droplr or dropbox or something.. no need to have much data, just some in every table at least to run that last query a see one result. |
I copied your schema and added 2 rows on every item from the tables in your last query (changing your ids in WHERE condition). I'm still trying to figure out the relationship between the tables. Is this your hierarchy?
If so, you're trying to fetch |
Yes you're right. I know that i can filter records in nodejs, but it's too slow. Only way for me is do it at SQL. I'm creating subset of my schema for you. |
It's late here, I have to dinner now, but I'm going to think about this after. I believe the best approach is using my Category(813).getProducts()
.only('id', 'ean')
.limit(250)
.offset(1250)
/* something here like .withProperty(4564)*/
.run(function (err, products) {
// ...
}); |
Bon appetite :), I have just finished subset of schema. You can download it here: http://download.kodytek.net/dump_orm2013-04-02.sql.gz . I am really curious on solution, in SQL I can solve it in few seconds, but at application level i'm a begginer. Thank you very much. |
Ok, I made some changes that I think will work to solve this problem. You'll have to use the git version and not npm version for now. If you don't know how to do it just tell me. I assume you have somewhere in your code something like: Product.hasMany("property_value", PropertyValue); Because of this, when you have a product, you have access to Category(813).getProducts()
.only('id', 'ean', 'price', /* ... */)
.hasPropertyValue(4564) // <-- the new filter method
.limit(250)
.offset(1250)
.run(function (err, products) {
// ...
}); |
Hi, Only little problem remains, it doesn't work for .count(). Thank you very much, |
You mean, instead of |
Yes you're right. I'm using server side paging so I need to get size of the subset and then fetch only requested page of data. |
Tested and works well. Thank you very much man, you have my gratitude. |
Hi Diogo,
TypeError: Object # has no method 'concat'
at Object.SelectQuery.proto.whereExists (F:\app\products-api\node_modules\sql-query\lib\Select.js:125:71)
at Driver.count (F:\app\products-api\node_modules\orm\lib\Drivers\DML\mysql.js:154:6)
at Object.ChainFind.chain.count (F:\app\products-api\node_modules\orm\lib\ChainFind.js:35:16)
...
...
|
Please try the latest git version if you can. |
Error message from last git version is:
|
Could you post the query you're trying to use? |
Following code is part of CategoryController of above mentioned model. var category_id = Number(req.params['category']); // 812
var categoryInstance = Category(category_id);
var productCount = categoryInstance.getProducts({});
productCount.hasPropertyValue(4564)
productCount.count(function (err, count) {
if (count > 0) {
....
}
} Produced SQL query should be something like: SELECT
count(*) as count
FROM
`product` `t1`
JOIN `category_product` `t2` ON `t2`.`product_id` = `t1`.`id`
WHERE
(`t2`.`category_id` = 812)
AND EXISTS(
SELECT * FROM `product_property_value` WHERE `product_id` = `t1`.`id` AND `property_value_id` = 4564
) |
I can't test it right now but could you try and remove the |
I think I've replicated your problem. I'm going to investigate. |
Update your git version, run |
Tested and works fine. Thank you very much. |
Hello dresende,
thank you very much for your work on this project, it helps me a lot. I'am using your orm for accessing mysql database with about 30M records and it works fine. So now I'm looking for some way to add custom condition to where clause.
Here is my SQL schema:
I'm getting products for category by:
SQL statement looks like this:
But now I need to add filtering by property values which means adding another condition:
So is there any correct way to do that?
Thank you very much,
kody
The text was updated successfully, but these errors were encountered: