Symfony2 and MongoDB

download Symfony2 and MongoDB

of 62

  • date post

    12-May-2015
  • Category

    Technology

  • view

    23.665
  • download

    3

Embed Size (px)

description

See how to use MongoDB in Symfony2 projects to speed up the development of web applications. We will give an introduction of MongoDB as a NoSQL database server and look at the options on how to work with it from Symfony2 and PHP applications.

Transcript of Symfony2 and MongoDB

  • 1.Pablo Godel @pgodel - 2012.phpDay.it May 18th 2012 - Verona, Italy https://joind.in/6383Friday, May 18, 2012

2. Who Am I? Born in Argentina, living in the US since 1999 PHP & Symfony developer Founder of the original PHP mailing list in spanish Master of the parrillaFriday, May 18, 2012 3. Who Am I? Born in Argentina, living in the US since 1999 PHP & Symfony developer Founder of the original PHP mailing list in spanish Master of the parrillaFriday, May 18, 2012 4. Friday, May 18, 2012 5. ServerGrove! Founded ServerGrove Networks in 2005 Provider of web hosting specialized in PHP,Symfony, ZendFramework, and others Mongohosting.com under beta!Friday, May 18, 2012 6. Community is our teacher Very active open source supporter through codecontributions and usergroups/conference sponsoringFriday, May 18, 2012 7. Agenda - Introduction to MongoDB - PHP and MongoDB - PHP Libraries - Symfony2 and MongoDBFriday, May 18, 2012 8. What is MongoDB?Who is 10Gen?Friday, May 18, 2012 9. Mongo Mongo as in "humongous". Used to describe something extremely large or important.Friday, May 18, 2012 10. MongoDB is a scalable, high-performance,open source NoSQL database.- Document Oriented DB- Written in C++- Available for *nux (Linux, Solaris, etc), Windows and OS X- Lots of Drivers (PHP, Java, Python, Ruby...)Friday, May 18, 2012 11. Features- Flexible JSON-style documents- Full Indexing- Complex Queries / Map Reduce- Aggregation Framework (coming soon)- GridFS (store les natively)- Multiple Replication Options- Sharding- Simple Installation / Zero CongFriday, May 18, 2012 12. Document Oriented Coming from SQL? Database => Database Table => Collection Row => DocumentFriday, May 18, 2012 13. JSON-style documents{ name: { rst: John, last: Doe}, title: Engineer, age: 40}Friday, May 18, 2012 14. No Schema or xed tables {name: {rst: Foo,last: Bar },title: Student,school: Harvard}Friday, May 18, 2012 15. Embedded documents {"_id" : ObjectId("4ccba15ef597e9352e060000")"srcFilename" : "/etc/apache2/sites-enabled/example1.com","vhostDirective" :{ "directives" : [ { "name" : "CustomLog", "value" : "logs/example1.com-access_log combined" }, {"name" : "DocumentRoot","value" : "/var/www/vhosts/example1.com/httpdocs" }, {"name" : "ServerName","value" : "example1.com" } ] } }Friday, May 18, 2012 16. Document Referencing{"_id" : ObjectId("4cc4a5c3f597e9db6e010109"),"billingId" : NumberLong(650),"created" : ISODate("2010-10-24T21:31:47Z"),"servers" : [ { "$ref" : "server", "$id" : ObjectId("4cc4a5c4f597e9db6e050201") }],"users" : [ { "$ref" : "user", "$id" : ObjectId("4cc4a5c4f597e9db6e980201") }, { "$ref" : "user", "$id" : ObjectId("4cc4a5c4f597e9db6e9c0201") }]}Friday, May 18, 2012 17. Full Indexingdb.coll.ensureIndex({name.last: 1})db.coll.ensureIndex({name.rst: 1, name.last: 1})db.coll.ensureIndex({age: 0})Friday, May 18, 2012 18. Querying db.coll.nd({name: John}) db.coll.nd({keywords: storage}) db.coll.nd({keywords: {$in: [storage, DBMS]}}Friday, May 18, 2012 19. GridFS - Files are divided in chunks and stored over multiple documents - Transparent APIFriday, May 18, 2012 20. Replication Source: http://www.mongodb.org/display/DOCS/ReplicationFriday, May 18, 2012 21. Shards Source: http://www.mongodb.org/display/DOCS/IntroductionFriday, May 18, 2012 22. Simple Installation/Zero Cong OS Xwget http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-2.0.4.tgztar zxvf mongodb-osx-x86_64-2.0.4.tgzcd mongodb-osx-x86_64-2.0.4./mongodFriday, May 18, 2012 23. Simple Installation/Zero Cong CentOS Linux/etc/yum.repos.d/10gen.repo[10gen]name=10gen Repositorybaseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64gpgcheck=0$ yum install -y mongo-stable-server$ service mongod startFriday, May 18, 2012 24. Why is MongoDB good for Rapid Development of Web Apps?Friday, May 18, 2012 25. Rapid Development Schema-less / Document OrientedFLEXIBILITY by exfordyFriday, May 18, 2012 26. Rapid Development Schema-less / Document Oriented EASIER MIGRATIONS by exfordyFriday, May 18, 2012 27. Rapid Development NO JOINS!Friday, May 18, 2012 28. Performance SPEED by xavi talledaFriday, May 18, 2012 29. PerformanceSCALABILITY by Jimee, Jackie, Tom & AshaFriday, May 18, 2012 30. A Word of Caution No TransactionsNo Rollbacks Unsafe defaultsMap Reduce locksby Ernst VikneFriday, May 18, 2012 31. Great Use Cases - Content Management - Product Catalogs - Realtime Analytics - Logs StorageFriday, May 18, 2012 32. andFriday, May 18, 2012 33. PECL driver Linux pecl install mongo echo extension=mongo.so >> /path/php.ini OS X http://php-osx.liip.ch/ Windows https://github.com/mongodb/mongo-php-driver/downloadsFriday, May 18, 2012 34. Usage Friday, May 18, 2012 35. Storing Files Friday, May 18, 2012 36. SQL to Mongo Queries SQL to Mongo Mapping Chart This is a PHP-specific version of the SQL to Mongo mapping chart in the main docs.SQL Statement Mongo Query Language Statement CREATE TABLE USERS (a Number, b Number) Implicit or use MongoDB::createCollection(). INSERT INTO USERS VALUES(1,1) $db->users->insert(array("a" => 1, "b" => 1)); SELECT a,b FROM users $db->users->find(array(), array("a" => 1, "b" => 1)); SELECT * FROM users WHERE age=33 $db->users->find(array("age" => 33)); SELECT a,b FROM users WHERE age=33 $db->users->find(array("age" => 33), array("a" => 1, "b" => 1)); SELECT a,b FROM users WHERE age=33 ORDER BY name $db->users->find(array("age" => 33), array("a" => 1, "b" => 1))->sort(array("name" => 1)); SELECT * FROM users WHERE age>33 $db->users->find(array("age" => array($gt => 33))); SELECT * FROM users WHERE ageusers->find(array("age" => array($lt => 33))); SELECT * FROM users WHERE name LIKE "%Joe%" $db->users->find(array("name" => new MongoRegex("/Joe/"))); SELECT * FROM users WHERE name LIKE "Joe%" $db->users->find(array("name" => new MongoRegex("/^Joe/"))); SELECT * FROM users WHERE age>33 AND ageusers->find(array("age" => array($gt => 33, $lte => 40))); SELECT * FROM users ORDER BY name DESChttp://php.net/manual/en/mongo.sqltomongo.phpFriday, May 18, 2012 37. Admin Interfaces- Genghishttp://genghisapp.com/- RockMongohttp://code.google.com/p/rock-php/wiki/rock_mongo- php-mongodb-adminhttps://github.com/jwage/php-mongodb-adminFriday, May 18, 2012 38. PHP Libraries - Doctrine ODM - Mandango - many more...Friday, May 18, 2012 39. Doctrine MongoDB ODM http://doctrine-project.orgDoctrine MongoDB Object Document Mapper is built for PHP 5.3.2+ and provides transparent persistence for PHP objects.Friday, May 18, 2012 40. Doctrine MongoDB ODM/** @Document */class User{/** @Id */private $id;/** @String */private $name;/** @String */private $email;/** @ReferenceMany(targetDocument="BlogPost", cascade="all") */private $posts = array();// ...}Friday, May 18, 2012 41. Doctrine MongoDB ODM/** @Document */class BlogPost{/** @Id */private $id;/** @String */private $title;/** @String */private $body;/** @Date */private $createdAt;// ...}Friday, May 18, 2012 42. Doctrine MongoDB ODM