Doctrine Mongo文档+嵌入表单集合

时间:2012-10-31 12:15:17

标签: mongodb symfony

使用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

表格代码:

namespace 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()));       
    }

}

当我尝试在控制器操作_

中持久保存对象时,表单崩溃
$this->persist($family);

我尝试了很多选项,但没有一个人有很好的结果。如果您有任何想法,请回复。

0 个答案:

没有答案
相关问题