3

symfony 2 と mongoDB を使用したプロジェクト。

私はこのチュートリアルに従っています: http://symfony.com/doc/current/cookbook/form/form_collections.html

しかし、フォームを保存すると、次のエラーが発生します。

Cannot create a DBRef, the document is not an object

クラッシュの行: https://github.com/doctrine/mongodb-odm/blob/master/lib/Doctrine/ODM/MongoDB/DocumentManager.php#L691

フォームコード:

名前空間 Fonts\FontsBundle\Form\Type;

Symfony\Component\Form\AbstractType を使用します。Symfony\Component\Form\FormBuilderInterface を使用します。

class FamilyType extends AbstractType
{

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

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('name', 'text', array('max_length' => 50, 'error_bubbling' => true));
        $builder->add('fonts', 'collection', array(
            'type' => new FontType($this->dm),
            'allow_add' => true,
            'by_reference' => false,
            ));     


    }

    public function getName()
    {
        return 'Family';
    }
}

コントローラーコード:

namespace Fonts\FontsBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

use Fonts\FontsBundle\Document\Family;
use Fonts\FontsBundle\Document\Font;

use Fonts\FontsBundle\Form\Type\FamilyType;

class FamilyBackendController extends BaseController
{

    public function newAction()
    {
        try {

            $family = new Family();

            $form = $this->createForm(new FamilyType($this->getMongoService()), $family);

            $request = $this->getRequest();
            if ($request->getMethod() == 'POST')
            {
                $form->bind($request);

                if ($form->isValid())
                {

                    $this->persist($family);

                    $this->get('session')->setFlash('notice', 'Item successfully created.');
                    return ($request->request->get('save') === 'Save') ? 
                        new RedirectResponse($this->generateUrl('backend_familys_list')) : 
                        new RedirectResponse($this->generateUrl('backend_familys_new'));
                }
            }

            return $this->render('FontsBundle:Backend:newFamily.html.twig', array(
                'form' => $form->createView(),
            ));

        } catch(\Exception $e) {
            return new Response($e->getMessage());
        }
    }

}

書類:

<?php

namespace Fonts\FontsBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Doctrine\Common\Collections\ArrayCollection;

use Symfony\Component\Validator\Constraints as Assert;

use Fonts\FontsBundle\Service\SlugService;

/**
 * @MongoDB\Document(repositoryClass="Fonts\FontsBundle\Repository\FamilyRepository")
 */

class Family
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     * @MongoDB\UniqueIndex(safe=true)
     * @Assert\NotBlank(message="FamilyName value should not be blank.")
     * @Assert\MinLength(limit=3,message="FamilyName must have at least {{ limit }} characters.")
     * @Assert\MaxLength(limit=50,message="FamilyName must have maximum {{ limit }} characters.")
     */
    protected $name;

    /**
     * @MongoDB\ReferenceMany(targetDocument="Font", simple=true)
     * @Assert\NotBlank(message="Fonts should not be blank.")
     */
    protected $fonts;

    /**
     * @MongoDB\String
     * @MongoDB\UniqueIndex(safe=true)
     */
    protected $slug;

    /**
     * @MongoDB\Int
     */
    protected $createdAt;

    public function __construct()
    {
        $this->fonts = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Get id
     *
     * @return id $id
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     * @return Family
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * Get name
     *
     * @return string $name
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Add fonts
     *
     * @param Fonts\FontsBundle\Document\Font $fonts
     */
    public function addFonts(\Fonts\FontsBundle\Document\Font $fonts)
    {
        $this->fonts[] = $fonts;
    }

    /**
     * Set fonts
     *
     * @param Doctrine\Common\Collections\Collection $fonts
     */
    public function setFonts($fonts)
    {
        $this->fonts = $fonts;
    }

    /**
     * Get fonts
     *
     * @return Doctrine\Common\Collections\Collection $fonts
     */
    public function getFonts()
    {
        return $this->fonts;
    }

    /**
     * Set slug
     *
     * @param string $slug
     * @return Family
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;
        return $this;
    }

    /**
     * Get slug
     *
     * @return string $slug
     */
    public function getSlug()
    {
        return $this->slug;
    }

    /**
     * Set createdAt
     *
     * @param int $createdAt
     * @return Family
     */
    public function setCreatedAt($createdAt)
    {
        $this->createdAt = $createdAt;
        return $this;
    }

    /**
     * Get createdAt
     *
     * @return int $createdAt
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * @MongoDB\PrePersist
     */
    public function prePersist()
    {
        $this->setCreatedAt(time());
        $slugService = new SlugService();
        $this->setSlug($slugService->slug($this->getName()));       
    }

    /**
     * @MongoDB\PreUpdate
     */
    public function preUpdate()
    {
        $slugService = new SlugService();
        $this->setSlug($slugService->slug($this->getName()));       
    }

}

コントローラー action_ でオブジェクトを永続化しようとするとフォームがクラッシュする

$this->persist($family);

私は多くのオプションを試しましたが、良い結果を得た人はいません。少しでもお分かりになりましたらご回答お願い致します。

4

0 に答える 0