开发者

What is this technique called?

I'm building a simple PHP 开发者_StackOverflow中文版library.

One feature is to allow doing stuff like this:

$myCatalog = new Catalog();  
// add books  
for ($i=0; $i<99; $i++) {  
    $myBook = new Book();  
    $myBook->title = 'Book ' . $i;  
    $myBook->parentId = $myCatalog->id;  
    $myBook->create();  
}  
// add other product  
for ($i=0; $i<99; $i++) {  
    $myProduct = new Product();  
    $myProduct->name = 'Product ' . $i;  
    $myProduct->parentId = $myCatalog->id;  
    $myProduct->create();  
}
$catalog_books = $myCatalog->getChildren('Book');  
$catalog_products = $myCatalog->getChildren('Product');

I guess it's not really ORM functionality, is it?


The term ORM (object relational mapping) suggests that there is a database (or other object / data space) involved, and that there are objects (eg tables) in the database called Catalog, Book, Product, etc. And that there are relationships between these objects - Catalog has many Books, Catalog has many Products, etc.... And that you've created these PHP objects to model the database objects in your application. But if that is not the case - that is, if you're not mapping database objects and relationships to PHP objects, then this is simply an example of good old Object Oriented Programming.


I'd say it looks like an Active Record Pattern

Typically active record entity implementations are responsible for maintaining themselves (Create(),Update(),Delete())

ORM (Object Relational Mapping) typically is when you map a database schema directly to a POCO (Plain old code object). Then an intermediary (Repository usually) is responsible for maintaining your entities.

Most ORM systems employ something like this:

$user = new User();
$user->Username = "Aren";
$user->Email = "aren@somewhere.com";

$em = new EntityManager;
$em->persist($user);
$em->flush();

----- Let me rephrase -----

ORM, Object Relational Mapping usually operates by defining an association between your tables and fields on an object. There's numerous ways of doing this, Doctrine ORM supports 3 methods: Code Annotation, XML, and YAML.

Example Doctrine Mapping:

<?php
/** @Entity */
class MyPersistentClass
{
    /** @Column(type="integer") */
    private $id;
    /** @Column(length=50) */
    private $name; // type defaults to string
    //...
}

Then all create/read/update/destroy (CRUD) operations on the entities are managed through an entity manager / repository. The SQL is generated for you, you're not responsible for providing the way your entity is brought out of the DB or persisted. (Although some ORM's give you the flexability to extend this).

ARP, Active Record Pattern is where each object represents a row in the database. In many common ARP implementations, most DB operations are exposed right on the object (although not required).

$user = new User();
$user->loadByID(42);

ORM Systems sometimes use ARP to help with entity management.

Ultimately though, without some kind of meta-data to tie an object to it's row, i wouldn't say it's an ORM.

I guess what it boils down to is this:

  • If the system uses meta-data to automatically assoicate an entity's properties/fields with the corresponding table colunms. It can be classified as an ORM system.
  • If the entities each represent a single row of a table, and commonally (but optionally) provide functions to manage itself like .save() .delete(), etc. It can be classified as an ARP (Active Record Pattern)
  • Your system can be both.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜