Implementation of doctrine orm in sugar crm 7.5

10
Implementing Doctrine ORM in SugarCRM 7.5 Jakub Paś Common Resources

Transcript of Implementation of doctrine orm in sugar crm 7.5

Page 1: Implementation of doctrine orm in sugar crm 7.5

Implementing Doctrine ORM in SugarCRM 7.5

Jakub PaśCommon Resources

Page 2: Implementation of doctrine orm in sugar crm 7.5

What is Object-Relational Mapping?

●It converts data between different database types to object-oriented programming languages

●It simplifies development by automating object-table conversion

●The SugarCRM attempt to acces data in objective way by SugarBeans (but it is very limited approach)

Page 3: Implementation of doctrine orm in sugar crm 7.5

Object-Relational Mapping

Advantages

● Database Abstraction, API-centric design mentality, cleaner code, huge reduction in code.

● Makes database access more transparent so that you can easily port your application to another database.

● Tight coupling: This approach creates a tight dependency between model objects and database schemas

● Speed (due to reflection mechanism use and caching)● Better for unit testing. Helps to maintain business logic in a clean way

Disadvantages

● Adds more complexity - one more abstraction layer● Steep learning curve at the beginning

Page 4: Implementation of doctrine orm in sugar crm 7.5

Popular Open Source Object-Relational Mapping

Software for PHP

Data mapper pattern approach

Doctrine 2.0PropelRedBeanPHPschemadb

Activerecord approach

Doctrine 1.0Zend DB

CodeIgniter

$user = new Account;$user->name = ‘johndoe’;EntityManager::persist($user);

Business logic stored in services

$user = new Account;$user->name = ‘johndoe’;$user->save();

Business logic stored on models

Page 5: Implementation of doctrine orm in sugar crm 7.5

Doctrine

- Easy to use.- DQL - nice way of creating queries in IDE- Migrations (database versioning)- import/export from/to schema/yaml/models

PDO might be enough for small apps. Doctrine is best solution for bigger Zend or

Symfony based projects.

Page 6: Implementation of doctrine orm in sugar crm 7.5

Working with Entitiesuse Doctrine\ORM\Mapping as ORM;/*** Accounts* @ORM\Table(name="accounts", indexes={@ORM\Index(name="idx_accounts_date_modfied", columns={"date_modified"})...*/class Accounts{ /** * @var string * @ORM\Column(name="id", type="string", length=36, nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id;

Page 7: Implementation of doctrine orm in sugar crm 7.5

SugarCRM 7.5 Introduced SugarQuery but...

● It does not return SugarBeans in results.● Complicated class structure, no namespaces used● Does not use Sugar autoloader● The types returned methods are not always defined● Does not implement many useful interfaces to order returned data like many

kinds of Countable, Iterators, Recurrence Iterators, Paginators and so on.● The query compiler has bug so the order in query builder is sometimes

important● Does not support transactions

Page 8: Implementation of doctrine orm in sugar crm 7.5

UsageMySQL

global $db;$date = mysql_escape_string($date);$query = "SELECT ac.anme FROM accounts ac WHERE ac.date_enterd = $date";$res = $db->query($query);$data = $db->fetchByAssoc($res);

SugarQueryrequire_once 'include/SugarQuery/SugarQuery.php';$date = mysql_escape_string($date);$query = new SugarQuery();$query->from(BeanFactory::getBean('Accounts'))->select(array("accounts.name"));$query->where()->equals('date_entered',$date);$data = $query->execute();

Doctrine$em = Zend_Registry::get(’entityManager’);$data = $em->createQueryBuilder()->select('ac.name')->from('Accounts','ac') ->where('ac.dateEntered = ?1')->setParameter(1,$date)

Page 9: Implementation of doctrine orm in sugar crm 7.5

Conclusions

Advantages● Better performance (20 - 400 times).● No changes in sugarCRM or sole core files necessary. Can be used optionally.● Doctrine allows to use transactions which allow to fall back queries in case or error or exception thrown.● Full autocompletion. Returning valid types. Easy to check what we can get from entity without leaving IDE window.● It is possible to change order in created queries. All parameters used to build query are quite escaped no additional

validation is necessary. It can by safely used in landing pages and other code exposed to customers.● Many hydration types (formats of returning data). Returned structures. implements many useful interfaces like many

kinds of Countable, Iterators, Recurrence Iterators, Paginators and so on.● Clear object oriented interface.● Support for Doctrine Migrations - the mechanism of versioning database schema and easily deploying changes to it.

Disadvantages● For now entities need to be updated every time cache is cleared on development machines or modules are created

but this can be automatized later.● Greater memory usage

Page 10: Implementation of doctrine orm in sugar crm 7.5

Thank You