PHPackages                             tobias/zend-form-doctrine - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. tobias/zend-form-doctrine

AbandonedArchivedLibrary[Database &amp; ORM](/categories/database)

tobias/zend-form-doctrine
=========================

Use Zend\\Form\\Element with Doctrine objects

04PHP

Since Jul 1Pushed 6y agoCompare

[ Source](https://github.com/tobias-trozowski/zend-form-doctrine)[ Packagist](https://packagist.org/packages/tobias/zend-form-doctrine)[ RSS](/packages/tobias-zend-form-doctrine/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

Zend Form Doctrine
==================

[](#zend-form-doctrine)

[![Build Status](https://camo.githubusercontent.com/d5470811579a8c2e0aa7e4c31e5c7bc511c9df92417567ae21bb14665e57470f/68747470733a2f2f7472617669732d63692e636f6d2f746f626961732d74726f7a6f77736b692f7a656e642d666f726d2d646f637472696e652e737667)](https://travis-ci.com/tobias-trozowski/zend-form-doctrine)

Inspired and based on the famous [DoctrineModule](https://github.com/doctrine/DoctrineModule).

This package comes with functionality that can automatically fill the `ValueOptions` of Select, MultiCheckbox or Radio Form Elements with data from a `ObjectRepository`.

### Usage

[](#usage)

Add a `Tobias\Zend\Form\Doctrine\Element\ObjectSelect`, `Tobias\Zend\Form\Doctrine\Element\ObjectRadio` or `Tobias\Zend\Form\Doctrine\Element\ObjectMultiCheckbox` to your Form. For this to work, you need to specify at least an `object_manager`, the `target_class` to use and a `property` of the class to use as the Label.

#### Example 1 : simple example

[](#example-1--simple-example)

```
namespace Module\Form;

use Zend\Form\Form;
use Doctrine\Common\Persistence\ObjectManager;

class MyForm extends Form
{
    protected $objectManager;

    public function init()
    {
        $this->add([
            'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
            'name' => 'name',
            'options' => [
                'object_manager' => $this->getObjectManager(),
                'target_class'   => 'Module\Entity\SomeEntity',
                'property'       => 'property',
            ],
        ]);
    }
}
```

When the Form gets rendered the `findAll` method of the `ObjectRepository` will be executed by default.

### Example 2 : modifying the label

[](#example-2--modifying-the-label)

In times you want to change the display of the label you will need to use the `label_generator` option. This option allows you to modify the label as much as you like. In this simple example i will concatenate two properties with a dash.

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'name',
    'options' => [
        'object_manager'  => $this->getObjectManager(),
        'target_class'    => 'Module\Entity\SomeEntity',
        'label_generator' => function ($targetEntity) {
            return $targetEntity->getId() . ' - ' . $targetEntity->getTitle();
        },
    ],
]);
```

The callable function will always receive the target entity as a parameter so you will be able to use all functionalities your entities provide. Another example would be to completely switch out the labels in case your website has specific options to provide more accessible labels.

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'name',
    'options' => [
        'object_manager'  => $this->getObjectManager(),
        'target_class'    => 'Module\Entity\SomeEntity',
        'label_generator' => function ($targetEntity) use ($someSession) {
            if ('accessible' === $someSession->getCurrentMode()) {
                return $targetEntity->getAccessibleLabel();
            }

            return $targetEntity->getLabel();
        },
    ],
]);
```

### Example 3 : extended version

[](#example-3--extended-version)

If you don't need or want the entire repository you can specify a `find_method`to use. This method must exist in the repository. The following example executes the `findBy` method and passes in the specified parameters, but when using custom repositories you can do even more advanced queries! Also you can specify a method as a property by setting `is_method` to true.

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'name',
    'options' => [
        'object_manager' => $this->getObjectManager(),
        'target_class'   => 'Module\Entity\User',
        'property'       => 'ComposedOfSeveralProperties',
        'is_method'      => true,
        'find_method'    => [
            'name'   => 'findBy',
            'params' => [
                'criteria' => ['active' => 1],

                // Use key 'orderBy' if using ORM
                'orderBy'  => ['lastname' => 'ASC'],

                // Use key 'sort' if using ODM
                'sort'  => ['lastname' => 'ASC'],
            ],
        ],
    ],
]);
```

### Example 4 : including an empty option

[](#example-4--including-an-empty-option)

If you want to include an empty option at the top, set the `display_empty_item`setting to true. You can also specify the `empty_item_label` setting, the default is an empty string.

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'name',
    'options' => [
        'object_manager'     => $this->getObjectManager(),
        'target_class'       => 'Module\Entity\SomeEntity',
        'property'           => 'property',
        'display_empty_item' => true,
        'empty_item_label'   => '---',
    ],
]);
```

### Example 5 : Add html attributes to the elements

[](#example-5--add-html-attributes-to-the--elements)

To set custom HTML attributes on each `valueOption` you can use the `option_attributes` setting to specify an array of key/value pairs whereby the keys represent a valid HTML attribute (data-*, aria-*, onEvent, etc.).

The value needs to be of type `string` or `callable` (in which case a `string` - or something able to be casted to string - needs to be returned). Check the following example:

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'test',
    'options' => [
        'object_manager'    => $this->getObjectManager(),
        'target_class'      => 'Module\Entity\SomeEntity',
        'property'          => 'property',
        'option_attributes' => [
            'class'   => 'styledOption',
            'data-id' => function (\Module\Entity\SomeEntity $entity) {
                return $entity->getId();
            },
        ],
    ],
]);
```

The above example will generate HTML options with a data-key attribute:

```

    property one
    property two

```

It is noteworthy that, when working with an option\_attribute value of type `callable`, you do **not** need to define the fully qualified classname into the function. The object passed into the function will always be identical to the type you define on the key `target_class`.

### Example 6: Implementing `` support

[](#example-6-implementing-optgroup-support)

Once lists become larger there's a big user-experience bonus when lists are groupt using the html attribute. This package provides this functionality with the `optgroup_identifier`.

The assumption this package does however is that your data structure has the optgroup-grouping in mind. See the following example:

**Add the Select list like this:**

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'name',
    'options' => [
        'object_manager'      => $this->getObjectManager(),
        'target_class'        => 'Module\Entity\SomeEntity',
        'property'            => 'property',
        'optgroup_identifier' => 'category',
    ],
]);
```

**With your data structure like this:**

```
id  | property   | category
1   | Football   | sports
2   | Basketball | sports
3   | Spaghetti  | food

```

**Will create a HTML Select list like this:**

```

        Football
        Basketball

        Spaghetti

```

### Example 7: formatting on empty optgroups

[](#example-7--formatting-on-empty-optgroups)

In case you define an `optgroup_identifier` and the data inside this column is empty or `null` you have two options of rendering these cases. From a UX point of view you should group all "loose" entries inside a group that you call "others" or the likes of that. But you're also able to render them without any grouping at all. Here's both examples:

#### 7.1: Rendering without a default group

[](#71-rendering-without-a-default-group)

To render without a default group you have to change nothing. This is the default behavior

**Add the Select list like this:**

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'name',
    'options' => [
        'object_manager'      => $this->getObjectManager(),
        'target_class'        => 'Module\Entity\SomeEntity',
        'property'            => 'property',
        'optgroup_identifier' => 'category',
    ],
]);
```

**With your data structure like this:**

```
id  | property   | category
1   | Football   | sports
2   | Basketball |
3   | Spaghetti  | food

```

**Will create a HTML Select list like this:**

```

        Football

        Spaghetti

    Basketball

```

Notice how the value for "Basketball" has not been wrapped with an `` element.

#### 7.2: Rendering with a default group

[](#72-rendering-with-a-default-group)

To group all loose values into a unified group, simply add the `optgroup_default` parameter to the options.

**Add the Select list like this:**

```
$this->add([
    'type' => 'Tobias\Zend\Form\Doctrine\Element\ObjectSelect',
    'name' => 'name',
    'options' => [
        'object_manager'      => $this->getObjectManager(),
        'target_class'        => 'Module\Entity\SomeEntity',
        'property'            => 'property',
        'optgroup_identifier' => 'category',
        'optgroup_default'    => 'Others',
    ],
]);
```

**With your data structure like this:**

```
id  | property   | category
1   | Football   | sports
2   | Basketball |
3   | Spaghetti  | food

```

**Will create a HTML Select list like this:**

```

        Football

        Basketball

        Spaghetti

```

###  Health Score

18

—

LowBetter than 8% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity3

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity37

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0355d9a2db10711c7f3d0ac8c97e9fbda01838b80455740b98f5a8b335b921cc?d=identicon)[tobias-trozowski](/maintainers/tobias-trozowski)

---

Top Contributors

[![tobias-trozowski](https://avatars.githubusercontent.com/u/3001979?v=4)](https://github.com/tobias-trozowski "tobias-trozowski (2 commits)")

### Embed Badge

![Health badge](/badges/tobias-zend-form-doctrine/health.svg)

```
[![Health](https://phpackages.com/badges/tobias-zend-form-doctrine/health.svg)](https://phpackages.com/packages/tobias-zend-form-doctrine)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
