Skip to content
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

Listener not invoked when relation loaded through getter #1720

Closed
BrettHoutz opened this issue Mar 8, 2018 · 1 comment
Closed

Listener not invoked when relation loaded through getter #1720

BrettHoutz opened this issue Mar 8, 2018 · 1 comment
Labels

Comments

@BrettHoutz
Copy link

Issue type:

[ ] question
[X ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[X ] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] websql

TypeORM version:

[X ] latest
[ ] @next
[ ] 0.x.x (or put your version here)

When loading an entity's relation through a getter function, the listener @AfterLoad is never invoked. To demonstrate this issue, i first modified the TypeScript example by adding a listener to Category.

/* src/entity/Category.ts */

@AfterLoad()
printMessage() {
    console.log("AfterLoad invoked for", this.name);
}
/* src/index.ts */

let post = await connection.manager.findOne(Post, {relations: ["categories"]});
console.log("Found post:", post);

The listener is invoked as expected:

AfterLoad invoked for TypeScript
AfterLoad invoked for Programming
Found post: Post {
  id: 1,
  title: 'Control flow based type analysis',
  text: 'TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.',
  categories: 
   [ Category { id: 1, name: 'TypeScript' },
     Category { id: 2, name: 'Programming' } ] }

Then, i changed Post to use a setter/getter for categories.

/* src/entity/Post.ts */

@ManyToMany(type => Category)
@JoinTable()
get categories() {
    return this._categories;
}
set categories(arr) {
    if(uniqBy(arr, "name").length != arr.length) throw "Duplicate categories found";
    this._categories = arr;
}
private _categories: Category[];

After this change, the listeners are not invoked:

Found post: Post {
  id: 1,
  title: 'Control flow based type analysis',
  text: 'TypeScript 2.0 implements a control flow-based type analysis for local variables and parameters.',
  _categories: 
   [ Category { id: 1, name: 'TypeScript' },
     Category { id: 2, name: 'Programming' } ] }
@pleerock pleerock added the bug label Mar 9, 2018
@pleerock
Copy link
Member

pleerock commented Mar 9, 2018

This was a bug, fixed and will be released in 0.1.17

pleerock pushed a commit that referenced this issue Mar 26, 2018
* master: (25 commits)
  version bump
  skipped failing test for now
  version bump
  fixed issue with entity order by applied to update query builder
  Update README.md
  Add TYPEORM_DATABASE to available env options
  Provide failing test
  chore: fix typo in repository clear test filename
  minor lint fix
  delete entities using tablePath
  implement timezone based tests for multiple database drivers
  remove UTC transformation from drivers on write and read
  fixed broken lazy relation behaviour with broadcaster
  Fixes to Postgres, MySQL and MSSQL drivers propagating unhandled errors and crashing the hosting application
  remove array cast apply on function typed columns
  exit process on migrations:run command complete
  removed only test
  fixes #1720
  added test for #1703
  Update cli docs for create migration
  ...

# Conflicts:
#	CHANGELOG.md
#	README.md
#	package-lock.json
#	package.json
#	src/driver/mysql/MysqlDriver.ts
#	src/driver/postgres/PostgresDriver.ts
#	src/driver/sqlite-abstract/AbstractSqliteDriver.ts
#	src/persistence/SubjectOperationExecutor.ts
#	src/subscriber/Broadcaster.ts
#	test/github-issues/1716/issue-1716.ts
bbakhrom added a commit that referenced this issue Mar 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants