Introducing PropertyInfo: a PHP component to find types and doc of properties

PHP Beer Mug

PHP doesn’t support explicit type definition. This is annoying, especially when doing meta programming. Various libraries including but not limited to Doctrine ORM and the Symfony Validator provide their own type managing system. Another common way to describe type(s) of a property is integrated documentation aka PHPDoc.

The PHP Property Info library extracts various information including the type and documentation from PHP class properties from metadata of popular sources including:

  • Setter method with type hint
  • PHPDoc’s DocBlock
  • Doctrine ORM mapping (annotation, XML, YML or custom format)

Thanks to the visitor pattern, adding support for other metadata source is easy, and Pull Requests are welcome!

As usual, this library is freely available (MIT license) on my GitHub page.

As you maybe know, I’m working on a new way to build hypermedia REST API with Symfony (an experimental release is already available: DunglasJsonLdApiBundle). This bundle will leverage PHP Property Info and the Symfony Serializer Component that will be shipped in Symfony 2.7 to allow powerful deserialization.

“Persistence in PHP with Doctrine ORM” eBook for $5 until January 6th 2015

Persistence in PHP with the Doctrine ORM

 

Thanks to Packt’s eBook Bonanza, my book “Persistence in PHP with Doctrine ORM” is available for only $5 until January 6th. Enjoy it!

What you will learn from this book

  • Install Doctrine through the Composer dependency manager
  • Configure Doctrine Command Line Tools
  • Learn to manage relations between entities with different association types
  • Create data fixtures, a custom entity repository, and native SQL queries
  • Master the query builder to generate DQL queries
  • Get started with inheritance and lifecycle events

In Detail

Doctrine 2 has become the most popular modern persistence system for PHP. It can either be used as a standalone system or can be distributed with Symfony 2, and it also integrates very well with popular frameworks. It allows you to easily retrieve PHP object graphs, provides a powerful object-oriented query language called DQL, a database schema generator tool, and supports database migration. It is efficient, abstracts popular DBMS, and supports PHP 5.3 features.

Doctrine is a must-have for modern PHP applications.

Persistence in PHP with the Doctrine ORM is a practical, hands-on guide that describes the full creation process of a web application powered by Doctrine. Core features of the ORM are explained in depth and illustrated by useful, explicit, and reusable code samples.

Persistence in PHP with the Doctrine ORM explains everything you need to know to get started with Doctrine in a clear and detailed manner.

From installing the ORM through Composer to mastering advanced features such as native queries, this book is a full overview of the power of Doctrine. You will also learn a bunch of mapping annotations, create associations, and generate database schemas from PHP classes. You will also see how to write data fixtures, create custom entity repositories, and issue advanced DQL queries. Finally it will teach you to play with inheritance, write native queries, and use built-in lifecycle events. If you want to use a powerful persistence system for your PHP application, Persistence in PHP with the Doctrine ORM is the book you.

Approach

Persistence in PHP with the Doctrine ORM is a concise, fast, and focused guide to build a blog engine with advanced features such as native queries and lifecycle callbacks.

Who this book is for

This book is primarily intended for PHP developers and architects who want to increase their skills in the field of Persistence and ORM to map the data they are working on to objects they are using in programming. Basic knowledge of databases and PDO and working knowledge of PHP namespaces is a prerequisite.

 

Two free chapters are available on the Pack website.

PHP Schema: generate a fully functional PHP / Doctrine / Symfony data model from Schema.org vocabulary in minutes

PHP Schema is a code generator that instantly generates a PHP data model from the Schema.org vocabulary : browse Schema.org, choose the types and properties you need, run our code generator and you’re done! You get a fully featured PHP data model including:

  • A set of PHP entities with properties, constants (enum values), getters, setters, adders and removers. The class hierarchy provided by Schema.org will be translated to a PHP class hierarchy with parents as abstract classes. The generated code complies with PSR coding standards.
  • Full high-quality PHPDoc for classes, properties, constants and methods extracted from Schema.org.
  • Doctrine ORM annotation mapping including database columns with type guessing, relations with cardinality guessing, class inheritance (through the @AbstractSuperclass annotation).
  • Data validation through Symfony Validator annotations including data type validation, enum support (choices) and check for required properties.
  • Interfaces and Doctrine ResolveTargetEntityListener support.
    Custom PHP namespace support.
  • List of values provided by Schema.org with PHP Enum classes.

Bonus:

  • The code generator is fully configurable and extensible: all features can be deactivated (e.g.: the Doctrine mapping generator) and custom generator can be added (e.g.: a Doctrine ODM mapping generator).
  • The generated code can be used as is in a Symfony app (but it will work too in a raw PHP project or any other framework including Laravel and Zend Framework).

Why use Schema.org data to generate a PHP model?

Don’t Reinvent The Wheel

Data models provided by Schema.org are popular and have been proved efficient. They cover a broad spectrum of topics including creative work, e-commerce, event, medicine, social networking, people, postal address, organization, place or review. Schema.org has its root in a ton of preexisting well designed vocabularies and is successfully used by more and more website and applications.

Pick up schemas applicable to your application, generate your PHP model, then customize and specialize it to fit your needs.

Improve SEO and user experience

Adding Schema.org markup to websites and apps increase their ranking in search engines results and enable awesome features such as Google Rich Snippets and Gmail markup.

Mapping your app data model to Schema.org structures can be a tedious task. Using the generator, your data model will be a derived from Schema.org. Adding microdata markup to your templates or serializing your data as JSON-LD will not require specific mapping nor adaptation. It’s a matter of minutes.

Be ready for the future

Schema.org improves the interoperability of your applications. Used with hypermedia technologies such as Hydra it’s a big step towards the semantic and machine readable web. It opens the way to generic web API clients able to extract and process data from any website or app using such technologies.

Connection to a MS SQL Server from Symfony / Doctrine on Mac or Linux

Microsoft provides a SQL Server driver for PDO. Unfortunately, this driver only works on Windows. Linux and Mac OS X apps must use the FreeTDS compatibility layer: an open source implementation of the MS SQL Server protocol for Unix.

It’s possible to connect a Symfony app to a SQL Server instance on Unix through FreeTDS but this involve to use a Doctrine driver that is not provided with the standard distribution. Some tutorials already explain how to do that, but they encourage doing dirty things like editing files in the vendor/ directory. Here is the clean way!

First, install FreeTDS.

On Mac OX X, use Homebrew:
brew install freetds

On Debian or Ubuntu:
apt-get install freetds-bin

The next step is to configure FreeTDS to be able to connect to the SQL Server instance.

Open the freetds.conf file (/etc/freetds/freetds.conf on Debian / Ubuntu and /usr/local/etc/freetds.conf on Mac OS X) and add the connection details of your server:

[my_server]
host = sql.example.com
port = 1433
tds version = 8.0
client charset = UTF-8
text size = 20971520

Be sure to set the protocol version to 8.0, the client charset and the text size.

Now, you should be able to connect to the SQL server from the command line:
tsql -S my_server -U myusername

Type your password when asked and Ctrl+D to disconnect from the server.

It’s time to install the DBLIB PDO Driver.

On Mac OS X (replace php55 by the version of PHP you are using):
brew install php55-pdo-dblib

On Debian or Ubuntu:
apt-get install php5-sybase

And add the DBLIB driver for Doctrine (packaged in a Symfony bundle) in your app:

# in your Symfony app directory
composer require realestateconz/mssql-bundle:dev-master

Enable the Symfony bundle. Add this line in the registerBundles() method of your AppKernel in app/AppKernel.php:

Finally, configure Doctrine to use this driver. Edit app/config/config.yml:

Note that you must use the driver_class parameter, and not driver. Of course, you should not hardcode these values. Use the interactive parameters system instead.

Your Symfony app is now able to connect to the SQL Server. Try to run a SQL query:
php app/console doctrine:query:sql "SELECT * FROM MY_TABLE"

As SQL Server is a bad default DBMS for a Symfony app, especially on Unix servers, you should be interested by using multiple database connection with Symfony and Doctrine.

DunglasTodoMVCBundle compatible with Symfony 2.4

I’m pleased to announce the release of DunglasTodoMVCBundle 1.1.0.

Screenshot Symfony Todomvc

DunglasTodoMVCBundle is an implementation of TodoMVC, the popular site providing the same todo app implemented in a ton of different JavaScript frameworks.

This bundle provides a REST / JSON API built with Symfony and Doctrine, and an API client built with Chaplin.js and Backbone.js.

In this new release:

Enjoy!

Entities and Mapping Information with the Doctrine ORM

Persistence in PHP with the Doctrine ORM

Another free chapter of my book Persistence in PHP with the Doctrine ORM is available on the Pack Website. In this second chapter of the book, you will:

  • Create your first Doctrine entity class
  • Mapping it to its related database table and columns with annotations
  • Use Doctrine Command Line Tools to automatically generate the database schema
  • Create some fixtures data
  • deal with the Entity Manager

To get started with Doctrine entities, read this chapter on the PacktLib.

Building Doctrine queries with DQL and the query builder

Persistence in PHP with the Doctrine ORM

A chapter of my book Persistence in PHP with Doctrine ORM is available for free on the Pack website.

This chapter explains some interesting Doctrine concepts including DQL (the proprietary SQL-like language of the Doctrine ORM), entity repositories, the query builder and aggregate functions.

Read this free chapter on the Packt website.

Persistence in PHP with the Doctrine ORM: my new book in pre-order

Persistence in PHP with the Doctrine ORM

My new book about the Doctrine project is pre-orderable on the Packt website. It will be available in late december. A free sample chapter will be published shortly.

What you will learn from this book

  • Install Doctrine through the Composer dependency manager
  • Configure Doctrine Command Line Tools
  • Learn to manage relations between entities with different association types
  • Create data fixtures, a custom entity repository, and native SQL queries
  • Master the query builder to generate DQL queries
  • Get started with inheritance and lifecycle events

In Detail

Doctrine 2 has become the most popular modern persistence system for PHP. It can either be used as a standalone system or can be distributed with Symfony 2, and it also integrates very well with popular frameworks. It allows you to easily retrieve PHP object graphs, provides a powerful object-oriented query language called DQL, a database schema generator tool, and supports database migration. It is efficient, abstracts popular DBMS, and supports PHP 5.3 features.

Doctrine is a must-have for modern PHP applications.

Persistence in PHP with the Doctrine ORM is a practical, hands-on guide that describes the full creation process of a web application powered by Doctrine. Core features of the ORM are explained in depth and illustrated by useful, explicit, and reusable code samples.

Persistence in PHP with the Doctrine ORM explains everything you need to know to get started with Doctrine in a clear and detailed manner.

From installing the ORM through Composer to mastering advanced features such as native queries, this book is a full overview of the power of Doctrine. You will also learn a bunch of mapping annotations, create associations, and generate database schemas from PHP classes. You will also see how to write data fixtures, create custom entity repositories, and issue advanced DQL queries. Finally it will teach you to play with inheritance, write native queries, and use built-in lifecycle events. If you want to use a powerful persistence system for your PHP application, Persistence in PHP with the Doctrine ORM is the book you.

Approach

Persistence in PHP with the Doctrine ORM is a concise, fast, and focused guide to build a blog engine with advanced features such as native queries and lifecycle callbacks.

Who this book is for

This book is primarily intended for PHP developers and architects who want to increase their skills in the field of Persistence and ORM to map the data they are working on to objects they are using in programming. Basic knowledge of databases and PDO and working knowledge of PHP namespaces is a prerequisite.

A new Facebook Connect plugin for symfony 1: kdDoctrineGuardFacebookConnectPlugin

After some tests I realized that the plugin sfFacebookConnectPlugin created by fabriceb was no longer functional or maintained. I tried to correct some bugs but it’s a pain: the plugin is not adapted to the new version of the Facebook SDK.

So I have created new plugin called kdDoctrineGuardFacebookConnectPlugin available on GitHub or in the symfony 1 plugins repository. It provides a simple an easy way to add Facebook Connect support to your symfony 1 app and it works ! The code base is very small, easy to understand and has a voluntary limitation: only with sfDoctrineGuard auth method is supported (no Propel backend).

kdDoctrineGuardFacebookConnectPlugin adds a “Login with Facebook” button to the sfGuard signin form, automatically creates a sfGuard user and populates it with Facebook provided data (first name, last name, gender, location, hometown, Facebook profile link, …), and updates the profile if some data are changed on Facebook and provides a clean logout mechanism (from your app and from Facebook).

Downloads and installation instructions on the plugin GitHub page!

If  you are already using Symfony2, take a look at the noelg’s FacebookBundle.