Skip to content

Latest commit

 

History

History
130 lines (97 loc) · 5.7 KB

namespaces.markdown

File metadata and controls

130 lines (97 loc) · 5.7 KB
layout title
documentation
How to Use PHP 5.3 Namespaces

How to Use PHP 5.3 Namespaces

The generated model classes can use a namespace. It eases the management of large database models, and makes the Propel model classes integrate with PHP 5.3 applications in a clean way.

Namespace Declaration And Inheritance

To define a namespace for a model class, you just need to specify it in a namespace attribute of the <table> element for a single table, or in the <database> element to set the same namespace to all the tables.

Here is an example schema using namespaces:

{% highlight xml %}

{% endhighlight %}

The <database> element defines a namespace attribute. The book and author tables inherit their namespace from the database, therefore the generated classes for these tables will be \Bookstore\Book and \Bookstore\Author.

The publisher table defines a namespace attribute on its own, which extends the database namespace. That means that the generated class will be \Bookstore\Book\Publisher.

As for the user table, it defines an absolute namespace (starting with a backslash), which overrides the database namespace. The generated class for the user table will be Admin\User.

Tip
You can use subnamespaces (i.e. namespaces containing backslashes) in the namespace attribute.

Using Namespaced Models

Namespaced models benefit from the Propel runtime autoloading just like the other model classes. You just need to alias them, or to use their fully qualified name.

{% highlight php %}

setAuthor($author); $book->save(); {% endhighlight %} The namespace is used for the ActiveRecord class, but also for the Query and Peer classes. Just remember that when you use relation names ina query, the namespace should not appear: {% highlight php %} useBookQuery() ->filterByPrice(array('max' => 10)) ->endUse() ->findOne(); {% endhighlight %} Related tables can have different namespaces, it doesn't interfere with the functionality provided by the object model: {% highlight php %} findOne(); echo get_class($book->getPublisher()); // \Bookstore\Book\Publisher {% endhighlight %} >**Tip**
Using namespaces make generated model code incompatible with versions of PHP less than 5.3. Beware that you will not be able to use your model classes in an older PHP application. ## Using Namespaces As A Directory Structure ## In a schema, you can define a `package` attribute on a `` or a `` tag to generate model classes in a subdirectory (see [Multi-Component](multi-component-data-model.html)). If you use namespaces to autoload your classes based on a SplClassAutoloader (see [http://groups.google.com/group/php-standards](http://groups.google.com/group/php-standards)), then you may find yourself repeating the `namespace` data in the `package` attribute: {% highlight xml %} {% endhighlight %} To avoid such repetitions, just set the `propel.namespace.autoPackage` setting to `true` in your `build.properties`: {% highlight ini %} propel.namespace.autoPackage = true {% endhighlight %} Now Propel will automatically create a `package` attribute, and therefore distribute model classes in subdirectories, based on the `namespace` attribute, and you can omit the manual `package` attribute in the schema: {% highlight xml %} {% endhighlight %}