简体   繁体   中英

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. Despite the fact the key word cascade is present. A foreign key problem with doctrine 2 annotations

/**
 * 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. Object column type is used when you want to serialize object stored on that property in database column, not for relations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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