M
M
MARDEN2012-12-20 10:15:38
symfony
MARDEN, 2012-12-20 10:15:38

Symfony 2 Forms + Doctrine MongoDb

I am writing a project on Symfony 2.1 in the Doctrine MongoDb bundle. There was a problem with forms.

There is a class Consultant:

/**
 * @MongoDB\Document
 */
class Consultant
{
    /**
     * @MongoDB\Id(strategy="NONE")
     */
    protected $id;

    /**
     * @MongoDB\EmbedMany(targetDocument="Specialization", strategy="set")
     */
    protected $specs;

    /**
     * @MongoDB\Hash
     */
    protected $schedule;

    // далее идут getters/setters
}


Specialization class:
/**
 * @MongoDB\Document
 */
class Specialization
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $name;

    public function __toString()
    {
        return $this->name;
    }

    // далее идут getters/setters
}


Having saved the object through the Doctrine DocumentManager, I got the following consultant structure in monge:
{
   "_id": "1",
   "specs": [
     {
       "_id": ObjectId("50d1c5116146a13948000000"),
       "name": "Юрист"
    },
     {
       "_id": ObjectId("50d069336146a10244000000"),
       "name": "Экономист"
    } 
   ], 
   "themes": ["Финансы", "Здоровье", "Семья", "Бизнес", "Недвижимость"]
}


Monga also has 2 collections of all specializations (specs) and themes (themes), which are separately created and edited.

The task is to show a form in which there will be 2 lists with checkboxes filled with data from the corresponding collections and marked with checkboxes for the items that the selected consultant has. When saving the form, the themes property should contain the marked names of topics, and the specs should contain the marked specializations, taking into account its structure, indicated above.
Form example:


Issue #1 : themes field ( @Hashdoctrine annotation).

Forms tested:
class ConsultantFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('themes', 'collection', array(
            'type' => 'choice',
            'options'  => array(
                'expanded' => true,
                'multiple' => true,
            ),
        ));
    }
}

Throws an exception Expected argument of type "array", "string" givenin the ChoicesToBooleanArrayTransformer.php file.

$builder->add('themes', 'choice', array(
            'choices' => array('Финансы', 'Здоровье', 'Семья', 'Бизнес'), // чисто для эксперимента
            'expanded' => true,
            'multiple' => true,
        ));

The form was displayed, but there were no selected items in the list, and the values ​​in the inputs turned out to be indexes (keys), not values.

Problem #2 : The specs( @EmbedMany) field.
Read the documentation .
The first is the assignment of data_class in the form options:
public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'        => 'Acme\ConsultantBundle\Document\Consultant',
        ));
    }

The second - I create a field with the document type (similar to entity, but for Mongo).
$builder->add('specs', 'document', array(
            'class' => 'AcmeConsultantBundle:Specialization',
            'property' => 'name',
            'expanded' => true,
            'multiple' => true,
        ));

The form displays a list of checkboxes, but again, none of them are checked. The role of value for inputs this time is the id of objects from the specs collection. This is despite the fact that data is written to the mongu as it should be.

People, please help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
JekaRu, 2012-12-22
@MARDEN

@EmbedMany do you really need it here?
It seems to me that @ReferenceMany is more suitable, your last definition should work with it correctly

J
JekaRu, 2012-12-20
@JekaRu

1. Probably should be like this: choices' => array('Finance'=>'Finance', ....)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question