Symfony2 Nested forms and ManyToMany relations - symfony

I'm trying to handle a form which is quite complex for me...
We have Collections, which contain books (OneToMany), with articles(OneToMany) and their authors (ManyToMany).
The user can edit a book: he can add or remove an article, and add or remove some authors for each article. There are nested forms : book>article>author.
If the author is new in the Collection, it is created for that Collection.
Entities descriptions look fine, database is generated by the console and seems consistent.
This is working fine if I don't have to deal with authors using the book edition form. If the author exists, I have a duplicate entry bug. If the author is new, I have a "Explicitly persist the new entity or configure cascading persist operations on the relationship" bug.
Here is the code:
public function onSuccess(Book $book)
{
$this->em->persist($book);
foreach($book->getArticles() as $article)
{
$article->setUrlname($this->mu->generateUrlname($article->getName()));
$article->setBook($book);
// Saving (and creating) the authors of the book
foreach ($this->collectionWithAuthors->getAuthors() as $existAuthor){
foreach($article->getAuthors() as $author) {
$authorUrlname=$this->mu->generateUrlname($author->getFirstname().' '.$author->getLastname());
if ( $existAuthor->getUrlname() == $authorUrlname) { // The author is existing
$article->addAuthor($existAuthor);
$this->em->persist($existAuthor);
}else{ // New Author
$newAuthor = new Author();
$newAuthor->setCollection($this->collectionWithBaseArticles);
$newAuthor->setLastname($author->getLastname());
$newAuthor->setFirstname($author->getFirstname());
$newAuthor->setUrlname($authorUrlname);
$this->em->persist($newAuthor);
$article->addAuthor($newAuthor);
}
}
}
$this->em->persist($article);
}
$this->em->flush();
}
I don't know how to use cascades. But the $article->addAuthor() is supposed to call $authors->addArticle():
Article Entity extract
/**
* #ORM\ManyToMany(targetEntity="bnd\myBundle\Entity\Author", mappedBy="articles")
*/
private $authors;
/**
* Add authors
*
* #param bnd\myBundle\Entity\Author $authors
* #return Article
*/
public function addAuthor(\bnd\myBundle\Entity\Author $authors)
{
$this->authors[] = $authors;
$authors->addArticle($this);
}

The logic in foreach statement is wrong. Suppose we have next authors:
Persisted authors (collectionWithAuthors):
John
Eric
Submitted authors
Ada
Eric
So for every existing author (John and Eric) the script loop thru new authors:
foreach ([John, Eric] as $author) {
foreach([Ada, Eric] as $newAuthor) {
// John author: the script persist Ada(right) and Eric(wrong) as new authors
// Eric author: the script persist Ada(wrong), but not Eric(right)
}
}
The solution is to replace article authors with existing authors (if there is similar)
foreach ($article->getAuthors() as $key => $articleAuthor) {
$authorUrlname=$this->mu->generateUrlname($articleAuthor->getFirstname().' '.$articleAuthor->getLastname());
$foundAuthor = false;
// Compare article author with each existing author
foreach ($this->collectionWithAuthors->getAuthors() as $existAuthor) {
if ($existAuthor->getUrlname() == $authorUrlname) {
$foundAuthor = true;
break; // It has found similar author no need to look further
}
}
// Use $existAuthor as found one, otherwise use $articleAuthor
if ($foundAuthor) {
$article->removeAuthor($articleAuthor); // Remove submitted author, so he wont be persisted to database
$article->addAuthor($existAuthor);
} else {
// Here you dont need to create new author
$articleAuthor->setCollection($this->collectionWithBaseArticles);
$articleAuthor->setUrlname($authorUrlname);
}
...
}
$this->_em->persist($article);
You have noticed i removed any author persistent from the loop, to persist these authors its better to set cascade={'persist'} in $authors annotation of Article Entity Class
/**
* #ORM\ManyToMany(targetEntity="Author", cascade={"persist"})
* #ORM\JoinTable(...)
*/
protected $authors;
UPD:
I forgot to mention one thing about cascade persistent. To persist relation betweed article and author you also have to add relation to author entity. Edit the addAuthor() method in the Article entity as below:
public function addAuthor(Author $author)
{
// Only add author relation if the article does not have it already
if (!$this->authors->contains($author)) {
$this->authors[] = $author;
$author->addArticle($this);
}
}
Also, it's a good practice to define default values for a collection of entities in the constructor:
use Doctrine\Common\Collections\ArrayCollection;
// ...
public function __construct()
{
$this->authors = new ArrayCollection();
}

Related

Api platform aliasing filters for nested resources

I'm currently using API Platform and its default SearchFilter and it works as intended.
However, filtering on a deep relationship between resources can be heavy by its quite long query string in the url. (I have got multiple entities like this.)
For instance I want to search every books listed in the stores of a specific country :
{url}/books?department.store.city.country.name=italy
Is there any way to edit the #ApiFilter(SearchFilter::class, properties={}) in order to get simply at the end ?
{url}/books?country_filter=italy
Thanks !
Thank you for your advices,
After some (hours of) researches, I came to the conclusion to extend the SearchFilter when creating my personnal CountryFilter :
In my entity class :
/*
* #ApiFilter(CountryFilter::class, properties={
* "country_filter": "department.store.city.country.name",
* })
*/
In my App\Filter\CountryFilter.php :
<?php
namespace App\Filter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractContextAwareFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use Doctrine\ORM\QueryBuilder;
final class CountryFilter extends SearchFilter
{
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $operationName = null)
{
foreach($this->properties as $alias => $propertyName){
if($alias == $property){
$property = $propertyName;
break;
}
}
/*
if (
null === $value ||
!$this->isPropertyEnabled($property, $resourceClass) ||
!$this->isPropertyMapped($property, $resourceClass, true)
) {
return;
}
*/
// The rest of the SearchFilter.php copy/pasted code ...
}
public function getDescription(string $resourceClass): array
{
// ....
}
}
You can make your one custom api filter and add your own logic in it.Call it country_filter and pass one value only, after that a custom query will search in database and return the rows. To make one you have to extend the AbstractFilter class and after that you have to add this filter it in your entity. A good tutorial from official site is here and the next chapter here

Sorting list of VirtualPages on a field from its Page

I have an AreaPage with $many_many VirtualPages:
class AreaPage extends Page {
/**
* #var array
*/
private static $many_many = [
'RelatedVirtualPages' => 'VirtualPage'
];
// ...
}
The RelatedVirtualPages are copying content from ContentPages:
class ContentPage extends Page {
/**
* #var array
*/
private static $db = [
'Highlighted' => 'Boolean'
];
// ...
}
What's the best way to sort RelatedVirtualPages on the Highlighted db field of the ContentPage that it's copying?
Virtual Pages could be pointed at pages of different types and there is no enforcement that all of those pages are ContentPages, or at least pages that have a Hightlighted db field. You can ensure this manually when you create your SiteTree, but users could come along and screw it up so keep this in mind.
Here is some psuedo-code that might help you get started. It assumes that all virtual pages are ContentPages. If you will have multiple types of VirtualPages referenced by an AreaPage then this is probably not sufficient.
$virtualPages = $myAreaPage->RelatedVirtualPages();
$contentSourcePages = ContentPage::get()->byIDs($virtualPage->column('CopyContentFromID'));
$sortedSourcePages = $contentSourcePages->sort('Highlighted','ASC');
You possibly could also use an innerJoin, but then you have to deal with _Live tables and possibly multiple page tables (again if not just using ContentPage as VirtualPage) which could lead to some complicated scenarios.
Update
So, to summarize in my own words, you need a list of the VirtualContentPages linked to a specific AreaPage sorted on the Highlighted field from the ContentPage that each VirtualContentPage links to. If this summary is accurate, would this work:
$sortedVirtualPages = $myAreaPage->RelatedVirtualPages()
->innerJoin('ContentPage', '"ContentPage"."ID" = "VirtualContentPage"."CopyContentFromID"')
->sort('Highlighted DESC');
I could not find a very clean method, but did find two ways to achieve this. The function goes in the class AreaPage
First
public function getRelatedVirtualPages()
{
$items = $this->getManyManyComponents('RelatedVirtualPages');
$highlighted = $items->filterByCallback(function($record, $list) {
if($record->CopyContentFrom() instanceOf ContentPage) {
//return ! $record->CopyContentFrom()->Highlighted; // ASC
return $record->CopyContentFrom()->Highlighted; // DESC
}
});
$highlighted->merge($items);
$highlighted->removeDuplicates();
return $highlighted;
}
Second (the method you described in the comments)
public function getRelatedVirtualPages()
{
$items = $this->getManyManyComponents('RelatedVirtualPages');
$arrayList = new ArrayList();
foreach($items as $virtualPage)
{
if($virtualPage->CopyContentFrom() instanceOf ContentPage) {
$virtualPage->Highlighted = $virtualPage->CopyContentFrom()->Highlighted;
$arrayList->push($virtualPage);
}
}
$arrayList = $arrayList->sort('Highlighted DESC');
return $arrayList;
}
I'm not very proud of any of these solutions, but I believe they do fit your criteria.
Here's what I ended up doing, which I think works:
/**
* #return ArrayList
*/
public function VirtualPages()
{
$result = [];
$virtualPages = $this->RelatedVirtualPages();
$contentPages = ContentPage::get()
->byIDs($virtualPages->column('CopyContentFromID'))
->map('ID', 'Highlighted')
->toArray();
foreach($virtualPages as $virtualPage) {
$highlighted = $contentPages[$virtualPage->CopyContentFromID];
$virtualPage->Highlighted = $highlighted;
$result[] = $virtualPage;
}
return ArrayList::create(
$result
);
}
And then it's sortable like so:
$areaPage->VirtualPages()->sort('Highlighted DESC');
Thank you for all the answers and pointers. I'll wait a bit before marking any answer.
Couldn't you just do
//just get one areapage
$AreaPageItem = AreaPage::get()->First();
//now get the RelatedVirtualPages sorted
$related_pages = $AreaPageItem->RelatedVirtualPages()->sort("Highlighted","ASC");

Doctrine 2 - Association table with additional properties & inheritance

I'm just starting with Doctrine (no experience with ORMs so far) on my first Symfony2 project.
I have a pretty simple structure, but am not satisfied with how classes are generated.I looked for tutorials/related questions on stackoverflow, but I haven't found a comprehensive answer so far :
Let say I have 2 entities + an association table with additional properties :
User
- id: int
- name: string
- movies: UserMovie
Movie
- id: int
- name: string
- duration: int
UserMovie:
- user_id: int
- movie_id: int
- seen: bool
A user can have several movies / a movie can be owned by several users.
What I would like is to have the generated UserMovie class inheriting from Movie, so I can access movies properties from a UserMovie instance directly.
I'd like to get something like this as a result of the generation process:
class UserMovie extends Movie
{
protected $user; // User instance
protected $id; // inherited from Movie
protected $name; // inherited from Movie
protected $duration; // inherited from Movie
}
Is this even possible?
Is there some best practices in this case?
Not sure this is clear enough (as I said earlier, I'm pretty new to this), any help would be gladly appreciated :)
It is not clear enough :)
You should build
User hasMany UserMovies
Movie hasMany UserMovies
relations. That will allow you to do something like this
var_dump( $user->hasMovie($movie) ) ;
by doing
class User
{
public function hasMovie(Movie $movie)
{
foreach($this->usermovies as $m2m) {
if ( $m2m->getMovie() === $movie )
return true ;
}
return false ;
}
public function addMovie(Movie $movie)
{
if ( !$this->hasMovie($movie) ) {
$m2m = new UserMovie() ;
$m2m->setMovie($movie) ;
$m2m->setUser($this) ;
$this->usermovies->add($m2m) ;
}
}
}
or alike

Doctrine OneToMany with aggregate field, how to keep it up to date?

In doctrine2 I have a OneToMany association: One Application <=> Many ApplicationCost
// Application.php
/**
* #ORM\OneToMany(targetEntity="ApplicationCost", mappedBy="application", orphanRemoval=true)
*/
protected $costs;
// ApplicationCost.php
/**
* #ORM\ManyToOne(targetEntity="Application", inversedBy="costs")
* #ORM\JoinColumn(name="application_id", referencedColumnName="id")
*/
protected $application;
In Application entity I have an agregate field sumCosts:
/**
* #ORM\Column(type="decimal", scale=2)
*/
protected $sumCosts;
Which is updated when addCost and removeCost are called:
// Application.php
public function addCost(ApplicationCost $cost)
{
if (!$this->costs->contains($cost)) {
$this->sumCosts += $cost->getBalance();
$this->costs[] = $cost;
$cost->setApplication($this);
}
return $this;
}
public function removeCost(ApplicationCost $cost)
{
if ($this->costs->contains($cost)) {
$this->sumCosts -= $cost->getBalance();
$this->costs->removeElement($cost);
}
}
Assuming User can edit already existing ApplicationCost's and can change it's parent Application, how do I make sure that this agregate field is up to date?
My approach is:
// ApplicationCost.php
public function setApplication(Application $application = null)
{
if ($this->application !== null) {
$this->application->removeCost($this);
}
if ($application !== null) {
$application->addCost($this);
}
$this->application = $application;
return $this;
}
Is that good? Or am I makeing here some huge mistake here and sumCosts may be out of sync?
EDIT: I've read Doctrine's Aggregate Fields cookbook and I have the versioning (and I use locking mechanism). My question is not about concurrency.
EDIT: I've created some tests
public function testSumCosts()
{
$app = new Application();
$costA = new ApplicationCost();
$costA->setBalance(150);
$costB = new ApplicationCost();
$costB->setBalance(100);
$costC = new ApplicationCost();
$costC->setBalance(50);
$app->addCost($costA);
$app->addCost($costB);
$app->addCost($costC);
$app->removeCost($costC);
$this->assertEquals(250, $app->sumCosts(), 'Costs are summed correctly');
}
public function testCostsChangeApplication()
{
$appA = new Application();
$appB = new Application();
$costA = new ApplicationCost();
$costA->setBalance(100);
$costB = new ApplicationCost();
$costB->setBalance(50);
$appA->addCost($costA);
$appB->addCost($costB);
$costA->setApplication($appB);
$costB->setApplication(null);
$this->assertEquals(0, $appA->sumCosts(), 'Costs are removed correctly');
$this->assertEquals(100, $appB->sumCosts(), 'Costs are added correctly');
}
And after adding $cost->setApplication($this); to addEntry both tests are green. Though I still wonder if I might have missed something.
Okay, I think I finally achieved desired result. I'll describe it for future reference and anyone who might have the same problem:
First of all correct the class
// Application.php
public function addCost(ApplicationCost $cost)
{
if (!$this->costs->contains($cost)) {
$this->sumCosts += $cost->getBalance();
}
$this->costs[] = $cost;
return $this;
}
public function removeCost(ApplicationCost $cost)
{
if ($this->costs->contains($cost)) {
$this->sumCosts -= $cost->getBalance();
}
$this->costs->removeElement($cost);
}
If you compare this to my original code you'll see that only updateing the agregate field is under condition. It does not hurt as collections can't hold duplicate elements and can't remove non existing elements.
Second of all, configure the cascade={all} option on inverse side of association (that is, on costs inside Application.php). So whenever you add/remove costs they are persisted too.
to be continued... (have to test what happens when i change application it from the owning side and persist only ApplicationCost -> will both old and new Application be updated?)

Symfony2 data transformer, validator and error message

I asked this question and found out that we can't get the error message thrown by a DataTransformer (according to the only user who answered, maybe it's possible, I don't know).
Anyway, now that I know that, I am stucked with a problem of validation. Suppose my model is this one: I have threads that contains several participants (users).
<?php
class Thread
{
/**
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToMany(targetEntity="My\UserBundle\Entity\User")
* #ORM\JoinTable(name="messaging_thread_user")
*/
private $participants;
// other fields, getters, setters, etc
}
For thread creation, I want the user to specify the participants usernames in a textarea, separated by "\n".
And I want that if one or more of the usernames specified don't exist, a message is displayed with the usernames that don't exist.
For example, "Users titi, tata and toto don't exist".
For that I created a DataTransformer that transforms the raw text in the textarea into an ArrayCollection containing instances of users. Since I can't get the error message provided by this DataTransformer (such a shame! Is it really impossible?), I don't check the existence of each usernames in the DataTransformer but in the Validator.
Here is the DataTransformer that converts \n-separated user list into an ArrayCollection (so that the DataBinding is ok):
<?php
public function reverseTransform($val)
{
if (empty($val)) {
return null;
}
$return = new ArrayCollection();
// Extract usernames in an array from the raw text
$val = str_replace("\r\n", "\n", trim($val));
$usernames = explode("\n", $val);
array_map('trim', $usernames);
foreach ($usernames as $username) {
$user = new User();
$user->setUsername($username);
if (!$return->contains($user)) {
$return->add($user);
}
}
return $return;
}
And here is my validator:
<?php
public function isValid($value, Constraint $constraint)
{
$repo = $this->em->getRepository('MyUserBundle:User');
$notValidUsernames = array();
foreach ($value as $user) {
$username = $user->getUsername();
if (!($user = $repo->findOneByUsername($username))) {
$notValidUsernames[] = $username;
}
}
if (count($notValidUsernames) == 0) {
return true;
}
// At least one username is not ok here
// Create the list of usernames separated by commas
$list = '';
$i = 1;
foreach ($notValidUsernames as $username) {
if ($i < count($notValidUsernames)) {
$list .= $username;
if ($i < count($notValidUsernames) - 1) {
$list .= ', ';
}
}
$i++;
}
$this->setMessage(
$this->translator->transChoice(
'form.error.participant_not_found',
count($notValidUsernames),
array(
'%usernames%' => $list,
'%last_username%' => end($notValidUsernames)
)
)
);
return false;
}
This current implementation looks ugly. I can see the error message well, but the users in the ArrayCollection returned by the DataTransformer are not synchronized with Doctrine.
I got two questions:
Is there any way that my validator could modify the value given in parameter? So that I can replace the simple User instances in the ArrayCollection returned by the DataTransformer into instances retrieved from the database?
Is there a simple and elegant way to do what I'm doing?
I guess the most simple way to do this is to be able to get the error message given by the DataTransformer. In the cookbook, they throw this exception: throw new TransformationFailedException(sprintf('An issue with number %s does not exist!', $val));, if I could put the list of non-existing usernames in the error message, it would be cool.
Thanks!
I am the one that answered your previous thread so maybe someone else will jump in here.
Your code can be simplified considerably. You are only dealing with user names. No need for use objects or array collections.
public function reverseTransform($val)
{
if (empty($val)) { return null; }
// Extract usernames in an array from the raw text
// $val = str_replace("\r\n", "\n", trim($val));
$usernames = explode("\n", $val);
array_map('trim', $usernames);
// No real need to check for dups here
return $usernames;
}
The validator:
public function isValid($userNames, Constraint $constraint)
{
$repo = $this->em->getRepository('SkepinUserBundle:User');
$notValidUsernames = array();
foreach ($userNames as $userName)
{
if (!($user = $repo->findOneByUsername($username)))
{
$notValidUsernames[$userName] = $userName; // Takes care of dups
}
}
if (count($notValidUsernames) == 0) {
return true;
}
// At least one username is not ok here
$invalidNames = implode(' ,',$notValidUsernames);
$this->setMessage(
$this->translator->transChoice(
'form.error.participant_not_found',
count($notValidUsernames),
array(
'%usernames%' => $invalidNames,
'%last_username%' => end($notValidUsernames)
)
)
);
return false;
}
=========================================================================
So at this point
We have used transformer to copy the data from the text area and generated an array of user names during form->bind().
We then used a validator to confirm that each user name actually exists in the database. If there are any that don't then we generate an error message and form->isValid() will fail.
So now we are back in the controller, we know we have a list of valid user names (possibly comma delimited or possibly just an array). Now we want to add these to our thread object.
One way would to create a thread manager service and add this functionality to it. So in the controller we might have:
$threadManager = $this->get('thread.manager');
$threadManager->addUsersToThread($thread,$users);
For the thread manager we would inject our entity manager. In the add users method we would get a reference to each of the users, verify that the thread does not already have a link to this user, call $thread->addUser() and then flush.
The fact that we have wrapped up this sort of functionality into a service class will make things easier to test as we can also make a command object and run this from the command line. it also gives us a nice spot to add additional thread related functionality. We might even consider injecting this manager into the user name validator and moving some of the isValid code to the manager.

Resources