Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Symfony 2中具有一对多关系的条令实体更新特定的图像相关数据_Php_Symfony_Doctrine Orm_Image Upload - Fatal编程技术网

Php 如何使用Symfony 2中具有一对多关系的条令实体更新特定的图像相关数据

Php 如何使用Symfony 2中具有一对多关系的条令实体更新特定的图像相关数据,php,symfony,doctrine-orm,image-upload,Php,Symfony,Doctrine Orm,Image Upload,我有两个实体(即书籍、图像),它们之间有一对一的关系,即一本书可以有多个图像。 现在,当我试图编辑这本书的细节时,我如何编辑这本书的任何特定图像 更清楚的是,如果我说,我只想改变一个特定的图像从所有以前上传的图像。有人能帮我吗 下面是我的实体和表格 /** * @ORM\Table(name="books") * @ORM\HasLifecycleCallbacks * @ORM\Entity(repositoryClass="Library\MainBundle\Entity\BookRepo

我有两个实体(即书籍、图像),它们之间有一对一的关系,即一本书可以有多个图像。 现在,当我试图编辑这本书的细节时,我如何编辑这本书的任何特定图像

更清楚的是,如果我说,我只想改变一个特定的图像从所有以前上传的图像。有人能帮我吗

下面是我的实体和表格

/**
* @ORM\Table(name="books")
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="Library\MainBundle\Entity\BookRepository")
*/
class Book
{
/**
 * @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=255)
 */
private $name;

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

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

/**
 * @var string
 *
 * @ORM\Column(name="Description", type="text")
 */
private $description;

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="books")
 * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
 **/
private $category;

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="bookpublishers")
 * @ORM\JoinColumn(name="publisher_id", referencedColumnName="id")
 **/
private $publisher;

/**
 * @ORM\ManyToMany(targetEntity="User", inversedBy="bookauthors")
 * @ORM\JoinTable(name="book_author")
 **/
private $author;

/**
 * @ORM\OneToMany(targetEntity="BookReview", mappedBy="bookreview", cascade={"all"})
 **/
private $reviews;

/**
 * @var string $image
 * @Assert\File( maxSize = "1024k", mimeTypesMessage = "Please upload a valid Image")
 * @ORM\Column(name="image", type="string", length=255)
 */
private $image;

/**
* @ORM\OneToMany(targetEntity="Image", mappedBy="book", cascade={"all"})
**/
private $pictures;

/**
 * @var string
 */
private $imageName;

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

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

    return $this;
}

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

/**
 * Set subject
 *
 * @param string $subject
 * @return Book
 */
public function setSubject($subject)
{
    $this->subject = $subject;

    return $this;
}

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

/**
 * Set isbn
 *
 * @param string $isbn
 * @return Book
 */
public function setIsbn($isbn)
{
    $this->isbn = $isbn;

    return $this;
}

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

/**
 * Set description
 *
 * @param string $description
 * @return Book
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

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

/**
 * Set category
 *
 * @param \Library\MainBundle\Entity\Category $category
 * @return Book
 */
public function setCategory(Category $category = null)
{
    $this->category = $category;

    return $this;
}

/**
 * Get category
 *
 * @return \Library\MainBundle\Entity\Category 
 */
public function getCategory()
{
    return $this->category;
}

/**
 * Set publisher
 *
 * @param \Library\MainBundle\Entity\User $publisher
 * @return Book
 */
public function setPublisher(User $publisher = null)
{
    $this->publisher = $publisher;

    return $this;
}

/**
 * Get publisher
 *
 * @return \Library\MainBundle\Entity\User 
 */
    public function getPublisher()
{
    return $this->publisher;
}

/**
 * Add author
 *
 * @param \Library\MainBundle\Entity\User $author
 * @return Book
 */
public function addAuthor(User $author)
{
    $this->author[] = $author;

    return $this;
}

/**
 * Remove author
 *
 * @param \Library\MainBundle\Entity\User $author
 */
public function removeAuthor(User $author)
{
    $this->author->removeElement($author);
}

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

/**
 * Set author
 *
 * @return User
 */
public function setAuthor(User $author)
{
    $this->author[] = $author;
}


/**
 * Add reviews
 *
 * @param \Library\MainBundle\Entity\BookReview $reviews
 * @return Book
 */
public function addReview(BookReview $reviews)
{
    $this->reviews[] = $reviews;

    return $this;
}

/**
 * Remove reviews
 *
 * @param \Library\MainBundle\Entity\BookReview $reviews
 */
public function removeReview(BookReview $reviews)
{
    $this->reviews->removeElement($reviews);
}

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

public function getFullImagePath() {
    return null === $this->image ? null : $this->getUploadRootDir(). $this->image;
}

protected function getUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return $this->getTmpUploadRootDir().$this->getId()."/";
}

protected function getTmpUploadRootDir() {
    // the absolute directory path where uploaded documents should be saved
    return __DIR__ . '/../../../../web/upload/books/';
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function uploadImage() {
    // the file property can be empty if the field is not required
    if (null === $this->image) {
        return;
    }
    $tempImageName = time() . '_' .$this->image->getClientOriginalName();
    if(!$this->id){
        $this->setImageName($this->image->getClientOriginalName());
        $this->image->move($this->getTmpUploadRootDir(), $this->image->getClientOriginalName());
    }else{
        $this->image->move($this->getUploadRootDir(), $tempImageName);
        unlink($this->getUploadRootDir().$this->getImageName());
    }

    $this->setImage($tempImageName);

}

 /**
 * @ORM\PostPersist()
 */
public function moveImage()
{
    if (null === $this->image) {
        return;
    }
    if(!is_dir($this->getUploadRootDir())){
        mkdir($this->getUploadRootDir());
    }
    copy($this->getTmpUploadRootDir().$this->getImageName(), $this->getFullImagePath());
    unlink($this->getTmpUploadRootDir().$this->getImageName());
}

/**
 * @ORM\PreRemove()
 */
public function removeImage()
{
    unlink($this->getFullImagePath());
    rmdir($this->getUploadRootDir());
}


/**
 * Set image
 *
 * @param string $image
 * @return Book
 */
public function setImage($image)
{
    $this->image = $image;

    return $this;
}

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

/**
 * Set image
 *
 * @param string $image
 * @return Book
 */
public function setImageName($image)
{
    $this->imageName = $image;

    return $this;
}

/**
 * Get image
 *
 * @return string 
 */
public function getImageName()
{
    return $this->imageName;
}

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

/**
 * Add pictures
 *
 * @param \Library\MainBundle\Entity\Image $pictures
 * @return Book
 */
public function addPicture(Image $pictures)
{
    $this->pictures[] = $pictures;
    $pictures->setBook($this);
    return $this;
}

/**
 * Remove pictures
 *
 * @param \Library\MainBundle\Entity\Image $pictures
 */
public function removePicture(Image $pictures)
{
    $this->pictures->removeElement($pictures);
}

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

}
图像实体(Image.php)

我的表格: BookType.php

namespace Library\MainBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Library\MainBundle\Entity\Role;
use Library\MainBundle\Entity\Image;

class BookType extends AbstractType
{

/**
 * @var \Library\MainBundle\Entity\Role
 */
protected $publisherrole;

/**
 * @var \Library\MainBundle\Entity\Role
 */
protected $authorrole;

public function __construct (Role $publisherRole, Role $authorRole)
{
    $this->publisherrole = $publisherRole;
    $this->authorrole = $authorRole;
}

/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text', array('label' => 'book.label.name',
                                    'attr' => array('class' => 'element text medium')))
        ->add('subject', 'text', array('label' => 'book.label.subject',
                                        'attr' => array('class' => 'element text medium')))
        ->add('isbn', 'text', array('label'=> 'book.label.isbn',
                                    'attr' => array('class' => 'element text medium')))
        ->add('description', 'textarea', array('label' => 'book.label.description',
                                                'attr' => array('class' => 'element textarea medium')))
        ->add('category', 'entity', array(
                'class' => 'LibraryMainBundle:Category',
                'property' => 'name',
                "label" => 'book.label.category',
                "attr" => array('class' => 'element select medium')))
        ->add('publisher', 'entity', array(
                'class' => 'LibraryMainBundle:User',
                'property' => 'name',
                'label' => 'book.label.publisher',
                'choices' => $this->publisherrole->getUser(),
                'attr' => array('class' => 'element select medium')
            ))
        ->add('author', 'entity', array(
                'class' => 'LibraryMainBundle:User',
                'property' => 'name',
                'label' => 'book.label.author',
                'choices' => $this->authorrole->getUser(),
                'multiple' => 'true',
                'attr' => array('class' => 'element select medium')
            ))
        ->add('image', 'file', array('label' => 'book.label.coverpic',
                                    'attr' => array('class' => 'element textarea medium'),
                                    'data_class' => null
            ))
        ->add("pictures", 'collection', array(
        'type'=>new FileType(),
        'allow_add'=>true,
                'by_reference' => true,
        'data'=>array(new Image(),
                                new Image()
                    )
    ))
        ->add("save", "submit", array('label' => 'book.button.save'));
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Library\MainBundle\Entity\Book'
    ));
}

/**
 * @return string
 */
public function getName()
{
    return 'library_mainbundle_book';
}
}
FileType.php

namespace Library\MainBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class FileType extends AbstractType
{

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    return $builder->add("name", "file");
}

public function getName()
{
    return "filetype";
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class'=>'Library\MainBundle\Entity\Image',
        'csrf_protection'=>true,
        'csrf_field_name'=>'_token',
        'intention'=>'file'
    ));
}
}

请让我知道我是否需要更多的信息。

只是为了澄清:您只是想删除以前设置的图像,还是?是的,我想从文件夹中删除这些图像,并用新的图像名称更新图像实体的现有DB表行。目前什么不起作用?为什么您有
图像
图片
?@Hpatoio,当我试图编辑书籍详细信息并上载图片的图像字段时,它会在图像实体中添加新行,而不是更新现有行。图像用于存储将存储在书本实体中的一个图像,而图片是书本和图像实体之间的一对多关系。i、 e.图片字段中上传的任何图像都是针对图像实体的(即book_images表,请参阅顶部图像实体中的ORM注释以获取更多参考。)
namespace Library\MainBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class FileType extends AbstractType
{

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    return $builder->add("name", "file");
}

public function getName()
{
    return "filetype";
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class'=>'Library\MainBundle\Entity\Image',
        'csrf_protection'=>true,
        'csrf_field_name'=>'_token',
        'intention'=>'file'
    ));
}
}