#How to show data from relation

1 messages · Page 1 of 1 (latest)

ebon edge
#

I have 3 entities: Product, Product Attributes and Attribute Types. When I get the data from a product I receive the attributes like this:

"attributes": [
  {
    "id": 1,
    "value": "2000",
    "attributeType": null
  },
  {
    "id": 2,
    "value": "1",
    "attributeType": null
  },
...
]

I would like to get the attributeType name too, like:

"attributes": [
  {
    "id": 1,
    "value": "2000",
    "attributeType": "unity"
  },
  {
    "id": 2,
    "value": "1",
    "attributeType": "meters"
  },
...

Entities:

Product:

  @OneToMany(
    () => ProductAttribute,
    (productAttributes) => productAttributes.product,
    { cascade: true, eager: true },
  )
  attributes: ProductAttribute[];

Product Attribute:

@Entity()
export class ProductAttribute {
  @PrimaryGeneratedColumn()
  id: number;

  @OneToOne(() => AttributeType, {eager: true})
  @JoinColumn()
  attributeType: AttributeType;

  @Column()
  value: string;

  @ManyToOne(() => Product, (product) => product.attributes)
  product: Product;
}

Attribute Type:

@Entity()
export class AttributeType {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @ManyToOne(() => Category, (category) => category.attributes)
  category: Category;
}
tropic dirge
#

What does the service look like?