Symfony2.1动态/扩展形式没有有效的附加部分

时间:2012-05-01 13:50:17

标签: symfony

我确实使用symfony2.1并且我有一个表格,我确实用另一种形式扩展,主要部分(来自第一个实体[ToolCategory])确实正确有效,但另外一个没有({{1 }})。这是代码:

ToolCategory

ToolDocument

ToolDocument

namespace Einder\Has\ToolsBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

use Einder\DoctrineBundle\Entity\ActiveInterface;

/**
 * Einder\Has\ToolsBundle\Entity\ToolCategory
 *
 * @ORM\Table(name="tool_category")
 * @ORM\Entity(repositoryClass="Einder\Has\ToolsBundle\Repository\ToolCategoryRepository")
 * @ORM\HasLifecycleCallbacks
 */
class ToolCategory implements ActiveInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @Assert\NotBlank()
     */
    private $name;

    /**
     * @var text $target
     *
     * @ORM\Column(name="target", type="text")
     * @Assert\NotBlank()
     */
    private $target;

    /**
     * @var text $activity
     *
     * @ORM\Column(name="activity", type="text")
     * @Assert\NotBlank()
     */
    private $activity;

    /**
     * @var boolean $active
     *
     * @ORM\Column(name="active", type="boolean", nullable=true)
     */
    private $active = true;

    /**
     * @ORM\OneToMany(targetEntity="ToolDocument", mappedBy="toolCategory", cascade={"persist", "remove"})
     */
    private $documents;

    /**
     * @var boolean $createAnother
     *
     * @Assert\Type(type="boolean")
     */
    private $createAnother = false;

    /**
     * @var datetime $createdAt
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;

    /**
     * @var datetime $updatedAt
     *
     * @ORM\Column(name="updated_at", type="datetime")
     */
    private $updatedAt;


    /**
     * Constructor
     */
    public function __construct()
    {
        $this->documents = new ArrayCollection();
    }

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

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

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

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

    /**
     * Set target
     *
     * @param text $target
     */
    public function setTarget($target)
    {
        $this->target = $target;
    }

    /**
     * Get target
     *
     * @return text 
     */
    public function getTarget()
    {
        return $this->target;
    }

    /**
     * Set activity
     *
     * @param text $activity
     */
    public function setActivity($activity)
    {
        $this->activity = $activity;
    }

    /**
     * Get activity
     *
     * @return text 
     */
    public function getActivity()
    {
        return $this->activity;
    }

    /**
     * Set active
     *
     * @param boolean $active
     */
    public function setActive($active = true)
    {
        $this->active = $active;
    }

    /**
     * Get active
     *
     * @return boolean 
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Add documents
     *
     * @param Einder\Has\ToolsBundle\Entity\ToolDocument $documents
     */
    public function addToolDocument(\Einder\Has\ToolsBundle\Entity\ToolDocument $documents)
    {
        $this->documents[] = $documents;
    }

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

    /**
     * Set createAnother
     *
     * @param boolean $createAnother
     */
    public function setCreateAnother($createAnother)
    {
        $this->createAnother = $createAnother;
    }

    /**
     * Get createAnother
     *
     * @return boolean
     */
    public function getCreateAnother()
    {
        return $this->createAnother;
    }

    /**
     * Set createdAt
     *
     * @ORM\PrePersist 
     */
    public function setCreatedAt()
    {
        $this->createdAt = new \DateTime();
    }

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

    /**
     * Set updatedAt
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setUpdatedAt()
    {
        $this->updatedAt = new \DateTime();
    }

    /**
     * Get updatedAt
     *
     * @return datetime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    public function setDocuments($documents) {
        $this->documents = $documents;

        foreach ($documents as $document) {
            $document->setToolCategory($this);
        }
    }
}

ToolCategoryType

namespace Einder\Has\ToolsBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

use Einder\DoctrineBundle\Entity\EntityInterface;

/**
 * Einder\Has\ToolsBundle\Entity\ToolDocument
 *
 * @ORM\Table(name="tool_document")
 * @ORM\Entity(repositoryClass="Einder\Has\ToolsBundle\Repository\ToolDocumentRepository")
 * @ORM\HasLifecycleCallbacks
 */
class ToolDocument implements EntityInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @Assert\File(maxSize="6000000")
     */
    public $file;

    /**
     * @ORM\Column(type="string", length=255)
     * @Assert\NotBlank()
     * @Assert\NotNull()
     * @Assert\MinLength(10)
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $path;

    /**
     * @var boolean $active
     *
     * @ORM\Column(name="active", type="boolean", nullable=true)
     */
    private $active = true;

    /**
     * @var boolean $deleted
     *
     * @ORM\Column(name="deleted", type="boolean", nullable=true)
     */
    private $deleted = false;

    /**
     * @var datetime $deletedAt
     *
     * @ORM\Column(name="deleted_at", type="boolean", nullable=true)
     */
    private $deletedAt;

    /**
     * @ORM\ManyToOne(targetEntity="ToolCategory", inversedBy="documents")
     * @ORM\JoinColumn(name="tool_category_id", referencedColumnName="id")
     */
    private $toolCategory;

    /**
     * @var datetime $createdAt
     *
     * @ORM\Column(name="created_at", type="datetime")
     */
    private $createdAt;


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

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

    public function getAbsolutePath()
    {
        return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded documents should be saved
        return __DIR__.'/../../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw when displaying uploaded doc/image in the view.
        return '/uploads/tools';
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            // do whatever you want to generate a unique name
           $this->setPath(uniqid() . '.' . pathinfo($this->file->getClientOriginalName(), PATHINFO_EXTENSION));
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }

        // you must throw an exception here if the file cannot be moved
        // so that the entity is not persisted to the database
        // which the UploadedFile move() method does automatically
        $this->file->move($this->getUploadRootDir(), $this->path);

        unset($this->file);
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }

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

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

    /**
     * Set path
     *
     * @param string $path
     */
    public function setPath($path)
    {
        $this->path = $path;
    }

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

    /**
     * Set active
     *
     * @param boolean $active
     */
    public function setActive($active = true)
    {
        $this->active = $active;
    }

    /**
     * Get active
     *
     * @return boolean 
     */
    public function getActive()
    {
        return $this->active;
    }

    /**
     * Set deleted
     *
     * @param boolean $deleted
     */
    public function setDeleted($deleted = false)
    {
        $this->deleted = $deleted;
    }

    /**
     * Get deleted
     *
     * @return boolean 
     */
    public function getDeleted()
    {
        return $this->deleted;
    }

    /**
     * Get deletedAt
     *
     * @return datetime
     */
    public function getDeletedAt()
    {
        return $this->deletedAt;
    }

    /**
     * Set deletedAt
     *
     * @param boolean $deletedAt
     */
    public function setDeletedAt(\DateTime $deletedAt = null)
    {
        $this->deletedAt = $deletedAt;
    }

    /**
     * Set toolCategory
     *
     * @param Einder\Has\ToolsBundle\Entity\ToolCategory $toolCategory
     */
    public function setToolCategory($toolCategory)
    {
        $this->toolCategory = $toolCategory;
    }

    /**
     * Get toolCategory
     *
     * @return Einder\Has\ToolsBundle\Entity\ToolCategory 
     */
    public function getToolCategory()
    {
        return $this->toolCategory;
    }

    /**
     * Set createdAt
     *
     * @ORM\PrePersist 
     */
    public function setCreatedAt()
    {
        $this->createdAt = new \DateTime();
    }

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

    /**
     * Set updatedAt
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function setUpdatedAt()
    {
        $this->updatedAt = new \DateTime();
    }

    /**
     * Get updatedAt
     *
     * @return datetime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
}

ToolDocumentType

namespace Einder\Has\ToolsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

use Einder\Has\ToolsBundle\Form\Type\ToolDocumentType;

class ToolCategoryType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
                ->add('name', null, array('label' => 'tool.category.form.labels.name'))
                ->add('target', null, array('label' => 'tool.category.form.labels.target'))
                ->add('activity', null, array('label' => 'tool.category.form.labels.activity'))
                ->add('createAnother', null, array('label' => 'tool.category.form.labels.create_another'))
                ->add('documents', 'collection', array(
                                        'by_reference'  => false,
                                        'type'          => new ToolDocumentType(),
                                        'allow_add'     => true,
                                        'allow_delete'  => true,
                                        'prototype'     => true,
                                        'error_bubbling' => true
                ));
    }

    public function getDefaultOptions()
    {
        return array(
            'data_class' => 'Einder\Has\ToolsBundle\Entity\ToolCategory'
        );
    }

    public function getName()
    {
        return 'tool_category';
    }
}
namespace Einder\Has\ToolsBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class ToolDocumentType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name', null, array('label' => 'tool.document.form.labels.name'))
                ->add('file', null, array('label' => 'tool.document.form.labels.file'));
    }

    public function getDefaultOptions()
    {
        return array(
            'data_class' => 'Einder\Has\ToolsBundle\Entity\ToolDocument',
            'cascade_validation' => true,
        );
    }

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

感谢您的回答!

1 个答案:

答案 0 :(得分:1)

你应该

  • @Assert\Valid(首选解决方案)或
  • 设置ToolCategory::$documents约束
  • 父表单上设置cascade_validationtrue (即ToolCategoryType),而不是在孩子身上