Presentation on how to chat with PDF using ChatGPT code interpreter
Doctrator Symfony Live 2011 San Francisco
1. Doctrator
Pablo Díez
Symfony Live 2011 - San Francisco
2. Pablo Díez
Creator of Mondongo
ODM for MongoDB and PHP
http://mondongo.es (in English :)
Creator of Mondator
Class generator for PHP
Creator of Doctrator
http://twitter.com/pablodip
http://github.com/pablodip
11. How does Doctrine2 work?
“Doctrine2 provides transparent persistence for PHP objects.”
http://www.doctrine-project.org/docs/orm/2.0/en/reference/introduction.html
12. That is, persist PHP objects without restrictions of a base class,
properties, methods.
namespace Model;
class User
{
public $id;
public $username;
public $email;
}
13. You only have to tell Doctrine2 (map) what you want to persist.
14. You only have to tell Doctrine2 (map) what you want to persist.
With Docblock Annotations
/**
* @Entity
*/
class User
{
/**
* @Id
* @Column(type="integer")
*/
public $id;
/**
* @Column(length=50)
*/
public $username;
/**
* @Column(length=100)
*/
public $email;
}
15. You only have to tell Doctrine2 (map) what you want to persist.
With YAML
EntitiesUser:
type: entity
fields:
id: { type: integer, id: true }
username: { type: string(50) }
email: { type: string(50) }
16. You only have to tell Doctrine2 (map) what you want to persist.
With XML
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="ModelUser" table="user">
<id name="id" type="integer" column="id">
<generator strategy="AUTO"/>
</id>
<field name="username" type="string" length="50" />
<field name="email" type="string" length="100" />
</entity>
</doctrine-mapping>
17. You only have to tell Doctrine2 (map) what you want to persist.
With PHP
$metadata->mapField(array(
'id' => true,
'fieldName' => 'id',
'type' => 'integer'
));
$metadata->mapField(array(
'fieldName' => 'username',
'type' => 'string'
));
$metadata->mapField(array(
'fieldName' => 'email',
'type' => 'string'
));
18. You only have to tell Doctrine2 (map) what you want to persist.
Then you are able to persist those objects.
19. Then you are able to persist those objects.
$user = new User();
$user->username = 'pablodip';
$user->password = 'pa$$word';
$user->email = 'pablodip@gmail.com';
$entityManager->persist($user);
$entityManager->flush();
20. A Doctrine2 best practice is to use non public
properties in the entities.
class User
{
protected $id;
protected $username;
protected $password;
protected $email;
}
21. You have to create methods to access to the properties.
Setters & Getters
public function setId($id)
{
$this->id = $id;
}
public function getId()
{
return $this->id;
}
public function setUsername($username)
{
$this->username = $username;
}
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
22. What do you need to work with this simple table?
user
id integer
username string
password string
email string
23. namespace Model;
class User
{
}
Class
user
id integer
username string
password string
email string
24. namespace Model;
protected $id; class User
protected $username; {
protected $password; }
protected $email;
Class
Properties
user
id integer
username string
password string
email string
25. namespace Model;
protected $id; class User
protected $username; {
protected $password; }
protected $email;
Class
Properties
user
id integer
username string
public function setId($id)
{
}
$this->id = $id;
password string
public function getId()
{
return $this->id;
email string
}
public function setUsername($username)
{
$this->username = $username; Setters/Getters
}
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
26. namespace Model;
protected $id; class User
protected $username; {
protected $password; }
protected $email;
Class
Mapping
Properties
user
id integer
/**
* @Entity
username string */
public function setId($id)
{ /**
}
$this->id = $id;
password string * @Id
* @Column(type="integer")
*/
public function getId()
{
return $this->id;
email string /**
* @Column(length=50)
}
*/
public function setUsername($username)
{
$this->username = $username; Setters/Getters /**
* @Column(length=100)
} */
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
27. namespace Model;
/**
* @Entity
*/
class User
{
/**
* @Id
* @Column(type="integer")
*/
protected $id;
/**
* @Column(length="50")
*/
protected $username;
/**
* @Column(length="40")
*/
protected $password;
/**
user * @Column(length="100")
*/
protected $email;
id integer public function setId($id)
{
$this->id = $id;
}
username string public function getId()
{
return $this->id;
password string }
public function setUsername($username)
{
email string }
$this->username = $username;
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getPassword()
{
return $this->password;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getEmail()
{
return $this->email;
}
}
28. namespace Model;
/**
* @Entity
*/
class User
{
/**
* @Id
* @Column(type="integer")
*/
protected $id;
/**
* @Column(length="50")
*/
protected $username;
/**
* @Column(length="40")
*/
protected $password;
/**
user * @Column(length="100")
*/
protected $email;
id integer public function setId($id)
{
$this->id = $id;
LORC
}
username string public function getId()
{
return $this->id;
password string }
public function setUsername($username)
{
email string }
$this->username = $username;
public function getUsername()
{
return $this->username;
}
public function setPassword($password)
{
$this->password = $password;
}
public function getPassword()
{
return $this->password;
}
public function setEmail($email)
{
$this->email = $email;
}
public function getEmail()
{
return $this->email;
}
}
44. namespace Model;
class User
{
protected $username;
public function setUsername($username)
{
$this->username = $username;
}
public function getUsername()
{
return $this->username;
}
}
45. namespace Model; Definition
class User
{ Properties
protected $username;
public function setUsername($username)
{
$this->username = $username;
}
public function getUsername()
{
return $this->username;
}
}
Methods
56. Then you can export them with the Dumper.
use MondongoMondatorDumper;
$dumper = new Dumper($definition);
$classCode = $dumper->dump();
echo $classCode;
57. /**
* User entity.
*/
class User
{
protected $username;
/**
* Set the username.
*
* @param string $username The username.
*/
public function setUsername($username)
{
$this->username = $username;
}
/**
* Returns the username.
*
* @return string The username.
*/
public function getUsername()
{
return $this->username;
}
}
58. And save them in files.
file_put_contents($file, $codeClass);
68. use MondongoMondatorExtension;
use MondongoMondatorDefinitionDefinition;
class Doctrator extends Extension
{
protected function doClassProcess()
{
$definition = new Definition($this->class);
$this->definitions['entity'] = $definition;
$definition = new Definition($this->class.'Repository');
$this->definitions['repository'] = $definition;
}
}
76. $mondator = new MondongoMondatorMondator();
$mondator->setConfigClasses($configClasses);
$mondator->setExtensions(array(
new DoctratorExtensionCore($options),
));
$mondator->process();
77. $mondator = new MondongoMondatorMondator();
$mondator->setConfigClasses($configClasses);
$mondator->setExtensions(array(
new DoctratorExtensionCore($options),
new DoctratorExtensionArrayAccess(),
));
$mondator->process();
78. $mondator = new MondongoMondatorMondator();
$mondator->setConfigClasses($configClasses);
$mondator->setExtensions(array(
new DoctratorExtensionCore($options),
new DoctratorExtensionArrayAccess(),
));
$mondator->process();
$article['title'] = 'Doctrator';
echo $article['title']; // Doctrator
79. $mondator = new MondongoMondatorMondator();
$mondator->setConfigClasses($configClasses);
$mondator->setExtensions(array(
new DoctratorExtensionCore($options),
//new DoctratorExtensionArrayAccess(),
));
$mondator->process();
$article['title'] = 'Doctrator';
echo $article['title']; // Doctrator
80. An extension can change the config class to extend
another extension.
81. class Doctrator extends Extension
{
protected function doClassProcess()
{
foreach ($this->configClass['columns'] as $name => $column) {
// ...
}
}
}
class DateColumn extends Extension
{
protected function doConfigClassProcess()
{
$this->configClass['columns']['date'] = array(
'type' => 'date',
)
}
}
82. You can even use extensions in the config classes.