Service dependence injection not include manager symfony2

guys I'm trying ti include in one Form type for some reason the manager did not include in the constructor maybe it is a simple typo but for now i cannot see a error through the examples of the coookbook of symfony2 (2.7).

Here is the FormType FloorType

namespace George\FloorBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use George\FloorBundle\Form\DataTransformer\ObjectToNumberTransformer;
use Doctrine\Common\Persistence\ObjectManager;

class FloorType extends AbstractType
{
private $manager;

public function __construct(ObjectManager $manager)
{
    $this->manager = $manager;
}
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('translations', 'a2lix_translations',array(
            'required_locales' => array('bg','en')
        ))
    ->add('object', 'hidden', array(
        // validation message if the data transformer fails
       'invalid_message' => 'That is not a valid issue number',
    ));

    $builder ->get('object')->addModelTransformer(new ObjectToNumberTransformer($this->manager));
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'George\FloorBundle\Entity\Floor'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'george_floorbundle_floor';
}
}

The service which need to inject the manager

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
    <service id="app.form.type.floor" class="George\FloorBundle\Form\Type\FloorType">
        <tag name="form.type" />
        <argument type="service" id="doctrine.orm.entity_manager"></argument>
    </service>
</services>

The transformer which use the manager (but i got not errors in it i just want to make a full example of the case)

<?php
namespace George\FloorBundle\Form\DataTransformer;

use George\ObjectsBundle\Entity\Object;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;

class ObjectToNumberTransformer implements DataTransformerInterface
{
private $manager;

public function __construct(ObjectManager $manager)
{
    $this->manager = $manager;
}

/**
 * Transforms an object (issue) to a string (number).
 *
 * @param  Object|null $issue
 * @return string
 */
public function transform($object)
{

    if (null === $object) {
        return '';
    }

    return $object->getId();
}

/**
 * Transforms a string (number) to an object (issue).
 *
 * @return Object|null
 * @throws TransformationFailedException if object (issue) is not found.
 */
public function reverseTransform($objectNumber)
{
    // no issue number? It's optional, so that's ok
    if (!$objectNumber) {
        return;
    }

    $object= $this->manager
        ->getRepository('ObjectsBundle:Object')
        // query for the issue with this id
        ->find($objectNumber)
    ;

    if (null === $object) {
        // causes a validation error
        // this message is not shown to the user
        // see the invalid_message option
        throw new TransformationFailedException(sprintf(
            'An issue with number "%s" does not exist!',
            $objectNumber
        ));
    }

    return $object;
}
}

And the controller method in which i need to load the FloorType:

  private function createEditForm(Floor $entity)
{
    $manager = $this->getDoctrine()->getManager();
    $form = $this->createForm(new FloorType($manager), $entity, array(
        'action' => $this->generateUrl('admin_floor_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

So when i remove the manager:

$manager = $this->getDoctrine()->getManager();

I got a big fat error:

Catchable Fatal Error: Argument 1 passed to George\FloorBundle\Form\FloorType::__construct() must implement interface Doctrine\Common\Persistence\ObjectManager, none given, called in D:\work\infinity3\src\George\FloorBundle\Controller\FloorController.php on line 171 and defined

I understand it like this - the service did not manage to inject the manager

Debug the service in local bundle

I have debug the service and it is listed but i cannot manage it to include the manager in the FloorType what i have been missed here?

Edit

So it was the service xml problem i did not include the alias attribute thanks to @Matteo answer i edit the code and it works like a charm! Thank you @Matteo!

Here is the edit service

 <services>
    <service id="app.form.type.floor" class="George\FloorBundle\Form\FloorType">
        <tag name="form.type" alias="george_floorbundle_floor" />
        <argument type="service" id="doctrine.orm.entity_manager"></argument>
    </service>
</services>

And where i create the form:

 $form = $this->createForm('george_floorbundle_floor', $entity, array(
        'action' => $this->generateUrl('admin_floor_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

Answers


You should define an alias in the tags section as follow:

service.xml

<services>
    <service id="app.form.type.floor" class="George\FloorBundle\Form\Type\FloorType">
        <tag name="form.type" alias="george_floorbundle_floor" />
        <argument type="service" id="doctrine.orm.entity_manager"></argument>
    </service>
</services>

And refer in the form creation with the reference, as follow:

$form = $this->createForm('george_floorbundle_floor', $entity, array(
        'action' => $this->generateUrl('admin_floor_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

Hope this help


Need Your Help

Locale-specific formatting of time and date including milliseconds

java date locale milliseconds

I am trying to format a date with time including the milliseconds in Java specific to the current Locale, so for example in US/English Locale it should look like this: 10/7/13 05:24:12.121 PM. And in

Modify the DFS method so that it can detect if a Graph is a Tree or not

java graph tree depth-first-search

This DFS method has to be changed, so it can check a graph on having cycles in it. My problem is to code it, because of my little expeirience in coding. It looks like my post is almost code :D