Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php][doctrine] Dodawanie klucza obcego do encji? Terminal wyświetla błąd., Invalid index-name cat-id given, has to be [a-zA-Z0-9_]
starterrrrr
post 8.02.2021, 11:50:48
Post #1





Grupa: Zarejestrowani
Postów: 138
Pomógł: 0
Dołączył: 7.01.2015

Ostrzeżenie: (0%)
-----


Witam.
Przerabiam sobie pewien tutorial z youtube i dokłądnie w miejscu do którego dałem link: https://www.youtube.com/watch?v=MS4LICZ1j0s&t=251s
Napotykam na problem. Pojawia się komunikat:

Cytat
In SchemaException.php line 44:

Invalid index-name cat-id given, has to be [a-zA-Z0-9_]


Z tego co rozumie, to komunikat ten mówi, że index o nazwie cat-id jest niepoprawny, z tego co widzę znak "-" jest niedozwolony.

Dziwna sprawa, bo tylko dodałem dwie encje poprzez bin/console tak jak na filmie. Dodałem klucz obcy dla dwóch encji i pojawia się ten błąd. Robie dokłądnie to co w tutorialu.

  1.  
  2. <?php
  3. //plik Post.php
  4.  
  5. namespace App\Entity;
  6.  
  7. use App\Repository\PostRepository;
  8. use Doctrine\ORM\Mapping as ORM;
  9.  
  10. /**
  11.  * @ORM\Entity(repositoryClass=PostRepository::class)
  12.  */
  13. class Post
  14. {
  15. /**
  16.   * @ORM\Id
  17.   * @ORM\GeneratedValue
  18.   * @ORM\Column(type="integer")
  19.   */
  20. private $id;
  21.  
  22. /**
  23.   * @ORM\Column(type="string", length=255)
  24.   */
  25. private $title;
  26.  
  27. /**
  28.   * @ORM\Column(type="text")
  29.   */
  30. private $content;
  31.  
  32. //tutaj $category dodana recznie
  33. /**
  34.   * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
  35.   */
  36. private $category;
  37.  
  38. public function getId(): ?int
  39. {
  40. return $this->id;
  41. }
  42.  
  43. public function getTitle(): ?string
  44. {
  45. return $this->title;
  46. }
  47.  
  48. public function setTitle(string $title): self
  49. {
  50. $this->title = $title;
  51.  
  52. return $this;
  53. }
  54.  
  55. public function getContent(): ?string
  56. {
  57. return $this->content;
  58. }
  59.  
  60. public function setContent(string $content): self
  61. {
  62. $this->content = $content;
  63.  
  64. return $this;
  65. }
  66.  
  67. public function getCategory(): ?Category
  68. {
  69. return $this->category;
  70. }
  71.  
  72. public function setCategory(?Category $category): self
  73. {
  74. $this->category = $category;
  75.  
  76. return $this;
  77. }
  78. }




  1. <?php
  2.  
  3. //Plik Category.php
  4.  
  5. namespace App\Entity;
  6.  
  7. use App\Repository\CategoryRepository;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11.  
  12. /**
  13.  * @ORM\Entity(repositoryClass=CategoryRepository::class)
  14.  */
  15. class Category
  16. {
  17. /**
  18.   * @ORM\Id
  19.   * @ORM\GeneratedValue
  20.   * @ORM\Column(type="integer")
  21.   */
  22. private $id;
  23.  
  24. /**
  25.   * @ORM\Column(type="string", length=255)
  26.   */
  27. private $name;
  28.  
  29. //tutaj $posts dodana recznie
  30. /**
  31.   * @ORM\OneToMany(targetEntity="Post", mappedBy="category")
  32.   */
  33. private $posts;
  34.  
  35. public function __construct()
  36. {
  37. $this->posts = new ArrayCollection();
  38. }
  39.  
  40. public function getId(): ?int
  41. {
  42. return $this->id;
  43. }
  44.  
  45. public function getName(): ?string
  46. {
  47. return $this->name;
  48. }
  49.  
  50. public function setName(string $name): self
  51. {
  52. $this->name = $name;
  53.  
  54. return $this;
  55. }
  56.  
  57. /**
  58.   * @return Collection|Posts[]
  59.   */
  60. public function getPosts(): Collection
  61. {
  62. return $this->posts;
  63. }
  64.  
  65. public function addPost(Posts $post): self
  66. {
  67. if (!$this->posts->contains($post)) {
  68. $this->posts[] = $post;
  69. $post->setCategory($this);
  70. }
  71.  
  72. return $this;
  73. }
  74.  
  75. public function removePost(Posts $post): self
  76. {
  77. if ($this->posts->removeElement($post)) {
  78. // set the owning side to null (unless already changed)
  79. if ($post->getCategory() === $this) {
  80. $post->setCategory(null);
  81. }
  82. }
  83.  
  84. return $this;
  85. }
  86. }
  87.  



Co robie nie tak, czy mogę gdzies podać nazawę tego klucza? Próbowałem różne kombinacje, i juz brak pomysłów.

Ten post edytował starterrrrr 8.02.2021, 11:52:50
Go to the top of the page
+Quote Post
YourFrog
post 8.02.2021, 16:53:34
Post #2





Grupa: Zarejestrowani
Postów: 124
Pomógł: 22
Dołączył: 10.01.2014

Ostrzeżenie: (0%)
-----


Według dokumentacji żeby połączyć 2 encje w taki sposób aby baza danych zapisała sobie relację między innymi należy użyć również @JoinColumn. Jak tak pomyśle to nigdy bez tego nie próbowałem biggrin.gif Tam sobie wybierz dokładnie o którą relacje chodzi i postępuj według instrukcji. Ja tak robię od kilku lat i nigdy niema problemów w doctrine zamieszczonym z nową wersją symfony.


https://www.doctrine-project.org/projects/d...on-mapping.html
Go to the top of the page
+Quote Post
LowiczakPL
post 8.02.2021, 17:23:16
Post #3





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

Ostrzeżenie: (0%)
-----


  1. /**
  2.   * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts")
  3.   */
  4. private $category;


  1. /**
  2.   * @ORM\OneToMany(targetEntity=Post::class, mappedBy="category")
  3.   */
  4. private $posts;


--------------------
Szukam zleceń Symfony, Laravel, Back-End, Front-End, PHP, MySQL ...
Go to the top of the page
+Quote Post
starterrrrr
post 9.02.2021, 13:02:14
Post #4





Grupa: Zarejestrowani
Postów: 138
Pomógł: 0
Dołączył: 7.01.2015

Ostrzeżenie: (0%)
-----


Cytat(LowiczakPL @ 8.02.2021, 17:23:16 ) *
  1. /**
  2.   * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts")
  3.   */
  4. private $category;


  1. /**
  2.   * @ORM\OneToMany(targetEntity=Post::class, mappedBy="category")
  3.   */
  4. private $posts;


Niestety to nie pomogło. Będę dzisiaj próbował sposób kolegi powyżej.
Go to the top of the page
+Quote Post
LowiczakPL
post 12.02.2021, 18:35:13
Post #5





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

Ostrzeżenie: (0%)
-----


Masz błędy ponieważ nie masz klasy Posts tylko Post

jest
  1. public function addPost(Posts $post): self


a powinno być
  1. public function addPost(Post $post): self


--------------------
Szukam zleceń Symfony, Laravel, Back-End, Front-End, PHP, MySQL ...
Go to the top of the page
+Quote Post
starterrrrr
post 20.02.2021, 11:36:59
Post #6





Grupa: Zarejestrowani
Postów: 138
Pomógł: 0
Dołączył: 7.01.2015

Ostrzeżenie: (0%)
-----


Cytat(LowiczakPL @ 12.02.2021, 18:35:13 ) *
Masz błędy ponieważ nie masz klasy Posts tylko Post
jest
  1. public function addPost(Posts $post): self

a powinno być
  1. public function addPost(Post $post): self


Dziękuje za podpowiedzi. Element ten został wygenerowany automatycznie z tego co kojarzę. Próbowałem zmienić nazwe klasy, jednak nie pomogło. Dziwna sytuacja, skąd się wzieła tutaj klasa Posts smile.gif uzywałem chyba opcji regenerate i metody add i get wskoczyły automatycznie.

---------------------- EDIT --------------------------

Cytat(YourFrog @ 8.02.2021, 16:53:34 ) *
Według dokumentacji żeby połączyć 2 encje w taki sposób aby baza danych zapisała sobie relację między innymi należy użyć również @JoinColumn. Jak tak pomyśle to nigdy bez tego nie próbowałem biggrin.gif Tam sobie wybierz dokładnie o którą relacje chodzi i postępuj według instrukcji. Ja tak robię od kilku lat i nigdy niema problemów w doctrine zamieszczonym z nową wersją symfony.


https://www.doctrine-project.org/projects/d...on-mapping.html


w pliku Cost.php
  1. /**
  2.  * Many posts have one categories. This is the owning side
  3.  * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
  4.  * @ORM\JoinColumn(name="cat_id", referencedColumnName="id")
  5.  */
  6. private $category;



w pliku Category.php

  1. /**
  2.  * @ORM\OneToMany(targetEntity="Post", mappedBy="category")
  3.  */
  4. private $posts;



Przy poleceniu "php bin/console d:s:u --force" pokazuje się dalej komunikat:
Cytat
Invalid index-name cat-id given, has to be [a-zA-Z0-9_]


Gdzie można podać nazwę tego indexu? Próbuje różne kombinacje z doctrine, które znalałem ale nie dają efektu.

Ten post edytował starterrrrr 20.02.2021, 11:38:02
Go to the top of the page
+Quote Post
LowiczakPL
post 21.02.2021, 19:58:45
Post #7





Grupa: Zarejestrowani
Postów: 531
Pomógł: 55
Dołączył: 3.01.2016
Skąd: Łowicz

Ostrzeżenie: (0%)
-----


Dlaczego upierasz się przy czymś czego nie powinno być.

To jest gotowiec i musi działać.

  1. /**
  2.   * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts")
  3.   */
  4. private $category;


Zakładam że w tym kodzie nie potrzebujesz indexów ale gdybyś jednak potrzebował kiedyś założyć index w projekcie Legacy na jakąś kolumnę to możesz to zrobić w ten sposób

  1. /**
  2. * @ORM\Table(name="twoja_nazwa_tabeli", indexes={@ORM\Index(name="code_index_idx", columns={"code"})})
  3. */
  4. class TwojaNazwaTabeli
  5. {


Ten post edytował LowiczakPL 21.02.2021, 20:06:08


--------------------
Szukam zleceń Symfony, Laravel, Back-End, Front-End, PHP, MySQL ...
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 25.04.2024 - 07:35