I just edit the topic details.
I am currently in the process of developing a Forum, which consists of 4 entity:
- Category which includes several forums
- Forum which brings together several topics
- Topic comprising several Posts
- Post which contains answers to topics
Everything works fine for now but one point at a query that I can not achieve.
I would like on the home page of the forum all the categories with sub forums to allow rapid navigation.
The problem I have is that I can not retrieve the post dernirer performed for each forum, and I would like to know how I do for solve this problem ?
So I would like to display all categories associated with all these forums and post the last post in the entity so LastPost topic of each forum.
Here's an overview of what I would do : http://hpics.li/029d17b
My repository :
public function findAllCategory()
{
$qb = $this->createQueryBuilder('c')
->leftJoin('c.forum', 'f')
->addSelect('f')
->leftJoin('f.topic', 't')
->addSelect('t')
->orderBy('c.id', 'DESC');
return $qb->getQuery()
->getResult();
}
My controler :
$categorys = $em->getRepository('AppBundle:Category')->findAllCategory();
Here is my twig file :
{% for category in categorys %}
<div>
<div class="row">
<div class="col-md-12"><h2>{{ category.title }}</h2></div>
</div>
<div class="row">
{% for forum in category.forum %}
<div class="col-md-4">
<h3>{{ forum.title }}</h3>
{{ forum.description }}
Last Post :<br/>
{% if forum.lastPost is not empty %}
{{ forum.topic.title }}
{% endif %}
</div>
{% endfor %}
</div>
</div>
{% endfor %}
My entities :
class Category
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Forum", mappedBy="category")
* #ORM\JoinColumn(nullable=true)
*/
private $forum;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #Gedmo\Slug(fields={"title", "id"})
* #ORM\Column(length=128, unique=true)
*/
private $slug;
}
class Forum
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Category", inversedBy="forum")
* #ORM\JoinColumn(nullable=false)
*/
private $category;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Topic", mappedBy="forum")
* #ORM\JoinColumn(nullable=true)
*/
private $topic;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #Gedmo\Slug(fields={"title", "id"})
* #ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* #var string
*
* #ORM\Column(name="description", type="string", length=255)
*/
private $description;
/**
* #ORM\OneToOne(targetEntity="AppBundle\Entity\Post")
* #ORM\JoinColumn(nullable=true)
*/
private $lastPost;
}
class Topic
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="topic")
* #ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Forum", inversedBy="topic")
* #ORM\JoinColumn(nullable=false)
*/
private $forum;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Post", mappedBy="topic")
* #ORM\JoinColumn(nullable=true)
*/
private $post;
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #Gedmo\Slug(fields={"title", "id"})
* #ORM\Column(length=128, unique=true)
*/
private $slug;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* #ORM\OneToOne(targetEntity="AppBundle\Entity\Post")
* #ORM\JoinColumn(nullable=true)
*/
private $lastPost;
}
class Post
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="UserBundle\Entity\User", inversedBy="post")
* #ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Topic", inversedBy="post")
* #ORM\JoinColumn(nullable=false)
*/
private $topic;
/**
* #var string
*
* #ORM\Column(name="message", type="text")
*/
private $message;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
}
Thanks !
Related
I have this entity
class Category
{
/**
* #var int
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Product",mappedBy="category")
*/
private $products;
/**
* #var int
* #ORM\Column(name="orderNr",type="integer")
*/
private $order;
/**
* #ORM\OneToMany(targetEntity="Category", mappedBy="parent")
*/
private $children;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Category",inversedBy="children")
* #ORM\JoinColumn(name="parent_id", referencedColumnName="id")
*/
private $parent;
I want to make a query to get all categories and for each category, the childrens.
I tried many versions of queryes from other questions, but nothing seems to work.
Can anyone help me,please?
In the end i need something like this
[Category Parent 1]
[Childrens]
[0]
//details
[1]
//details
I'm facing a big problem with Doctrine when i'm requesting a table with a composite primary key referencing foreign keys.
I have a 3 entities:
Library(idLibrary,adress), Book(idBook,title,pageCount), Container(idLibrary,idBook, quantity).
Everything is generated without any errors but when i'm doing findAll() request on my Container repository my browser freezes and nothing is showed like it was stuck in a loop (there 3 rows in my Container table).
class Bibliotheque
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="name", type="string", length=60)
*/
private $name;
/**
* #ORM\Column(name="adress", type="string", length=60)
*/
private $adress;
}
class Conteneur
{
/**
* Many Containers have One library.
* #ORM\Id
* #ORM\OneToOne(targetEntity="Bibliotheque")
* #ORM\JoinColumn(name="libraryId", referencedColumnName="id")
*/
private $library;
/**
* One Container has One book.
* #ORM\Id
* #ORM\OneToOne(targetEntity="Livre")
* #ORM\JoinColumn(name="bookId", referencedColumnName="id")
*/
private $book;
/**
* #ORM\Column(name="quantity", type="integer")
*/
private $quantity;
}
class Livre
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #ORM\Column(name="pageCount", type="integer")
*/
private $pageCount;
/**
* Un livre a plusieurs auteurs
* #ORM\ManyToMany(targetEntity="Auteur")
* #ORM\JoinTable(name="books_authors",
* joinColumns={#ORM\JoinColumn(name="book_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="author_id", referencedColumnName="id")}
* )
*/
private $authors;
}
class Auteur
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="name", type="string", length=60)
*/
private $name;
/**
* #ORM\Column(name="surname", type="string", length=60)
*/
private $prenom;
/**
* #ORM\Column(name="birthDate", type="date")
*/
private $birthDate;
}
firstly, please use only French or English for class name and variable.
Add id inside class "Container" and remove annotation #ORM\id for attributes "library" and "book"
Define attribute "authors" with ArrayCollection and add method getAuthors(), addAuthor(Author $author), removeAuthor(Author $author)
And rename the tables name => "name_id" not "nameId"
look documention http://symfony.com/doc/current/doctrine.html
class Library
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=60)
*/
private $name;
/**
* #var string $address
*
* #ORM\Column(name="address", type="string", length=60)
*/
private $address;
}
class Container
{
/**
* #var integer $id
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* Many Containers have One library.
*
* #ORM\OneToOne(targetEntity="Library")
* #ORM\JoinColumn(name="library_id", referencedColumnName="id")
*/
private $library;
/**
* One Container has One book.
*
* #ORM\OneToOne(targetEntity="Book")
* #ORM\JoinColumn(name="book_id", referencedColumnName="id")
*/
private $book;
/**
* #ORM\Column(name="quantity", type="integer")
*/
private $quantity;
}
class Book
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $title
*
* #ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* #var string $pageCount
*
* #ORM\Column(name="page_count", type="integer")
*/
private $pageCount;
/**
* A book have many authors
*
* #var ArrayCollection $authors
*
* #ORM\ManyToMany(targetEntity="Auteur")
* #ORM\JoinTable(name="books_authors",
* joinColumns={#ORM\JoinColumn(name="book_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="author_id", referencedColumnName="id")}
* )
*/
private $authors;
/**
* #return ArrayCollection
*/
public function getAuthors()
{
return $this->authors;
}
/**
* #param Author $author
*
* #return $this
*/
public function addAuthor(Author $author)
{
$this->authors->add($author);
return $this;
}
/**
* #param Author $author
*
* #return $this
*/
public function removeAuthor(Author $author)
{
$this->authors->removeElement($author);
return $this;
}
}
class Author
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $firstname
*
* #ORM\Column(name="firstname", type="string", length=60)
*/
private $firstname;
/**
* #var string $lastname
*
* #ORM\Column(name="lastname", type="string", length=60)
*/
private $lastname;
/**
* #var DateTime $birthDate
*
* #ORM\Column(name="birthDate", type="date")
*/
private $birthDate;
}
I'm quite new to Symfony and Doctrine so....
In my application i have the following entities:
class Company
/**
* #ORM\Id()
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(name="name", type="string", length=255)
* #ORM\OneToMany(targetEntity="\UserBundle\Entity\User", mappedBy="company")
* #ORM\OneToMany(targetEntity="\AppBundle\Entity\Account", mappedBy="company")
*/
protected $name;
public function __construct()
{
$this->name = new ArrayCollection();
}
UserClass (FOSUserBundle):
class User extends BaseUser
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="firstname", type="string", length=255)
*/
protected $firstname;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
/**
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\Company")
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
*/
protected $company;
and Accounts:
class Account
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="num", type="integer")
*/
private $num;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #ORM\ManyToOne(targetEntity="\AppBundle\Entity\Company")
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
*/
protected $company;
There are the following relations.
one company --> many users, many users --> one company;
one company --> many accounts; many accounts --> one company;
is it possible to generate the relations as i did by:
* #ORM\Column(name="name", type="string", length=255)
* #ORM\OneToMany(targetEntity="\UserBundle\Entity\User", mappedBy="company")
* #ORM\OneToMany(targetEntity="\AppBundle\Entity\Account", mappedBy="company")
--> two target entities?
thx for your help...
This is absolutely wrong. If you don't want any links from company to users and accounts then you can just omit this fields. And this relation will be unidirectional. You will have link to Company from both User and Account.
Just omit wrong mappings:
/**
* #ORM\Column(name="name", type="string", length=255)
*/
protected $name;
If you want to create link to users and accounts related to company you need to define fields for these ArrayCollections like that:
/**
* #ORM\OneToMany(targetEntity="\UserBundle\Entity\User", mappedBy="company")
*/
protected $users;
I have a easy form with a data transformer, it works correctly (update, persist and delete) but I get a error on twig.
Impossible to invoke a method ("trans") on a NULL variable ("") in SonataDoctrineORMAdminBundle:CRUD:edit_orm_one_to_many.html.twig at line 30
error is in this line:
{{ nested_field.vars['sonata_admin'].admin.trans(nested_field.vars.label) }}
All fields have value in nested_field.vars['sonata_admin'] less my custom field
My code is this:
$formMapper
->add(
$formMapper->create('articleAmount', 'text')
->addModelTransformer($articleAmountToStringTransformer)
)
...
Entities
/**
* AppShopHasArticles
*
* #ORM\Table(name="app_shop_has_articles")
* #ExclusionPolicy("all")
* #ORM\Entity(repositoryClass="Nvia\ShopAppBundle\Entity\Repository\AppShopHasArticlesRepository")
* #ORM\HasLifecycleCallbacks()
*/
class AppShopHasArticles
{
/**
* #var \Nvia\CommonBundle\Entity\Article
*
* #ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\Article", inversedBy="appShopHasArticles")
* #ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
* #ORM\Id
* #Expose
*/
private $article;
/**
* #var \Nvia\CommonBundle\Entity\Country
*
* #ORM\ManyToOne(targetEntity="Nvia\CommonBundle\Entity\Country")
* #ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
* #ORM\Id
* #Expose
*/
private $country;
/**
* #var \Nvia\ShopAppBundle\Entity\AppShop
*
* #ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\ArticleAmount")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="country_id", referencedColumnName="country_id", nullable=false),
* #ORM\JoinColumn(name="article_id", referencedColumnName="article_id", nullable=false)
* })
* #Expose
*/
private $articleAmount;
...
}
/**
* ArticleAmount
*
* #ORM\Table(name="article_amount")
*/
class ArticleAmount
{
/**
* #var \Nvia\CommonBundle\Entity\Article
*
* #ORM\ManyToOne(targetEntity="Nvia\ShopAppBundle\Entity\Article", inversedBy="articleAmounts")
* #ORM\JoinColumn(name="article_id", referencedColumnName="id", nullable=false)
* #ORM\Id
*/
private $article;
/**
* #var \Nvia\CommonBundle\Entity\Country
*
* #ORM\ManyToOne(targetEntity="Nvia\CommonBundle\Entity\Country")
* #ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=false)
* #ORM\Id
*/
private $country;
/**
* #var float
*
* #ORM\Column(name="amount", type="float", precision=10, scale=0, nullable=false)
* #Expose
*/
private $amount;
/**
* #var \DateTime
*
* #ORM\Column(name="created_at", type="datetime", nullable=false)
*/
private $createdAt;
}
What am I doing wrong :/ ?
When using the create method of the formMapper sonata will not insert it's own logic.
Instead try something like this
$formMapper
->add('articleAmount', 'text')
->get('articleAmount')
->addModelTransformer($articleAmountToStringTransformer)
Tested on Sonata 2.4
On my index page are displayed different users. What i want to achieve is when someone click on the username to be redirected on other page where are displayed informations for the user.
Here is part of the twig code which will redirect the user to the hello route.
{% for user in users %}
<strong><em> {{ user.username}}
And this is hello route :
hello:
pattern: /hello
defaults: {_controller:AcmeWebBundle:Default:hello }
I have no idea how to implement this in the contoroler. Can i use variable in which are stored informations for the users from other function or i need to make db query? And how that query to be for particular user who is displayed? In addition is part from the entity. Thanks.
<?php
namespace Acme\Bundle\WebBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* baza
*
* #ORM\Table()
* #ORM\Entity
*/
class baza
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=30)
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=30)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="od", type="string", length=30)
*/
private $od;
/**
* #var string
*
* #ORM\Column(name="do", type="string", length=30)
*/
private $do;
/**
* #var float
*
* #ORM\Column(name="cena", type="float")
*/
private $cena;
/**
* #var string
*
* #ORM\Column(name="comment", type="text")
*/
private $comment;
/**
* #var integer
*
* #ORM\Column(name="rating", type="integer")
*/
private $rating;
/**
* #var \DateTime
*
* #ORM\Column(name="date", type="date")
*/
private $date;
/**
* #var string
*
* #ORM\Column(name="car", type="string", length=20)
*/
private $car;
Try this in your template:
{% for user in users %}
<strong><em><a href="{{ path('hello', {"id": user.id}</a>
and this in your routing:
hello:
pattern: /hello/{id}
and your controller will have something like:
public function helloAction(Request $request, $id)
then in your controller retrieve the user by id. This and the rest of it is can be inferred in the book.
Hope this helps