Inserting data with PHP OO (Similar to EntityManager.persist (Object) java manager)

1

I would like a hint of how to loop to insert an object into the mysql / postgres database ...

Example

class User {
     // PRIMARY KEY
     public $id;
     public $login;
     // PRIMARY FOIREIGN KEY
     public $people;

     __construct() { 
         $this->id = -1;
         $this->login = "";
         $this->people = new People();  
    }
}




class People {
     // PRIMARY KEY
     public $id;
     public $name;

     __construct() { 
         $this->id = -1;
         $this->name = "";
    }
}


class Controller {

     $em = EntityManager();
     $em->beginTrasaction();
     $user = new User();
     $user->setLogin('admin');
     $bool = $em->persist($user);
     if($bool) {
          $em->commit();
     } else {
          $em->rollBack();
     }
}
    
asked by anonymous 11.06.2014 / 14:16

1 answer

1

There is an example of the Hibernate framework of Java, the frameworks of php for ORM one that I started to use in a short time, but today I can not open Doctrine (ORM framework) through it you can define your entity doing the mapping of the class with comments (similar to java annotations), see how to work with objects in Doctrine . I believe that's what you're looking for. Regarding the usage to the SGDB you are using, you just have to configure the connection (similar to the driver in the case of hibernate) done that. If you use the conventions established by Doctrine (usually similar to Hibernate) you can port your application to other databases without any difficulties. As I said before, there are other ORM frameworks, but what I know and liked was Doctrine.

Once using Doctrine, configured the connection to the database and added annotations in the entity, you loop creates the instances, assigns the values, and executes the command below to save the data in the persistence layer. / p>

$em->persist($user); //considerando $em o entity manager do doctrine
$em->flush();
    
26.06.2014 / 14:39