Programing

cascade = {“remove”} VS orphanRemoval = true VS ondelete = "CASCADE

lottogame 2020. 9. 12. 11:20
반응형

cascade = {“remove”} VS orphanRemoval = true VS ondelete = "CASCADE


부모 엔티티가 삭제되면 자동으로 자식 엔티티를 삭제하는 방법에 대한 정보를 거의 수집하지 않았습니다. 가장 일반적인 방법은 cascade = { "remove"} OR orphanRemoval = true OR ondelete = "CASCADE" 세 가지 주석 중 하나를 사용하는 것 같습니다 .

나는 : 세 번째에 대한 약간의 혼동 ondelete = "CASCADE" 이 일이 매우 부족한 대해 교리 공식 문서에서 설명으로) 누군가 할 수 있다면 나는 사랑을 나에게 다음과 같은 정보 확인 I 수집을하고 내 연구에서 이해 그물과 경험 ...

그것이하는 일

cascade = { "remove"}
==> 소유 측 엔티티가있을 때 반대 측 엔티티가 삭제됩니다. 당신이 다른 소유 측 엔티티와 많은 토 머니에 있더라도.
-컬렉션에 사용되어야 함 (OneToMany 또는 ManyToMany 관계에서)-ORM
에서 구현

orphanRemoval = true
==> 소유 측 엔티티가이고 다른 소유 측 엔티티에 더 이상 연결되지 않으면 반대 측의 엔티티가 삭제됩니다. (참조 doctrine official_doc -ORM에서 구현
-OneToOne, OnetoMany 또는 ManyToMany와 함께 사용할 수 있음)

onDelete = "CASCADE"
==> 이것은 데이터베이스의 외래 키 열에 On Delete Cascade를 추가합니다
.-이 전략은 약간 까다 롭지 만 매우 강력하고 빠를 수 있습니다. (참조 doctrine official_doc ...하지만 더 많은 설명을 읽지 않음)
-ORM은 이전의 두 가지 방법에 비해 작업이 적으므로 성능이 향상되어야합니다.

기타 정보
-이 세 가지 방법은 모두 양방향 관계 엔티티에서 구현됩니다 ( right ??? )
-cascade = { "remove"}를 사용하면 모든 외래 키 onDelete = CASCADE를 완전히 우회합니다. (참조 doctrine_official_doc )

코드에서 사용하는 방법에 대한 예

  • orphanRemoval 및 cascade = { "remove"}는 반전 된 엔티티 클래스에 정의됩니다.
  • ondelete = "CASCADE"는 소유자 엔티티에 정의됩니다.
  • @ORM \ JoinColumn (onDelete = "CASCADE")를 작성하고 doctrine이 열 이름을 처리하도록 할 수도 있습니다.

cascade = { "제거"}

/**
* @OneToMany(targetEntity="Phonenumber", mappedBy="contact", cascade={"remove"})
*/
protected $Phonenumbers

orphanRemoval = true

/**
* @OneToMany(targetEntity="Phonenumber", mappedBy="contact", orphanRemoval=true)
*/
protected $Phonenumbers

onDelete = "CASCADE"

/** 
* @ManyToOne(targetEntity="Contact", inversedBy="phonenumbers")
* @JoinColumn(name="contact_id", referencedColumnName="contact_id", onDelete="CASCADE")
*/ 
protected $contact; 

onDelete="CASCADE"데이터베이스 자체에서 관리합니다. cascade={"remove"}교리에 의해 관리됩니다.

onDelete="CASCADE"작업이 교리에 의해 대신 데이터베이스 수준에서 수행되기 때문에 더 빠릅니다. 제거는 Doctrine이 아닌 데이터베이스 서버에 의해 수행됩니다. With cascade={"remove"}doctrine은 엔티티 자체를 관리해야하며 다른 소유 엔티티가 없는지 확인하기 위해 추가 검사를 수행합니다. 다른 것이 없으면 엔티티를 삭제합니다. 그러나 이것은 오버 헤드를 발생시킵니다.


cascade = { "제거"}

  • the entity on the inverse side is deleted when the owning side entity is. Even if you are in a manytomany with other owning side entity. No, if the entity is owned by something else. It won't be deleted.
  • should be used on collection (so in OneToMany or ManyToMany relationship)
  • implementation in the ORM

orphanRemoval="true"

  • the entity on the inverse side is deleted when the owning side entity is AND it is not connected to any other owning side entity anymore. Not exactly, this makes doctrine behave like it is not owned by an other entity, and thus remove it.
  • implementation in the ORM
  • can be used with OneToOne, OnetoMany or ManyToMany

onDelete="CASCADE"

  • this will add On Delete Cascade to the foreign key column IN THE DATABASE
  • This strategy is a bit tricky to get right but can be very powerful and fast. (this is a quote from doctrine official tutorial... but haven't seen much more explaination)
  • ORM has to do less work (compared to the two previous way of doing) and therefore should have better performance.

참고URL : https://stackoverflow.com/questions/27472538/cascade-remove-vs-orphanremoval-true-vs-ondelete-cascade

반응형