You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm writing a previous CMS project of mine written in PHP on Laravel, later on Ruby on Hanami with Rails' ORM to Sails.js with Bookshelf.js
The issue at hand is implementing tagging functionality via the Keyword model.
I'm not sure whether it is because non-oop style of bookshelf or missing guides online that I can't work my way around it, but however I try I couldn't get a simple polymorphic functionality worked out.
Background structure of the database/models
keywords table: id, keyword, etc.
keywordables table: id, keyword_id, keywordable_id, keywordable_type, etc.
posts table: id, loc, etc.
stories table: id, loc, etc.
and all other sorts of contents with their respective tables.
Issue Description
A more detailed tutorial on doing basic things on associations, especially polymorphic associations is really missing. For example: how to get all the keywords that have an association with a Post model, while skipping the ones that are in the database because they are associated with other kinds of content like Story & etc but have no association with a Post model.
Steps to reproduce issue
In the PHP code, I do something like this: Keyword class defined in a separate library and then an additional function is added to it in this new library like this:
namespace IAtelier\BPLog;
use IAtelier\Atelier\Keyword as KeywordBase;
use IAtelier\BPLog\Post as Post;
class Keyword extends KeywordBase
{
public function posts()
{
return $this->morphedByMany(Post::class, 'keywordable');
}
}
then I can effortlessly retrieve all the keywords that have at least one association of the post model, by simply writing:
$tags = Keyword::has('posts')->get();
In Bookshelf I have designed a similar structure, two separate libraries, and Keyword is defined in like this:
I hardwire added Post and Book while in reality, I rather preferred to do the same thing as I did in PHP: adding additional functions to the Keyword class from the second library.
Post model is an extension on Book model, like this:
var Book = sails.hooks.orm.bookshelf.model(
'Book',
{
....
keywords() {
return this.morphMany('Keyword', 'Keywordable', ['keywordable_type', 'keywordable_id']);
},
....
and the Post definition:
let Post = sails.hooks.orm.bookshelf.model(
'Post',
sails.hooks.orm.bookshelf.model('Book').extend({
tableName: 'posts',
....
Here comes writing the same line of code I wrote in php to get all the keywords that are associated with one or more posts:
However whatever I try I get errors! For example: the above segment itself is just finding a single keyword and should return the reverse associated Book submode, which in this case is a Post model. However it returns errors!
Expected behaviour
I was looking/hoping for something like this:
Keyword.fetchAll({ has: 'Post'});
While being able to add the post function in advance:
After much wandering in the darkness I realized this one is related to issue #1324; So, @chamini2 I/we highly appreciate your efforts. Only that MorphTo and MorphToMany are two totally separate animals!
captainhusaynpenguin
changed the title
Question: OOP style retrieval of polymorphic many-to-many associations (example: tag/keyword functionality on a CMS)
#feature-request OOP style retrieval of polymorphic many-to-many associations (example: tag/keyword functionality on a CMS)
May 13, 2020
I'm new to Node.js and Jr anything other than DOM manipulation, also the total absence of guide-like documentation and in-depth tutorials for bookshelf, makes the learning curve higher. So, for the time being, I can't develop the feature myself, however, this is how I hacked my way around it, with the addition of taking advantage of 'virtuals' plugin which was removed from the core of the project [WHY?] and I thought sharing it would elaborate on the description & help someone to actually implement it in the core:
var Book = sails.hooks.orm.bookshelf.model(
'Book',
{
async keywords() {
const Keyword = sails.hooks.orm.bookshelf.model('Keyword');
let type = this.virtuals.kind() + 's';
var getKeywords = async function(array) {
const Model = sails.hooks.orm.bookshelf.model('Keyword');
let keywords = {};
for (const [id, value] of Object.entries(array) ) {
keywords[id] = await Model.forge({id: value.keyword_id })
.fetch()
.then(model => { return model; });
}
return keywords;
}
let result = await sails.hooks.orm.bookshelf.knex('keywordable')
.where({
keywordable_type: type,
keywordable_id: this.id,
}).select('keyword_id').then(getKeywords);
return result;
},
...
Thanks in advance & much success wishes for the future contributor(s)!
Introduction
I'm writing a previous CMS project of mine written in PHP on Laravel, later on Ruby on Hanami with Rails' ORM to Sails.js with Bookshelf.js
The issue at hand is implementing tagging functionality via the
Keyword
model.I'm not sure whether it is because non-oop style of bookshelf or missing guides online that I can't work my way around it, but however I try I couldn't get a simple polymorphic functionality worked out.
Background structure of the database/models
and all other sorts of contents with their respective tables.
Issue Description
A more detailed tutorial on doing basic things on associations, especially polymorphic associations is really missing. For example: how to get all the keywords that have an association with a Post model, while skipping the ones that are in the database because they are associated with other kinds of content like Story & etc but have no association with a Post model.
Steps to reproduce issue
In the PHP code, I do something like this:
Keyword
class defined in a separate library and then an additional function is added to it in this new library like this:then I can effortlessly retrieve all the keywords that have at least one association of the post model, by simply writing:
In Bookshelf I have designed a similar structure, two separate libraries, and Keyword is defined in like this:
I hardwire added
Post
andBook
while in reality, I rather preferred to do the same thing as I did in PHP: adding additional functions to the Keyword class from the second library.Post model is an extension on Book model, like this:
and the Post definition:
Here comes writing the same line of code I wrote in php to get all the keywords that are associated with one or more posts:
However whatever I try I get errors! For example: the above segment itself is just finding a single keyword and should return the reverse associated Book submode, which in this case is a Post model. However it returns errors!
Expected behaviour
I was looking/hoping for something like this:
While being able to add the post function in advance:
Actual behaviour
I get all sorts of error depending on how I bend the code, but never anything remotely close to what I'm trying to achieve 🤷♂️
Questions
Keywordable
model before linking things together? How does the relationship definition on it should look like?The text was updated successfully, but these errors were encountered: