mirror of
https://github.com/strapi/strapi.git
synced 2025-11-10 23:29:33 +00:00
Fix issue when the relation is populated or not (Bookshelf)
This commit is contained in:
parent
19c696ff25
commit
0d9626b165
@ -14,9 +14,23 @@ class QueryBuilder {
|
|||||||
let fieldKey = `${model.collectionName}.${attributeKey}`;
|
let fieldKey = `${model.collectionName}.${attributeKey}`;
|
||||||
if (association) {
|
if (association) {
|
||||||
if (association.nature === 'manyToMany') {
|
if (association.nature === 'manyToMany') {
|
||||||
const { attribute, column } = model.attributes[key];
|
const { attribute, column } = model.attributes[association.via];
|
||||||
fieldKey = `${association.tableCollectionName}.${attribute}_${column}`;
|
fieldKey = `${association.tableCollectionName}.${attribute}_${column}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If for instance we want to filter on the model relation, such as
|
||||||
|
* {
|
||||||
|
* product: {
|
||||||
|
* method: 'whereIn',
|
||||||
|
* value: ["1", "2", "3"]
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* the fieldKey should be "products.id" and not "products.product"
|
||||||
|
*/
|
||||||
|
if (attributeKey === key) {
|
||||||
|
fieldKey = `${model.collectionName}.${model.primaryKey}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (value.symbol) {
|
if (value.symbol) {
|
||||||
@ -108,7 +122,7 @@ class QueryBuilder {
|
|||||||
|
|
||||||
qb.innerJoin(
|
qb.innerJoin(
|
||||||
relationTable,
|
relationTable,
|
||||||
`${association.tableCollectionName}.${strapiModel.attributes[key].attribute}_${strapiModel.attributes[key].column}`,
|
`${association.tableCollectionName}.${strapiModel.attributes[association.alias].attribute}_${strapiModel.attributes[association.alias].column}`,
|
||||||
`${relationTable}.${astModel.primaryKey}`,
|
`${relationTable}.${astModel.primaryKey}`,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@ -97,7 +97,26 @@ module.exports = {
|
|||||||
|
|
||||||
return data
|
return data
|
||||||
.filter(entry => {
|
.filter(entry => {
|
||||||
const entryValue = entry[ids.alias][astModel.primaryKey];
|
/**
|
||||||
|
* In some cases (Mongoose for instance) the entry will be populated so we need to access its value using the primary key
|
||||||
|
* Example
|
||||||
|
* // Mongoose
|
||||||
|
* entry = {
|
||||||
|
* product: {
|
||||||
|
* _id: "some_id", // the value is entry["product"]["_id"]
|
||||||
|
* price: 10
|
||||||
|
* },
|
||||||
|
* company: "strapi"
|
||||||
|
* }
|
||||||
|
* // Bookshelf
|
||||||
|
* entry = {
|
||||||
|
* product: 1, // the value is entry["product"]
|
||||||
|
* company: "strapi"
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
const entryValue = _.isUndefined(entry[ids.alias][astModel.primaryKey])
|
||||||
|
? entry[ids.alias]
|
||||||
|
: entry[ids.alias][astModel.primaryKey];
|
||||||
return entryValue.toString() === ids.value.toString();
|
return entryValue.toString() === ids.value.toString();
|
||||||
})
|
})
|
||||||
.slice(skip, skip + limit);
|
.slice(skip, skip + limit);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user