Can Doctrine2 #OrderBy a calculated field? - doctrine-orm

I want to sort my model's associated ArrayCollection with a quotient, like this (I know the following code doesn't work):
/**
* #OneToMany (targetEntity="Review", mappedBy="product")
* #OrderBy ({"voted_up / voted_down" = "DESC"})
*/
protected $reviews;
Is something this possible directly in the model definition or do I need to simply use a sort() on the ArrayCollection when requesting the data?

With Doctrine 2.1 you can do that directly in the model definition, but not with #OrderBy. You can define DQL snippets at model level, like stated in the 2.1 Beta release notes:
Named DQL Queries in Metadata: You can add dql queries in the mapping files using #NamedQueries(#NamedQuery(name="foo", query="DQL")) and access them through $em->getRepository()->getNamedQuery().
As such you can create your DQL query with the ORDER BY keywords, something like:
SELECT c.id, c.text, (c.voted_up / c.voted_down) AS sortkey FROM Comment c
ORDER BY sortkey DESC
So, I imagine you add this annotation to the model definition, something like:
/**
* #Entity
* #Table(name="comment")
* #NamedQueries(#NamedQuery(name="sortedComment", query="SELECT c.id, c.text, (c.voted_up / c.voted_down) AS sortkey FROM Comment c ORDER BY sortkey DESC"))
*/
class Comment {
...
}
And then on your code call:
$em->getRepository("Comment")->getNamedQuery("sortedComment");
I didn't test this, but you get the idea.

Related

Will Doctrine2 select all fields on all associations (JOINS from a query) to populate the full aggregate object?

I'm researching whether to try Doctrine2 or not. One thing that scares me is the over SELECTing of columns I don't need (ie. consider lots of varchars being selected unnecessarily).
You might ask: but don't you want your full entity object filled? Yes, unless I'm looking for an array hydration. However, many times I don't need the full aggregation filled. Take the association shown below. If I query the Users table with a JOIN on Address, will all the columns from the address table be SELECTed as well (and therefore populated into an address object inside of users object)? Now imagine we have more JOINs. This could get really bad. What if I only want the fields from User populated in just a users only object? I guess I'm a little confused at what Doctrine is doing behind the scenes with associations and query JOINs.
/** #Entity **/
class User
{
// ...
/**
* #ManyToOne(targetEntity="Address")
* #JoinColumn(name="address_id", referencedColumnName="id")
**/
private $address;
}
/** #Entity **/
class Address
{
// ...
}
So does Doctrine2 populate all the fields of all the objects within the aggregate after a query (unless I specifiy partial)?
It depends on your query, but generally it is not implicit.
Using the query builder, you can fetch the associated record like this:
<?php
$qb = $em->createQueryBuilder();
$query = $qb->select(array("u", "a"))
->from("User", "u")
->innerJoin("u.address", "a")
->getQuery();
In the select() statement you specify what to fetch, in this case you get both.
If you only fetch the User records, then when you get the associated record with $user->getAddress(), Doctrine will make the query on the fly and hydrate the Address record for you.
That said, performance wise it is better to select both entities so Doctrine will make only one query and not 1+N queries

Doctrine 2 orm:validate-schema fails because of constraint

I have a database with several one-to-many/many-to-one relationships. For example, I have table called Students, and a related table called StudentNotes. The StudentNotes table has a foreign key called student_id. I want the foreign key to have the constraint on delete = cascade.
I set up my Doctrine 2 entities with the property #JoinColumn(on="CASCADE") and updated the database schema. Unfortunately, whenever it does this, it sets the on delete to "restrict". What am I doing wrong?
Here's the relevant code from my Students entity:
/**
* #var Collection Notes
*
* #OneToMany(targetEntity="StudentNotes", mappedBy="student")
* #JoinColumn(onCascade="DELETE")
*/
protected $notes;
And from StudentNotes:
/**
* #var \Entities\Students Student
*
* #ManyToOne(targetEntity="Students", inversedBy="notes")
* #OrderBy({"datetime"="DESC"})
*/
protected $student;
I've even tried adding all of the column information (i.e., name="student_id", referencedColumnName="id"), but nothing changes.
EDIT
I messed up when I originally wrote this: I wrote #JoinColumn(onCascade="DELETE"), when I meant to write #JoinColumn(onDelete="CASCADE"). Either way, this is not working properly: validate-schema fails because the database is not in sync with the schema.
onCascade do not exists, you need instead onDelete="CASCADE".
#JoinColumn(onDelete="CASCADE")
OK, I figured out what I'd forgotten to do!
I had neglected to go back to the command line and run the following command:
php doctrine.php orm:schema-tool:update --force
This did the trick

Delete associated/referenced entity

I have a table product, the items in these table are referenced in tables such as cart_item and order_item as well as shipping_item etc.
All these references are optional (the product_id is set to nullable in those tables).
I need to have a way to delete a product and still keeping the other tables's records. One way I can think of is to go into all those tables, set the product_id to null, then go back to the product table to delete. However, since I may not know all the tables that are referencing to product (many other bundles can have entities that are referencing to this product), is there a way that I can know all these association to loop through and set null?
(Or perhaps there is a better way?)
PS: the idea that this is a shopping cart and the owner may want to remove expired products to clean up but for ordered, shipped items they still need to keep records.
Edit1:
This is the definition of the product reference in the OrderItem entity:
/**
* #var \Product
*
* #ORM\ManyToOne(targetEntity="Product")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="product_id", referencedColumnName="id")
* })
*/
private $product;
The error I'm getting:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1451
Cannot delete or update a parent row: a foreign key constraint fails
(test.order_item, C ONSTRAINT fk_order_item_product1 FOREIGN KEY
(product_id) REFERENCES product (id) ON DELETE NO ACTION ON
UPDATE NO ACTION)
Edit2:
I initially set onupdate="SET NULL" to the order_item entity and thought that was enough, it was not:
/**
* #var \Product
*
* #ORM\ManyToOne(targetEntity="Product")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="product_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
* })
*/
private $product;
After that, I had to update db schema as well.
Assuming you have the proper relations set up between the owning entity product and the other entities e.g. cart_item that should have a foreign_key, your wanted behaviour is the default for doctrine 2.
Take a look here in the manual
As an example they show the deletion of a User entity and its corresponding Comments
$user = $em->find('User', $deleteUserId);
foreach ($user->getAuthoredComments() AS $comment) {
$em->remove($comment);
}
$em->remove($user);
$em->flush();
The example states:
Without the loop over all the authored comments Doctrine would use an UPDATE statement only to set the foreign key to NULL and only the User would be deleted from the database during the flush()-Operation.
This suggests to me that in your case you actually want that behaviour. So just remove the product entity and doctrine 2 will automatically find all other entities with a foreign_key belonging to that product and will set it to NULL
Edit
Your error message suggests that upon attempted removal of the product entity there are still foreign_keys present, i.e. they have not been set to null properly by Doctrine.
You need to be sure to add the cascade property, specifically remove to your entity relationship. It would look something like the following:
<?php
class Product
{
//...
/**
* Bidirectional - One-To-Many (INVERSE SIDE)
*
* #OneToMany(targetEntity="Cart", mappedBy="product", cascade={"remove"})
*/
private $carts;
//...
}

Doctrine2: How to keep custom join table synchronized in a bi-directional Many-to-Many association?

The statement
I'm trying to reproduce the automatic Doctrine mechanism for handling Many-to-Many bidrectional relationships, but introducing a custom join table.
I've already digged into similar questions:
Joining-Table with Metadata Impairs Getters/Setters - Doctrine 2
but it doesn't really help me because it's absolutely unidirectional
doctrine2 many to many self referencing with intermediate details
but this one does not even talk about managing the relations
Doctrine2: Best way to handle many-to-many with extra columns in reference table
is very interesting. However, although the author mentions its bidirectional needs, he doesn't cover the case.
I'm aware that a join table with extra fields is not an association anymore, just a third entity that refers to the two other ones. And from that statement, it's obvious that one cannot expect it to work out-of-the-box as an implicit Many-to-Many association managed by Doctrine.
But i want to have this trio to work as a simple, straight, bidirectional Many-to-Many association, so that means using proxy methods and relying on a Logic class.
The code
There's a Category entity and a Product entity:
/**
* #ORM\Table(name="category")
* #ORM\Entity(repositoryClass="CategoryRepository")
*/
class Category
{
/**
...
*/
protected $id = null;
/**
* #ORM\OneToMany(targetEntity="CategoryProduct", mappedBy="category", fetch="LAZY", cascade={"persist"})
*/
protected $categoryProducts;
}
and
/**
* #ORM\Table(name="product")
* #ORM\Entity(repositoryClass="ProductRepository")
*/
class Product
{
/**
...
*/
protected $id = null;
/**
* #ORM\OneToMany(targetEntity="CategoryProduct", mappedBy="product", fetch="LAZY", cascade={"persist"})
*/
protected $categoryProducts;
}
and of course a join entity:
/**
* #ORM\Table(name="category_product")
* #ORM\Entity(repositoryClass="CategoryProductRepository")
*/
class CategoryProduct
{
/**
...
*/
protected $id = null;
/**
* #ORM\ManyToOne(targetEntity="Category", fetch="EAGER", inversedBy="categoryProducts")
* #ORM\JoinColumn(onDelete="CASCADE")
*/
protected $category;
/**
* #ORM\ManyToOne(targetEntity="Product", fetch="EAGER", inversedBy="categoryProducts")
* #ORM\JoinColumn(onDelete="CASCADE")
*/
protected $product;
/**
* #ORM\Column(type="boolean", nullable=true)
*/
protected $starred = false;
}
The problem
How to keep an up-to-date list of CategoryProduct entities available to both entities in a pure ORM-style way? In an ORM, everything is managed on the Object layer. Changes to DB are made only on user's request, but it's not compulsory as long as one only works from the ORM point of view. In other words:
$category->addProduct($product);
does not write anything to the DB, and does not even persist any object to the entity manager, but one can still retrieve or remove this product from the list as long as the script runs.
In the case of a custom join table, it's different, because when one wants to add a product, he must create and persist a CategoryProduct entity. So what if we need to retrieve this association from the inverse side?. Here is a code sample that demonstrates my problem:
$product->addCategory($category);
$category->addProduct($product);
In this bidirectional association, how can the $category::addProduct function know about the instance of CategoryProduct entity created by $product::addcategory? The risk is to create two similar join entities for the same association, and i don't know how to avoid it.

Doctrine: complicated select with two ManyToMany relationships

I need to query a number of issues in a table of a issue tracking system limiting this query by a complicated condition:
Issues (Entity) are grouped into categories (another Entity). Persons (Entity) are members of multiple roles (fourth Entity), this is one ManyToMany relationship. And finally, a role can have access on one or many categories, this is the second ManyToMany relationship.
<?php
/**
* #Entity
* #Table(name="issue")
*/
class Issue
{
/**
* #ManyToOne(targetEntity="Category", fetch="EAGER")
* #JoinColumn(name="category", referencedColumnName="id", onDelete="RESTRICT", nullable=false)
*/
private $category;
…
}
/**
* #Entity
* #Table(name="category")
*/
class Category
{
/**
* #ManyToMany(targetEntity="Role", mappedBy="categories")
*/
private $roles;
…
}
/**
* #Entity
* #Table(name="role")
*/
class Role
{
/**
* #ManyToMany(targetEntity="Person", mappedBy="roles")
*/
private $persons;
/**
* #ManyToMany(targetEntity="Category", inversedBy="roles")
* #JoinTable(name="role_has_access_on_category",
* joinColumns={#JoinColumn(name="role_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="category_id", referencedColumnName="id")}
* )
*/
private $categories;
…
}
/**
* #Entity
* #Table(name="person")
*/
class Person
{
/**
* #ManyToMany(targetEntity="Role", inversedBy="persons")
* #JoinTable(name="person_is_member_of_role",
* joinColumns={#JoinColumn(name="person_id", referencedColumnName="id")},
* inverseJoinColumns={#JoinColumn(name="role_id", referencedColumnName="id")})
*/
private $roles;
…
}
I have left all fields except the relationship ones away, of course there are primary keys and a lot more columns there…
I want to retrieve all issues that belong to categories to which a person with a given primary key has access via the roles it is member of.
At first I just started to experiment how to query ManyToMany relationships, so the code below does not really resemble my target.
I have finally found out how I can get the query to retrieve the other side of one ManyToMany relationship, so I can already get the roles a person belongs to. But this query does not fetch the categories a role has access to.
$qb = $this->em->createQueryBuilder();
$qb->select('person')
->addSelect('role')
->addSelect('category')
->from('Person', 'person')
->innerJoin('person.roles', 'role')
->innerJoin('role.categories', 'category');
$result = $qb->getQuery()->getResult();
$result contains the persons data with all associated roles, but a blank array of categories instead of the entities. The final query would start from the issue side of course, but for now I would only like to get through to the other side…
So now I wonder whether I have to take all the roles and loop through them to fetch all categories. Is there no easier Doctrine way?
By the way, that's the SQL I would use:
SELECT issue.* FROM person AS p, person_is_member_of_role AS pim, role_has_access_on_category AS rha, issue
WHERE
p.id = pim.person_id AND
pim.role_id = rha.role_id AND
rha.category_id = todo.category AND
p.id = ?;
I hope this all makes it clear somehow, otherwise I will revise my question…
You are over-complicating it by trying to put all join conditions in the WHERE clause.
If I understood the question correctly, the query looks like this:
SELECT
i
FROM
Issue i
JOIN
i.category c
JOIN
c.roles r
JOIN
r.persons p
WHERE
p.id = :personId
Translated in QueryBuilder API:
$qb = $entityManager->createQueryBuilder();
$issues = $qb
->select('i')
->from('Issue', 'i')
->innerJoin('i.category', 'c')
->innerJoin('c.roles', 'r')
->innerJoin('r.persons', 'p')
->andWhere($qb->expr()->eq('p.id', ':personId'))
->setParameter('personId', $personId)
->getQuery()
->getResult();
Also, consider avoiding the QueryBuilder if there's no good reason to use it. After all, it's just a string builder.