Sobes.tech
Junior — Middle

Can you name the main types of relationships used in ORM?

sobes.tech AI

Answer from AI

In ORM (Object-Relational Mapping), the main types of relationships between entities are usually as follows:

  • One-to-One: Each record in one table is linked to exactly one record in another table.
  • One-to-Many: One record in a table is linked to multiple records in another table.
  • Many-to-Many: Records in one table can be linked to multiple records in another table and vice versa. Usually implemented through an intermediate table.

Example in PHP using Doctrine ORM:

/**
 * @Entity
 */
class User {
    /**
     * @OneToMany(targetEntity="Post", mappedBy="author")
     */
    private $posts;
}

/**
 * @Entity
 */
class Post {
    /**
     * @ManyToOne(targetEntity="User", inversedBy="posts")
     */
    private $author;
}

This shows a one-to-many relationship between a user and posts.