enniel / adonis-lucid-polymorphic Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
Hey, I've been using adonis-lucid-polymorphic on a new project and just encountered a confusing issues. Briefly, the MorphTo's whereHas
query (seems like also true for has
query) only considers the first model in the relatedModels
array, resulting in incomplete results.
Example:
Model Setup
class Video extends Model {
}
class Post extends Model {
}
class Comment extends Model {
static get traits () {
return ['@provider:Morphable']
}
commentable () {
return this.morphTo([
Video,
Post
], 'id', 'id', 'commentable_id', 'commentable_type')
}
}
When I run this code
let _query = null;
Comment.onQuery((query) => (_query = query));
await Comment.query().whereHas('commentable', b=>b.where('id', '>', 2)).fetch();
The resulting SQL query (_query.sql
) is
'select * from "comments" where exists (select * from "videos" where "id" > ? and "comments"."commentable_id" = "videos"."id")'
The problem
The query should return all the comments for the commentables with id>2. Instead, it considers only the Video
model and neglects the Post
model. So for example, if I had no videos but only 10 posts (ids 1-10), the query would return zero results which isn't correct (should have returned 8 posts, with ids 3-10).
Thanks for your help.
As far as I can tell this is not already supported...
Can we add the ability to define a pivot table for polymorphic relations?
For the use case of; a single shared list of tags, that can be applied to multiple content types.
ie:
tags:
content types:
multiple blog posts, videos and articles might all be tagged with 'science' but 'science' should only exist as a tag one time, and simply be referenced multiple times as one does with a regular many to many relationship utilizing a pivot table.
I suppose a workaround might be just to make the 'taggable' pivot table an actual model, and then setup 1:many relationships between it and tags, and regular polymorphic relations between taggable and the various content types.
Would be nice to not have to make taggable a model though. Here's an example of how it's done in laravel for reference: https://itsolutionstuff.com/post/laravel-many-to-many-polymorphic-relationship-tutorialexample.html
6.1.3
9.1.0 and 6.4.1
Getting this error:
E_CANNOT_OVERRIDE_RELATION: Trying to eagerload headChef relationship twice
When I try to fetch multiple with:
await Kitchen.query().with('headChef').with('chef').fetch()
both headChef()
and chef()
belongs to the same model App/Models/Chef
but using different id (head_chef_id, chef_id
)
Getting this error just after adding adonis-lucid-polymorphic/providers/PolymorphicProvider
to the app providers.
Debug:
`warning:
WARNING: Adonis has detected an unhandled promise rejection, which may
cause undesired behavior in production.
To stop this warning, use catch() on promises or wrap await
calls inside try/catch.
TypeError: Cannot read property 'ioc' of null
at IocResolver.get ioc [as ioc] (/app/node_modules/@adonisjs/lucid/lib/iocResolver.js:58:23)
at Object. (/app/node_modules/adonis-lucid-polymorphic/src/Traits/Morphable.js:4:17)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
at PolymorphicProvider.register (/app/node_modules/adonis-lucid-polymorphic/providers/PolymorphicProvider.js:7:23)`
I currently don't have the resources to maintain this project. If someone is willing to put in the time to do it, I'm willing to assign the necessary privileges to that person. If you're willing to do this, reply on this issue or send me email.
// App/Model/Comment
'use strict'
const Model = use('Lucid')
class Comment extends Model {
static get traits () {
return ['@Provider:Morphable']
}
commentable () {
return this.morphTo([
'App/Models/Post', 'App/Models/Video'
], 'id', 'id', 'commentable_id', 'commentable_type')
}
}
module.exports = Video
This is similar to laravels morphMap.
Relation::morphMap([
'posts' => 'App\Post',
'videos' => 'App\Video',
]);
Appreciate to your great job.
As like laravel on PHP, can you support to handle pivot data?
I need to utilize pivot data as below workaround in my application
eg. (pseudo code)
File {
...
}
Attachment {
...
file_id
as (String)
attachable_id
attachable_type
}
User {
attachments() {
return morphMany('Files', 'attachable')
}
avatarImage() {
return this.attachments().where('as', 'avatarImage')
}
}
Thanks, if you don't mind i will fork and make a PR for this
Can I store a register with this sintax?
post.comments().create({ ... })
The console show this error
post.comments is not a function
I'm wondering this situation.
We'd used this package on AdonisJS 4.1 project but now we'd upgraded it from 4.1 to 5 and we need to this package so much that you have no idea.
Are you planning to going to write a lucid polymorphic package for AdonisJS v5?
Thanks..
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.