简体   繁体   English

学说一对多注释

[英]doctrine one to many annotation

hello i make a small web site in symfony 2 When i want to remove a categorie with the associated pictures a SQLSTATE Exception is thrown. 你好,我在symfony 2中创建了一个小型网站。当我想删除带有相关图片的类别时,将引发SQLSTATE异常。 Despite the fact the key word cascade is present. 尽管存在关键词层叠。 A foreign key problem with doctrine 2 annotations 教义2注释的外键问题

/**
 * Categorie   
 *
 * @ORM\Table()
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="Amd\PictureBundle\Entity\CategorieRepository")
 */
class Categorie
 {
 /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="nom", unique=true, type="string", length=100)
 */
private $nom;

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

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

/**
 * @var \stdClass
 * @ORM\OneToMany(targetEntity="Image", mappedBy="categorie", cascade={"persist", "remove"})
 * @ORM\Column(name="listeImages", type="object", nullable=true)
 */
private $listeImages;


public function __construct() {
    $this->listeImages = new ArrayCollection();
}


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

/**
 * Set nom
 *
 * @param string $nom
 * @return Categorie
 */
public function setNom($nom)
{
    $this->nom = $nom;

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set url
 *
 * @param string $url
 * @return Categorie
 */
public function setUrl($url)
{
    $this->url = $url;

    return $this;
}

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

/**
 * Set listeImages
 *
 * @param \stdClass $listeImages
 * @return Categorie
 */
public function setListeImages($listeImages)
{
    $this->listeImages = $listeImages;

    return $this;
}
public function addImage(Image $image)
{
    $this->listeImages[] = $image;
    $image->setCategorie($this);
    return $this;
}
/**
 * Get listeImages
 *
 * @return \stdClass 
 */
public function getListeImages()
{
    return $this->listeImages;
}

/**
 * @ORM\PrePersist()
 */
public function prePersistCategorie(){
    $this->setUrl(parseNom($this->getNom()));
    mkdir(CATEGORIEPATH.$this->getNom(),0755);
}
public function parseNom($nom){
    //TODO
    return $nom;
}
/**
 * @ORM\PreRemove()
 */
public function removeCategorie(){
    $dirImagesCategorie = CATEGORIEPATH . $this->getNom();
    if(is_dir($dirImagesCategorie)){
          $handle = opendir($dirImagesCategorie);
        while ($elem = readdir($handle)) {
            //ce while vide tous les repertoire et sous rep
            if (substr($elem, -2, 2) !== '..' && substr($elem, -1, 1) !== '.') {
                unlink($dirImagesCategorie . '/' . $elem);
            }
        }
        rmdir($dirImagesCategorie);

    }  
 }


 public static function categoriePath(){
    return __DIR__."/../../../../web/categories/";
 }
}

and the picture class contains the "other side" of the association 图片类别包含关联的“另一面”

 /**
 * Image
 * @ORM\Table()
 * @ORM\HasLifecycleCallbacks
 * @ORM\Entity(repositoryClass="Amd\PictureBundle\Entity\ImageRepository")
 */
class Image {

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @Assert\File(maxSize="60000000")
 */
protected $file;
private static $uploadDir = 'categories/';

/**
 * @ORM\ManyToOne(targetEntity="Categorie", inversedBy="listeImages")
 * 
 * @var type 
 */
 private $categorie;

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

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

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

/**
 * ->getForm
 * @var string
 *
 * @ORM\Column(name="nom", type="string", length=100)
 */
private $nom;

public function setCategorie($categorie) {
    $this->categorie = $categorie;
    return $this;
}


public function getCategorie() {
    return $this->categorie;
}

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

/**
 * Set src
 *
 * @param string $src
 * @return Image
 */
public function setSrc($src) {
    $this->src = $src;
    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

/**
 * 
 * @param string $extension
 * @return \Amd\PictureBundle\Entity\Image
 */
public function setExtension($extension) {
    $this->extension = $extension;
    return $this;
}

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

public function setFile($file) {
    $this->file = $file;
    return $this;
}

public function getFile() {
    return $this->file;
}

protected function getUploadDir() {
    // On retourne le chemin relatif vers l'image pour un navigateur

    return Image::$uploadDir . $this->getCategorie()->getUrl();
}

protected function getUploadRootDir() {
    // On retourne le chemin relatif vers l'image pour notre code PHP
    return __DIR__ . '/../../../../web/' . $this->getUploadDir();
}

/**
 * @ORM\PrePersist()
 * @ORM\PreUpdate()
 */
public function preUpload() {
    if (null != $this->file) {          
       // faites ce que vous voulez pour générer un nom unique
        $array = explode('.', basename($this->getFile()));
        $this->setExtension($array[1]);
        $this->setSrc($this->getUploadDir());
        $this->setNom($array[0]);
        $this->setAlt($this->getNom());
    }
}



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

    $this->file->move($this->getUploadRootDir(), $this->getNom() . '.' . $this->getExtension());
}

what could be the source of the foreign key problem 什么是外键问题的根源

Remove @ORM\\Column(name="listeImages", type="object", nullable=true) from listeImages property on Categorie entity. Categorie实体的listeImages属性中删除@ORM\\Column(name="listeImages", type="object", nullable=true) Object column type is used when you want to serialize object stored on that property in database column, not for relations. 如果要序列化存储在数据库列中该属性上的对象,而不是用于关系,则使用对象列类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM