How to OrderBy in Many to Many Relationship in Symfony - php

I have two tables products and categories. Products can have many categories and categories can have many products.(The relationship between these two tables is many-to-many)
To map the associations between these two tables I have created another new table product_categories which stores product_id and category_id.
Following is my annotations used in entities to join these three tables.
Product Entity
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Category", mappedBy="products")
*/
private $categories;
Category Entity
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Product", inversedBy="categories")
* #ORM\JoinTable(name="product_categories",
* joinColumns={
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
* }
* )
*/
private $products;
Now the problem is, I want to order the records in product_categories into a certain order. So I added a new column called serial_number to product_categories table.
The result what I am trying to achieve is when I am retrieving categories to a product using $product->getCategories() the resulting category array should be ordered by the serial_number.
I have tried the following annotations.
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Product", inversedBy="categories")
* #ORM\JoinTable(name="product_categories",
* joinColumns={
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
* }
* )
* #OrderBy({"product_categories.serial_number"="DESC"})
*/
private $products;
But this results in an annotation syntax error.
Fatal error: Uncaught Doctrine\Common\Annotations\AnnotationException: [Semantical Error] The annotation "#OrderBy" in property ProductBundle\Entity\Product::$products was never imported. Did you maybe forget to add a "use" statement for this annotation? in /var/www/sonicwall.local/app/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/AnnotationException.php on line 54
When I use #ORM\OrderBy({"product_categories.serial_number"="DESC"}), I do not get a syntax error but results are not getting ordered either.
My question is, is there a way to achieve this using annotations without me having to create new Entities and Repositories for product_categories table?

You have to use #ORM\OrderBy:
#OrderBy acts as an implicit ORDER BY clause for the given fields, that is appended to all the explicitly given ORDER BY items.
All collections of the ordered type are always retrieved in an ordered fashion.
To keep the database impact low, these implicit ORDER BY items are only added to a DQL Query if the collection is fetch joined in the DQL
query.
/**
* #var \Doctrine\Common\Collections\Collection
*
* #ORM\ManyToMany(targetEntity="Product", inversedBy="categories")
* #ORM\JoinTable(name="product_categories",
* joinColumns={
* #ORM\JoinColumn(name="category_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
* }
* )
* #ORM\OrderBy({"serialNumber"="DESC"})
*/
private $products;
You tried to do something similar, but with product_categories.serial_number. You do not need product_categories because the order by will try to sort every member of the collection with its own field. So just writing serialNumber will sort by each product serial number.
However, I just saw that you already tried something similar in comments so makes sure to use the attribute name as your field: serialNumber, serial, ... as it appear with $serialNumber

Related

Implementing getters for joined doctrine 2 collection

I'm trying to create simple getter methods in original entity for specific item in doctrine collection of joined entity.
Main entity looks something like this:
class Product
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
/**
* #ORM\OneToMany(targetEntity="File", mappedBy="product")
*/
private $files;
}
And joined entity:
class PrankFile
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Product", inversedBy="files")
* #ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $product;
/**
* #var string
*
* #ORM\Column(name="type", type="string", length=16)
*/
private $type;
...
My DQL in repository class is very simple:
return $this->getEntityManager()
->createQuery('SELECT p, f FROM AppProductBundle:Product p INNER JOIN p.files f ORDER BY p.created DESC')
->setMaxResults($limit)
->getResult();
In files entity type field tells me what kind of a file is it (image, sound, video, demo etc...)
Problem comes when I wish to print out a list of all products and display image next to product details, I would hate to loop through product files for each product displayed.
Is it possible to create some simple getter on product entity to fetch file of certain type?
Or maybe it would be better to create more complex DQL query for this, again how to do this?
I can't just fetch image record from files in DQL because I need all files for certain products.
Any help with this would be most welcome.
You can filter directly on collections using the filter api. If the collection is not loaded already Doctrine will apply your filter on a SQL level, giving you max performance. In case the collection is already eager loaded Doctrine will filter the ArrayCollection in memory.
use Doctrine\Common\Collections\Criteria;
class Product
{
public function getFilesByType($type)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq("type", $type))
return $this->files->matching($criteria);
}
}

adding created timestamp to join table in doctrine2

I have the following property in my User entity to track followers and following. Basically a user can follow other user as well. I have a join column called app_user_follow_user, however I also wanted to add a timestamp of whenever someone follows another user, when did it happen. How can I specify a created timestamp via this ORM?
/**
* #ORM\ManyToMany(targetEntity="User", mappedBy="following")
*/
protected $followers;
/**
* #ORM\ManyToMany(targetEntity="User", inversedBy="followers")
* #ORM\JoinTable(name="app_user_follow_user",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="follow_user_id", referencedColumnName="id")}
* )
*/
protected $following;
Doctrine ManyToMany relationships are used when your join table has two columns. If you need to add another column you have to convert the relationship to OneToMany on both sides and ManyToOne on the joined entity.
This is entirely untested but it will hopefully give you the gist.
User Entity
/**
* #ORM\OneToMany(targetEntity="AppUserFollowUser", mappedBy="appUser")
*/
protected $followers;
/**
* #ORM\OneToMany(targetEntity="AppUserFollowUser", mappedBy="followUser")
*/
protected $following;
AppUserFollowUser Entity
/**
* #ORM\Table(name = "app_user_follow_user")
*/
class AppUserFollowUser
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="User", inversedBy="followers")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $appUser;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="User", inversedBy="following")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="follow_user_id", referencedColumnName="id")
* })
*/
private $followUser;
/**
* #ORM\Column(name="created_date", type="datetime", nullable=false)
*/
private $createdDate;
}
I think that you will have to create a link entity manually (entiy1 onetomany linkEntity manytoone entity2.
Because, the usual link entity are automated and should be as simple and (data less) as possible, so doctrine can take all the controle over it,
imagine you need to get the timestamp, how can you do it on an (none hard coded) entity, you will need a getter, and the annotations are not supposed to contains code.

doctrine query builder join adding table twice

UPDATED at bottom:
I am trying to do what should be a simple join between two tables. I have a Gig table and a Venue table for a simple band site that I am building using Symfony2 (2.2). It's my first time with Symfony2 and doctrine so it is possible I am going completely in the wrong direction. I have created and populated the tables with DataFixtures and have verified that the ID relationships are correct. The problem I am getting is that the resulting DQL query has the Gig table referenced twice in the FROM section and that is causing me to get back several instances of the same record instead of the x number of records I am expecting. I don't know what I am doing wrong for that to be happening. Also, there may be an easier way of doing this but I am exploring all of my options since I am teaching myself Symfony2 in the process of building the site.
The Gig table contains a venue_id pointing to a Venue table that is defined in the Gig entity as a ManyToOne relationship (shown below). Using a doctrine findAll everything seems to work fine with the Venue class in the Gig Entity being populated correctly. I am trying to create a flat view of a few of the most recent Gigs to be displayed on the front page so I figured I would try to use a Join and include only the fields I need.
Here is the Repository Query:
public function getGigsWithLimit($maxGigs)
{
$qb = $this->createQueryBuilder('b')
->select('
g.gigDate,
g.startTime,
g.endTime,
g.message1 as gig_message1,
g.message2 as gig_message2,
g.url,
v.name,
v.address1,
v.address2,
v.city,
v.state,
v.zip,
v.phone,
v.url as venue_url,
v.message1 as venue_message1,
v.message2 as venue_message2,
v.message3 as venue_message3'
)
->from('WieldingBassBundle:Gig', 'g')
->leftJoin('g.venue', 'v')
->orderBy('g.gigDate', 'DESC')
->setMaxResults($maxGigs);
return $qb->getQuery()->getResult();
}
Here is the DQL it creates:
SELECT
g0_.id AS id0,
g0_.gig_date AS gig_date1,
g0_.start_time AS start_time2,
g0_.end_time AS end_time3,
g0_.message1 AS message14,
g0_.message2 AS message25,
g0_.url AS url6,
v1_.name AS name7,
v1_.address1 AS address18,
v1_.address2 AS address29,
v1_.city AS city10,
v1_.state AS state11,
v1_.zip AS zip12,
v1_.phone AS phone13,
v1_.url AS url14,
v1_.message1 AS message115,
v1_.message2 AS message216,
v1_.message3 AS message317
FROM
Gig g2_,
Gig g0_
LEFT JOIN
Venue v1_ ON g0_.venue_id = v1_.id
LIMIT
6
The Gig g2_ is my problem. If I delete it and execute the query everything is as expected. I don't know what is generating that.
The first table Gigs Entity looks like this (I am leaving out the getters and setters):
/**
* Gig
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Wielding\BassBundle\Entity\GigRepository")
* #ORM\HasLifecycleCallbacks
*/
class Gig
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var \DateTime
*
* #ORM\Column(name="gig_date", type="date")
*/
private $gigDate;
/**
* #var \DateTime
*
* #ORM\Column(name="start_time", type="datetime")
*/
private $startTime;
/**
* #var \DateTime
*
* #ORM\Column(name="end_time", type="datetime")
*/
private $endTime;
/**
* #var string
*
* #ORM\Column(name="message1", type="string", length=50, nullable=true)
*/
private $message1;
/**
* #var string
*
* #ORM\Column(name="message2", type="string", length=50, nullable=true)
*/
private $message2;
/**
* #var string
*
* #ORM\Column(name="url", type="string", length=128, nullable=true)
*/
private $url;
/**
* #var integer
*
* #ORM\Column(name="venue_id", type="integer")
*/
private $venueId;
/**
* #ORM\Column(type="datetime")
*/
protected $created;
/**
* #ORM\Column(type="datetime")
*/
protected $updated;
/**
* #ORM\ManyToOne(targetEntity="Venue", cascade="persist")
* #ORM\JoinColumn(name="venue_id", referencedColumnName="id")
*/
protected $venue;
The Venue table is simple and does not have any relationships defined so I will leave it out unless it is asked for.
Any ideas? Thanks for any help.
Andrew
I removed everything except what would recreate the problem and here is what I was left with:
I simplified the repository method to:
public function getGigsWithLimit2($maxGigs)
{
$qb = $this->createQueryBuilder('a')
->select('g.id')
->from('WieldingBassBundle:Gig', 'g')
->setMaxResults($maxGigs);
return $qb->getQuery()->getResult();
}
This now generates:
SELECT
g0_.id AS id0
FROM
Gig g1_,
Gig g0_
LIMIT
6
There is that darn Gig g1_ problem again. I got the "Explain Query" from the Symfony profiler and it shows:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE g1_ index IDX_ED7D664240A73EBA 4 9 Using index
1 SIMPLE g0_ index IDX_ED7D664240A73EBA 4 9 Using index; Using join buffer
I don't pretend to know what that means but it shows both table entries with different information about how it was used.
whats the use of "venueId" if you already got "venue" which contains the foreign key?
I found the problem. I am not used to Doctrine and was using the ->from in a repository that did not need it since the entity was automatically related through the annotations. My earlier query that worked was in the controller and not a repository so the ->from was necessary.
You are trying to do SQL. Doctrine is different. Your query fetches every field. Doctrine prefers to fetch entities. I think you probably want this:
public function getGigsWithLimit($maxGigs)
{
$qb = $this->createQueryBuilder('g')
->leftJoin('g.venue', 'v')
->orderBy('g.gigDate', 'DESC')
->setMaxResults($maxGigs);
return $qb->getQuery()->getResult();
}
The return result is a list of entities which you can call all the specific methods directly. You are still welcome to specify fields with doctrine, if you want partial objects, but I've found the normal method of fetching the entire entity covers most of my needs.
This is a fundamentally different paradigm than SQL and will take some getting used to.

Doctrine multiple composite foreign key

I am trying to construct an object with two composite foreign keys pointing out to the same object, but they seem to have the same data, like doing the join only on one column, product_id.
class PostpaidProduct extends Product {
/**
* #ManyToOne(targetEntity="Bundle", fetch="EAGER", cascade={"persist"})
* #JoinColumn(name="bundle_voice_id", referencedColumnName="id")
*/
private $bundleVoice;
/**
* #ManyToOne(targetEntity="Bundle", fetch="EAGER", cascade={"persist"})
* #JoinColumn(name="bundle_data_id", referencedColumnName="id")
*/
private $bundleData;
/**
* #OneToMany(targetEntity="BundlePromo", mappedBy="product", fetch="EAGER", cascade={"persist"})
* #JoinColumns({
* #JoinColumn(name="id", referencedColumnName="product_id"),
* #JoinColumn(name="bundle_voice_id", referencedColumnName="bundle_id")
* })
*/
private $bundleVoicePromos;
/**
* #OneToMany(targetEntity="BundlePromo", mappedBy="product", fetch="EAGER", cascade={"persist"})
* #JoinColumns({
* #JoinColumn(name="id", referencedColumnName="product_id"),
* #JoinColumn(name="bundle_data_id", referencedColumnName="bundle_id")
* })
*/
private $bundleDataPromos;
}
What would be wrong with my mapping?
Is it possible to have composite foreign keys but without being primary keys?
I have talked to one of the developers of Doctrine and he said that the #JoinColumns field in #OneToMany relationships is ignored. The alternative would be having just one foreign key and to use matching criterias in an entity method, filtering for the entries needed based on the other key. Another solution would be having repository methods specific for getting these values.
Also, in OneToMany relationships eager fetching does not work, so it does separate queries for all children. So if you have a product with multiple prices, when fetching a product it will do separate queries for fetching the prices.

Doctrine 2.1 reference table

I have a reference table album_content which has: album_id, content_id, and sort_key. I set it up as an entity with #ManyToOne relations:
/**
* #ManyToOne(targetEntity="Album")
* #JoinColumns({
* #JoinColumn(name="album_id", referencedColumnName="id")
* })
*/
private $albumId;
/**
* #ManyToOne(targetEntity="Content")
* #JoinColumns({
* #JoinColumn(name="content_id", referencedColumnName="id")
* })
*/
private $contentId;
/**
* #Column(name="sort_key", type="integer", nullable=false)
*/
private $sortKey;
Right now Doctrine is complaining No identifier/primary key specified. What's the correct annotation to reference these without adding an extra ID column?
First, you probably shouldn't be naming things $contentId or $albumId, but instead just call them $content and $album.
That said, the quick solution is to add #Id annotations to both of your associations.
The manual goes into further detail about using composite keys in Doctrine 2.

Categories