Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony][Symfony2]Prosty problem z relacjami
szypi1989
post 25.08.2018, 20:04:43
Post #1





Grupa: Zarejestrowani
Postów: 207
Pomógł: 0
Dołączył: 7.09.2010

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


Witam. Otóż mam prosty problem jak dla ludzi , którzy robili relacje baz danych.
Mam Entity User ( przez pakiet Fos User):
  1. /**
  2.  * @ORM\Entity
  3.  * @ORM\Table(name="fos_user")
  4.  */
  5. class User extends BaseUser
  6. {
  7.  
  8. /**
  9.   * @ORM\Id
  10.   * @ORM\Column(type="integer")
  11.   * @ORM\GeneratedValue(strategy="AUTO")
  12.   * @ORM\OneToOne(targetEntity="Userinfo", inversedBy="User")
  13.   */
  14. public $id;
  15.  
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. }
  20.  
  21. }


I Drugi Entity dla którego będzie relacją:
  1.  
  2. namespace AppBundle\Entity;
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7.  
  8. /**
  9.  * Userinfo
  10.  *
  11.  * @ORM\Table(name="user_info")
  12.  * @ORM\Entity(repositoryClass="AppBundle\Repository\UserinfoRepository")
  13.  * @UniqueEntity("id_user")
  14.  */
  15. class Userinfo {
  16.  
  17. /**
  18.   * @var int
  19.   *
  20.   * @ORM\Column(name="id", type="integer")
  21.   * @ORM\Id
  22.   * @ORM\GeneratedValue(strategy="AUTO")
  23.   */
  24. private $id;
  25.  
  26. /**
  27.   * @ORM\OneToOne(targetEntity="User", inversedBy="Userinfo")
  28.   * @ORM\JoinColumn(name="id", referencedColumnName="id")
  29.   */
  30. public $user;
  31.  
  32. /**
  33.   * @var string
  34.   *
  35.   * @ORM\Column(name="address", type="string", length=255)
  36.   */
  37. private $address;
  38.  
  39. /**
  40.   * @var string
  41.   *
  42.   * @ORM\Column(name="phone_number", type="integer")
  43.   */
  44. private $phone_number;
  45.  
  46. /**
  47.   * @var int
  48.   *
  49.   * @ORM\Column(name="id_user", type="integer")
  50.   */
  51. private $id_user;
  52.  
  53. /**
  54.   * Get id
  55.   *
  56.   * @return integer
  57.   */
  58. public function getId() {
  59. return $this->id;
  60. }
  61.  
  62. /**
  63.   * Set id_user
  64.   *
  65.   * @param integer $id_user
  66.   * @return Userinfo
  67.   */
  68. public function setId_user($id_user) {
  69. $this->id_user = $id_user;
  70.  
  71. return $this;
  72. }
  73.  
  74. /**
  75.   * Get id_user
  76.   *
  77.   * @return integer
  78.   */
  79. public function getId_user() {
  80. return $this->id_user;
  81. }
  82.  
  83. /**
  84.   * Get user
  85.   *
  86.   * @return User
  87.   */
  88. public function getUser() {
  89. return $this->user;
  90. }
  91. }


I próbuje wyciągnąć dane w kontrolerze :
  1. $userinfo = $this->getDoctrine()
  2. ->getRepository(Userinfo::class)
  3. ->find(2);
  4. $id = $userinfo->getUser()->getId();


Niestety nie idzie. Zaznaczam, że tabela Userinfo zawiera kolumnę id_user , która ma być relacją do tabeli User dla klucza id

Ten post edytował szypi1989 25.08.2018, 20:10:44
Go to the top of the page
+Quote Post
ohm
post 25.08.2018, 20:06:37
Post #2





Grupa: Zarejestrowani
Postów: 618
Pomógł: 143
Dołączył: 22.12.2010

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


W @OneToOne masz targetEntity="UsersInfo", powinno byc UserInfo
Go to the top of the page
+Quote Post
szypi1989
post 26.08.2018, 09:21:32
Post #3





Grupa: Zarejestrowani
Postów: 207
Pomógł: 0
Dołączył: 7.09.2010

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


  1. var_dump($id);


Pokazuje mi 2 . A Tabela userinfo powiązana z tym kluczem ma id 4

userinfo
-----------------
id|id_user
2|4

user
----------
id|
4

userinfo.id_user=user.id

Wynik powinien być 4

Problem rozwiązany. JoinColumn(name="id_user") . Ale ze mnie sierota.Zamiast id_user wpisałem id

Ten post edytował szypi1989 25.08.2018, 22:29:29
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: 29.03.2024 - 00:16