Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP+Symfony] mapowanie trzech tabel
Forum PHP.pl > Forum > PHP
dopelganger
hej
próbuje zrobić mapowanie dla trzech tabel:

posts / tabela przechowująca newsy itp
-----------
id
title
slug
content
dateAdd

menu / tabela z kategoriami - każdy news może być przyporządkowany do kilku kategorii
-----------
id
parentId
category_name
slug

connector / tabela łącząca rekordy z tabel: posts i menu
-------------
id
menuId
postId


Post.php
  1. /**
  2. * @ORM\OneToMany(targetEntity="Connector", mappedBy="post", cascade={"all"})
  3. */
  4. protected $connectors;
  5.  
  6. public function getConnectors() {
  7. return $this->connectors;
  8. }
  9.  
  10. public function __construct(){
  11. $this->connectors = new ArrayCollection();
  12. }


Connector.php
  1. /**
  2. * @ORM\ManyToOne(targetEntity="Post", inversedBy="connectors")
  3. * @ORM\JoinColumn(name="postId", referencedColumnName="id", onDelete="cascade")
  4. */
  5. protected $post;
  6.  
  7. public function setPost(\MyBundle\Entity\Post $post) {
  8. $this->post = $post;
  9. }
  10. /*
  11. * @return MyBundle\Entity\Post
  12. */
  13. public function getPost() {
  14. return $this->post;
  15. }


w kontrolerze mam:
  1. $connector_mapp = $em->getRepository('MyBundle:Connector')->findBy(array('menuId' => $request->get('idm'))); // idm kategorii przekazane w url


w twigu mam:
  1. {% for c in connector_mapp %}
  2. <p>Post ID: {{ c.postId }} w kategorii: {{ c.menuId }}</p>
  3. {% endfor %}
  4.  
  5. // rezultat np:
  6. Post ID: 151 w kategorii: 2
  7. Post ID: 149 w kategorii: 2
  8. Post ID: 148 w kategorii: 2


no i to mapowanie działa, np.: jak usune posta to usuwa mi również jego kategorie z tabeli connector.
ale za piernika nie wiem jak w tej pętli wyświetlić jeszcze informacje z tabeli posts questionmark.gifquestionmark.gif.
musze jakieś mapowanie jeszcze zrobić?

Prosze o pomoc.
Dzięki
Puszy
Spróbuj:

  1. {{ c.post.xyz }}


Gdzie xyz to nazwa pola, np "title". Chociaż ja osobiście iterowałbym po postach a nie po tym konektorze.
dopelganger
Cytat(Puszy @ 9.05.2017, 14:39:22 ) *
Spróbuj:

  1. {{ c.post.xyz }}


Gdzie xyz to nazwa pola, np "title". Chociaż ja osobiście iterowałbym po postach a nie po tym konektorze.


działa smile.gif dzięki

aż mi wstyd biggrin.gif
Puszy
Cytat(dopelganger @ 9.05.2017, 14:41:52 ) *
działa smile.gif dzięki

aż mi wstyd biggrin.gif

Z tego co wiem to gdy Twig wywołuje {{ entity.id }} to wyświetli Ci id a gdy pole jest protected/private to automatycznie szuka metody getId(), ale nie dam głowy uciąć. więc {{ c.post }} działa jak $c->getPost(); co z kolei zwróci Ci encję Post i dalej .title = getTitle();
maly_swd
Implementation

For convenience's sake foo.bar does the following things on the PHP layer:

check if foo is an array and bar a valid element;
if not, and if foo is an object, check that bar is a valid property;
if not, and if foo is an object, check that bar is a valid method (even if bar is the constructor - use __construct() instead);
if not, and if foo is an object, check that getBar is a valid method;
if not, and if foo is an object, check that isBar is a valid method;
if not, and if foo is an object, check that hasBar is a valid method;
if not, return a null value.
foo['bar'] on the other hand only works with PHP arrays:

check if foo is an array and bar a valid element;
if not, return a null value.
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.