Forms Symfony2-使用集合原型上载多个文件,文件应位于从属表中
我在通过symfony表单上传文件时遇到问题。当我只有一个实体并且想要上传文件时,一切都很好,但当我使用集合原型添加多个字段并且有源表和依赖表时,一切都会出错 源表Pro_配置文件 依亲表职业教育 我已经得到了Pro_profile的表单,希望使用prototype添加一些字段,因为这些字段是string、integer等,所以可以正常工作,但当我添加到集合文件字段,并希望向控制器添加一些代码以获取路径、上载等时,就不起作用了。我想原因是我使用controller for Pro_profile并希望将文件加载到不同的实体,但我不知道如何访问此Pro_教育实体 我找到的所有教程都谈到了当你们有一个实体时上传文件,但并没有提到上传到依赖表 你能帮我吗 Pro_profile.phpForms Symfony2-使用集合原型上载多个文件,文件应位于从属表中,forms,symfony,file-upload,doctrine,prototype,Forms,Symfony,File Upload,Doctrine,Prototype,我在通过symfony表单上传文件时遇到问题。当我只有一个实体并且想要上传文件时,一切都很好,但当我使用集合原型添加多个字段并且有源表和依赖表时,一切都会出错 源表Pro_配置文件 依亲表职业教育 我已经得到了Pro_profile的表单,希望使用prototype添加一些字段,因为这些字段是string、integer等,所以可以正常工作,但当我添加到集合文件字段,并希望向控制器添加一些代码以获取路径、上载等时,就不起作用了。我想原因是我使用controller for Pro_profile
namespace My\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Pro_profile
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="My\MainBundle\Entity\Pro_profileRepository")
*/
class Pro_profile
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="Pro_education", mappedBy="profile", cascade= {"persist"})
*/
protected $educations;
/**
* Constructor
*/
public function __construct()
{
$this->educations = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add educations
*
* @param \My\MainBundle\Entity\Pro_education $educations
* @return Pro_profile
*/
public function addEducation(\My\MainBundle\Entity\Pro_education $educations)
{
$educations->setProfile($this);
$this->educations->add($educations);
return $this;
}
/**
* Remove educations
*
* @param \My\MainBundle\Entity\Pro_education $educations
*/
public function removeEducation(\My\MainBundle\Entity\Pro_education $educations)
{
$this->educations->removeElement($educations);
}
/**
* Get educations
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEducations()
{
return $this->educations;
}
Pro_education.php
namespace My\MainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Pro_education
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="My\MainBundle\Entity\Pro_educationRepository")
*/
class Pro_education
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=128, nullable=true)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=64, nullable=true)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=128, nullable=true)
*/
private $path;
/**
* @Assert\File(maxSize="6000000")
*/
public $file;
/**
* @var boolean
*
* @ORM\Column(name="verified", type="boolean", nullable=true)
*/
private $verified;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=128, nullable=true)
*/
private $city;
/**
* @ORM\ManyToOne(targetEntity="Pro_profile", inversedBy="educations")
* @ORM\JoinColumn(name="profile_id", referencedColumnName="id", nullable=false)
*/
protected $profile;
/**
* Get city
*
* @return string
*/
public function getCity()
{
return $this->city;
}
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/documents';
}
public function upload()
{
// the file property can be empty if the field is not required
if (null === $this->file) {
return;
}
// we use the original file name here but you should
// sanitize it at least to avoid any security issues
// move takes the target directory and then the target filename to move to
$this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
// set the path property to the filename where you'ved saved the file
$this->path = $this->file->getClientOriginalName();
// clean up the file property as you won't need it anymore
$this->file = null;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Pro_education
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set title
*
* @param string $title
* @return Pro_education
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set path
*
* @param string $path
* @return Pro_education
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set verified
*
* @param boolean $verified
* @return Pro_education
*/
public function setVerified($verified)
{
$this->verified = $verified;
return $this;
}
/**
* Get verified
*
* @return boolean
*/
public function getVerified()
{
return $this->verified;
}
/**
* Set city
*
* @param string $city
* @return Pro_education
*/
public function setCity($city)
{
$this->city = $city;
return $this;
}
/**
* Set profile
*
* @param \My\MainBundle\Entity\Pro_profile $profile
* @return Pro_education
*/
public function setProfile(\My\MainBundle\Entity\Pro_profile $profile)
{
$this->profile = $profile;
return $this;
}
/**
* Get profile
*
* @return \My\MainBundle\Entity\Pro_profile
*/
public function getProfile()
{
return $this->profile;
}
}
Pro_profileEducationType.php
namespace My\MainBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class Pro_profileEducationType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('educations', 'collection', array('type' => new Pro_educationType(),
'allow_add' => true,
'by_reference' => false,
'allow_delete' => true,
'label' => false,
))
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\MainBundle\Entity\Pro_profile'
));
}
/**
* @return string
*/
public function getName()
{
return 'my_mainbundle_pro_profile_education';
}
}
Pro_educationType.php
namespace My\MainBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class Pro_educationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text', array(
'attr' => array ('placeholder' => 'Nazwa szkoły/uczelni'),
'label' => false,
));
$builder-> add('title', 'text', array(
'attr' => array ('placeholder' => 'Uzyskany tytuł'),
'label' => false,
));
$builder-> add('file', 'file', array(
'label' => 'Skan dyplomu',
'multiple' => true,
'data_class' => null,
)) ;
$builder-> add('city', 'text', array(
'attr' => array ('placeholder' => 'Miasto'),
'label' => false,
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'My\MainBundle\Entity\Pro_education',
));
}
public function getName()
{
return 'pro_educations';
}
}
RegistrationSpecialEducationController.php-最后可能会出现问题,因为我尝试在主Pro_概要文件实体中使用Pro_education.php(dependent)no中的upload()函数,但出现了一个错误
试图调用类的名为“upload”的未定义方法
“我的\MainBundle\Entity\Pro\u配置文件”
也许我的问题在于没有理解这个问题,但我真的试图自己找到解决办法,我被卡住了
如果你能帮我做这件事,我将不胜感激
namespace My\MainBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use My\MainBundle\Entity\Pro_profile;
use My\MainBundle\Entity\Pro_education;
use My\MainBundle\Form\Pro_profileEducationType;
use Symfony\Component\HttpFoundation\Session\Session;
use Application\Sonata\UserBundle\Entity\User;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class RegistrationSpecialEducationController extends Controller
{
/**
* Creates a new Pro_profile entity.
*
* @Route("/create_education", name="pro_profile_create_education")
* @Method("POST")
* @Template("MyMainBundle:RegistrationSpecialEducation:education.html.twig")
*/
public function createAction(Request $request)
{
$user = $this->get('security.token_storage')->getToken()->getUser()->getId();
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyMainBundle:Pro_profile')->findOneBy(array('user' => $user));
// $educations = $entity-> getEducations();
//$scan = $educations -> getScan();
//foreach ($educations as $edu) {
//
// $tabela = $edu ->getScan();
// }
$form = $this->createCreateForm($entity);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$entity->getEducations() ->upload();
$em->persist($entity);
$em->flush();
$this->redirect($this->generateUrl('RegisterSpecialEducation'));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
// 'edu' => $tabela,
);
}
/**
* Creates a form to create a Pro_profile entity.
*
* @param Pro_profile $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Pro_profile $entity)
{
$form = $this->createForm(new Pro_profileEducationType(), $entity, array(
'action' => $this->generateUrl('pro_profile_create_education'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Zakończ rejestracje'));
return $form;
}
/**
* @Route("/register/special/education", name="RegisterSpecialEducation")
* @Template()
*/
public function educationAction()
{
$user = $this->get('security.token_storage')->getToken()->getUser()->getId();
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('MyMainBundle:Pro_profile')->findOneBy(array('user' => $user));
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
}