1

我是stackoverflow的新手,希望你能帮助我。

我在 symfony2 上使用 OneUploaderBundle 并且它正在工作我可以上传多个图像,并且可以将它们保存在我的数据库中。

但现在我想为一个“Annonce”上传多张图片,例如,我有 2 个实体

安诺斯

namespace TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Annonce
 *
 * @ORM\Table(name="annonce")
 * @ORM\Entity(repositoryClass="TestBundle\Repository\AnnonceRepository")
 */
class Annonce
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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


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

    /**
     * Set titre
     *
     * @param string $titre
     * @return Annonce
     */
    public function setTitre($titre)
    {
        $this->titre = $titre;

        return $this;
    }

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

还有我的实体形象

namespace TestBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Image
 *
 * @ORM\Table(name="image")
 * @ORM\Entity(repositoryClass="TestBundle\Repository\ImageRepository")
 */
class Image
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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


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

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

        return $this;
    }

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


    /**
     * @ORM\ManyToOne(targetEntity="TestBundle\Entity\Annonce")
     * @ORM\JoinColumn(nullable=false)
     */
    private $annonce;

    /**
     * Set annonce
     *
     * @param \TestBundle\Entity\Annonce $annonce
     * @return Image
     */
    public function setAnnonce(\TestBundle\Entity\Annonce $annonce)
    {
        $this->annonce = $annonce;

        return $this;
    }

    /**
     * Get annonce
     *
     * @return \TestBundle\Entity\Annonce 
     */
    public function getAnnonce()
    {
        return $this->annonce;
    }
}

我的事件监听器

namespace TestBundle\EventListener;

use Doctrine\Common\Persistence\ObjectManager;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use TestBundle\Entity\Annonce;
use TestBundle\Entity\Image;

class UploadListener
{
    /**
     * @var ObjectManager
     */
    private $em;

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

    public function onUpload(PostPersistEvent $event)
    {
        $file = $event->getFile();

        $annonce = new Annonce();
        $image = new Image();
        /* TEST */
        $annonce->setTitre("I'am a title");
        $this->em->persist($annonce);

        $image->setName($file->getFilename());
        $image->setAnnonce($annonce);


        $this->em->persist($image);
        $this->em->flush();


        $response = $event->getResponse();
        $response['files'] = [
            'name' => $file->getFilename(),
            'size' => $file->getSize()
        ];
    }

问题是,当我上传许多图像时,Annonce 中的行数与 Image 中的行数相同。

有人可以帮助我:)

4

0 回答 0