Atlas ORM The Ultimate Guide to Database Interaction and Management

Welcome to Atlas ORM: The Ultimate Guide

Atlas ORM is a powerful Object-Relational Mapping tool designed to simplify database interaction within your application. With dozens of APIs and code snippets, this introduction will help you master Atlas ORM!

Getting Started

Atlas ORM integrates seamlessly into your existing PHP applications. Here’s how you can start using it:

  
  composer require atlas/orm
  

Basic Usage

Here’s how to set up a new Atlas ORM instance:

  
  
  

Working with Entities

Atlas ORM allows you to define and manipulate entities and their relationships. Let’s look at the key APIs:

Creating Entities

Define an entity:

  
  oneToMany('comments', Comment::CLASS);
      }
  }
  ?>
  

Persisting and Retrieving Entities

Insert, update, and fetch records:

  
  newRecord(Blog::CLASS);
  $blog->title = 'First Blog Post';
  $blog->content = 'Hello, World!';
  $atlas->persist($blog);

  // fetch
  $blog = $atlas->fetchRecord(Blog::CLASS, 1);
  echo $blog->title; // Output: First Blog Post
  ?>
  

Relationships

Define and work with relationships:

  
  use App\Atlas\Mapper\Comment;

  class Comment extends Record
  {
      public $id;
      public $blog_id;
      public $text;
  }

  class CommentMapper extends Mapper
  {
      protected function setRelated()
      {
          $this->manyToOne('blog', Blog::CLASS);
      }
  }
  ?>
  

Fetching a record with related entities:

  
  $blog = $atlas->fetchRecord(Blog::CLASS, 1, ['comments']);
  foreach ($blog->comments as $comment) {
      echo $comment->text; // Outputs comments related to the blog
  }
  ?>
  

Example Application

Use Atlas ORM in a simple blog application:

  
  newRecord(Blog::CLASS);
  $blog->title = 'My Atlas ORM Blog Post';
  $blog->content = 'This is a blog post created with Atlas ORM.';
  $atlas->persist($blog);

  // add a comment to the blog post
  $comment = $atlas->newRecord(Comment::CLASS);
  $comment->blog_id = $blog->id;
  $comment->text = 'Great post!';
  $atlas->persist($comment);

  // fetch and display the blog post with comments
  $fetchedBlog = $atlas->fetchRecord(Blog::CLASS, $blog->id, ['comments']);
  echo "Title: " . $fetchedBlog->title; // Output: My Atlas ORM Blog Post
  echo "Content: " . $fetchedBlog->content; // Output: This is a blog post created with Atlas ORM.
  foreach ($fetchedBlog->comments as $fetchedComment) {
      echo "Comment: " . $fetchedComment->text; // Output: Great post!
  }
  ?>
  

Atlas ORM can greatly simplify your workflow and is flexible enough to handle complex database interactions. Start using it today and experience the difference!

Hash: 13492cfede2224fcd19e05ce0a557fd3f7ad6bc7e3467fcf7c9961bc486918e1

Leave a Reply

Your email address will not be published. Required fields are marked *