学说一对多注释

时间:2014-05-24 16:44:31

标签: symfony orm doctrine-orm cascading-deletes

您好,我在symfony 2中创建了一个小型网站 当我想删除带有相关图片的类别时,会抛出SQLSTATE异常。 尽管事实上存在关键词级联。 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/";
 }
}

并且图片类包含关联的“另一面”

 /**
 * 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());
}

可能是外键问题的根源

1 个答案:

答案 0 :(得分:0)

@ORM\Column(name="listeImages", type="object", nullable=true)实体上的listeImages属性中移除Categorie。 如果要序列化存储在数据库列中该属性上的对象,而不是关系,则使用对象列类型。