Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF2][SF] Modyfikacja CRUD
Forum PHP.pl > Forum > PHP > Frameworki
basso
Witam.
Wygenerowałem sobie Cruda. I teraz chcę zrobić jedną rzecz.
Mam o to poniższy screen dodawania elementu. I problem w tym taki, że chcę wyrzuć stąd te nieszczęsne pole daty.
Zatem w

  1. public function buildForm(FormBuilderInterface $builder, array $options)
  2. {
  3. $builder
  4. ->add('title')
  5. ->add('lead')
  6. ->add('description')
  7. ->add('createdAt')
  8. ;
  9. }


usunąłem ->add('createdAt'). I jest wszystko okej, bo znikło z formularza. Ale gdy zapisuje to dostaje mi tam do bazy null i wyskakuje błąd. No bo null nie może być.
Próbowałem zatem w jakiejś metodzie z Entity np setDescription wrzucić $this->setCreated aby mi tam dodało datę, tam dałem date("Y-m-d",time()).
No ale błędy, bo wiecznie format coś tam... ;/
Pytanie:
1. Czy muszę całość zapisania zrobić od nowa w EntityRepository nadpisując tą wygenerowaną metodę?
2. Czy można jakoś to krócej zrobic?

peter13135
Cytat
Próbowałem zatem w jakiejś metodzie z Entity np setDescription wrzucić $this->setCreated aby mi tam dodało datę, tam dałem date("Y-m-d",time()).
No ale błędy, bo wiecznie format coś tam... ;/

Dobrze kombinujesz. Tylko ten kod wrzuć do konstruktora, a nie do setDescription()
toffiak
"created" prawdopodobnie musi być jako obiekt klasy DateTime czyli tak jak wyżej podano do konstruktora dodajesz:
  1. public function __construct(){
  2. $this->created = new \DateTime();
  3. }
mortus
W SF2 służą do tego lifcycleCallbacks (w tym przypadku prePersist)
  1. /**
  2.  * @ORM\Entity
  3.  * @ORM\HasLifcycleCallbacks
  4.  */
  5. class Post {
  6.  
  7. /**
  8.   * @ORM\Id
  9.   * @ORM\Column(type="integer")
  10.   * @ORM\GeneratedValue
  11.   */
  12. protected $id;
  13.  
  14. /**
  15.   * @ORM\Column(type="datetime")
  16.   */
  17. protected $createdAt;
  18.  
  19. /**
  20.   * @ORM\PrePersist
  21.   */
  22. public function setCreatedAtValue() {
  23. if(!$this->createdAt) {
  24. $this->createdAt = new \DateTime();
  25. }
  26. }
  27.  
  28. }
basso
Hej,
Kurcze próbuję tego i tego i LIPA. Faktycznie nie pomyślałem o konstruktorze.
Ale Wy to wrzucacie w EntityRepository czy do Entity?

  1. An exception occurred while executing 'INSERT INTO pages (title, lead, description, created_at) VALUES (?, ?, ?, ?)' with params {"1":"d","2":"fdsaf","3":"sdf","4":null}:
  2.  
  3. SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'created_at' cannot be null
  4. 500 Internal Server Error - DBALException
  5. 1 linked Exception: PDOException ť


A co to za tworzenie obiektu new \DateTime()? Z backsleshem, tak to trzeba zrobić? W sumie pierwszy raz z takim czymś się spotykam, to od razu pytam.
Dzięki za pomoc tak w ogóle smile.gif

A jeszcze jedno, czy muszę na nowo wygenerować Entity i bazę z tym : @ORM\HasLifecycleCallbacks() czy mogę dopisać?
mortus
Metody dla lifecycleCallbacks definiujemy w klasie encji, nie w repozytorium. Pokaż kod encji.

PS: Klasa DateTime zdefiniowana jest w domyślnej/standardowej przestrzeni nazw stąd \.
basso
Całe Entity wygląda tak

  1. <?php
  2.  
  3. // src/Backend/PagesBundle/Entity/Pages.php
  4. namespace Backend\PagesBundle\Entity;
  5.  
  6. use Doctrine\ORM\Mapping as ORM;
  7.  
  8. /**
  9.  * Backend\PagesBundle\Entity\Pages
  10.  *
  11.  * @ORM\Table(name="pages")
  12.  * @ORM\Entity
  13.  */
  14. class Pages
  15. {
  16. /**
  17.   * @var bigint $id
  18.   *
  19.   * @ORM\Column(name="id", type="bigint", nullable=false)
  20.   * @ORM\Id
  21.   * @ORM\GeneratedValue(strategy="IDENTITY")
  22.   */
  23. private $id;
  24.  
  25.  
  26. /**
  27.   * @var string $title
  28.   *
  29.   * @ORM\Column(name="title", type="string", length=255, nullable=false)
  30.   */
  31. private $title;
  32.  
  33. /**
  34.   * @var text $lead
  35.   *
  36.   * @ORM\Column(name="lead", type="text", nullable=false)
  37.   */
  38. private $lead;
  39.  
  40.  
  41. /**
  42.   * @var text $description
  43.   *
  44.   * @ORM\Column(name="description", type="text", nullable=false)
  45.   */
  46. private $description;
  47.  
  48. /**
  49.   * @var datetime $createdAt
  50.   *
  51.   * @ORM\Column(name="created_at", type="datetime", nullable=false)
  52.   */
  53. private $createdAt;
  54.  
  55.  
  56.  
  57.  
  58. /**
  59.   * Get id
  60.   *
  61.   * @return integer
  62.   */
  63. public function getId()
  64. {
  65. return $this->id;
  66. }
  67.  
  68. /**
  69.   * Set title
  70.   *
  71.   * @param string $title
  72.   * @return Pages
  73.   */
  74. public function setTitle($title)
  75. {
  76. $this->title = $title;
  77.  
  78. return $this;
  79. }
  80.  
  81. /**
  82.   * Get title
  83.   *
  84.   * @return string
  85.   */
  86. public function getTitle()
  87. {
  88. return $this->title;
  89. }
  90.  
  91. /**
  92.   * Set lead
  93.   *
  94.   * @param string $lead
  95.   * @return Pages
  96.   */
  97. public function setLead($lead)
  98. {
  99. $this->lead = $lead;
  100. return $this;
  101. }
  102.  
  103. /**
  104.   * Get lead
  105.   *
  106.   * @return string
  107.   */
  108. public function getLead()
  109. {
  110. return $this->lead;
  111. }
  112.  
  113. /**
  114.   * Set createdAt
  115.   *
  116.   * @param \DateTime $createdAt
  117.   * @return Pages
  118.   */
  119. public function setCreatedAt($createdAt)
  120. {
  121. $this->createdAt = $createdAt;
  122. return $this;
  123. }
  124.  
  125. /**
  126.   * Get createdAt
  127.   *
  128.   * @return \DateTime
  129.   */
  130. public function getCreatedAt()
  131. {
  132. return $this->createdAt;
  133. }
  134.  
  135. /**
  136.   * Set description
  137.   *
  138.   * @param string $description
  139.   * @return Pages
  140.   */
  141. public function setDescription($description)
  142. {
  143. $this->description = $description;
  144.  
  145.  
  146.  
  147. return $this;
  148. }
  149.  
  150. /**
  151.   * Get description
  152.   *
  153.   * @return string
  154.   */
  155. public function getDescription()
  156. {
  157. return $this->description;
  158. }
  159. }
mortus
Przed deklaracją klasy brakuje adnotacji @ORM\HasLifecycleCallbacks a w kodzie klasy dodaj metodę setCreatedAtValue, którą wcześniej napisałem (nie zapomnij o adnotacji @ORM\PrePersist).
basso
NO no ja CI dałem moja oryginalną bez zmian Entity.
Spoko wkleiłem to na górze, ale w metodzie ucinałem Value i zostawialem tylko jedną setCreatedAt smile.gif

Zadziałało, bardzo dziękuje Ci i Wam za pomoc.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.