One to many selected items - symfony

I have 2 entities:
Teams (id, name, short_name...)
TeamMembers (id, teams_id, player_id)
In admin, I have multiselect field, and on submit it sends ids array. How can I save it in related table?
Here is a part of code that I have in TeamsController
/**
* #param array<string, mixed> $data
*/
protected function mapDataToEntity(array $data, Teams $entity): void
{
$image = null;
if ($imageId = ($data['logo']['id'] ?? null))
{
$image = $this->mediaRepository->findMediaById($imageId);
}
$entity->setLogo($image);
$entity->setName($data['name']);
$entity->setCountry($data['country']);
$entity->setGender($data['gender']);
$entity->setShortName($data['shortName']);
$entity->setPlayers($data['players']); // <-- how do I handle THIS?
}
P.S. Also, I need to handle deletion...
Thanks in advance...

You need to load the entities you need to map them to your entity.
At the best you have here a look at the doctrine/orm documentation about association mapping: https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/association-mapping.html

You just have to load the entities as you load the image.
$entity->setPlayers($this->playerRepository->findById($data['players']));

Related

Add distinct values to array collection

Within my application I am sending out notifications to different users, which are assigned to agencies, which are assigned to documents. So whenever a document gets created and an agency is assigned to that document, all the users belonging to that agency should get a notification. The problem: it may happen, that an user is assigned to multiple agencies so whenever the notifications get sent out and all his agencies are assigned to the document, he would get notified multiple times. I'd like to avoid this but I can't figure out how to add only distinct objects to my array collection since it doesn't seem like there's something like the array_unique function.
So far it looks like that:
foreach($document->getAgencies() as $agency) {
if(count($agency->getUseragencies()) > 0){
$users = $agency->getUseragencies();
foreach ($users as $user){
... notifications are generated
$manager->addNotification($user, $notif);
}
}
}
Any help would be appreciated!
oh and background info: Agency is an own entity, as well as User is and they are in a many to many relationship!
edit
mapping infos:
in entity Agency:
/**
* #ORM\ManyToMany(targetEntity="UserBundle\Entity\User", mappedBy="agencies")
**/
private $useragencies;
in entity User
/**
* #ORM\ManyToMany(targetEntity="AppBundle\Entity\Agency", inversedBy="useragencies", cascade={"persist"})
* #ORM\JoinTable(name="user_user_agencies",
* joinColumns={#ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={#ORM\JoinColumn(name="iata8", referencedColumnName="iata8")})
* #var \AppBundle\Entity\Agency
**/
private $agencies;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Notification", mappedBy="user", orphanRemoval=true, cascade={"persist"})
**/
private $notifications;
ArrayCollection has a method contains that you could use - link.
I assume your Agency entity has also bidirectional mapping with Document entity i guess as ManyToMany. To get the users whom you want to send the notification which belongs to multiple agencies and an agency can have many documents you can use below DQL to get distinct users by providing the document id if you have any other property to identify the document you can adjust WHERE clause accordingly
SELECT DISTINCT u
FROM YourBundle:User u
JOIN u.agencies a
JOIN a.documents d
WHERE d.id = :documentid
Using query builder it would be something like
$user = $em->getRepository('YourBundle:User');
$user = $user->createQueryBuilder('u')
->select('u')
->join('u.agencies','a')
->join('a.documents','d')
->where('d.id = :documentid')
->setParameter('documentid', $documentid)
->distinct()
->getQuery();
$users= $user->getResult();
While you cannot use in_array() on an ArrayCollection, you'll have to build your own array of unique users.
$users = array();
foreach($document->getAgencies() as $agency) {
if(count($agency->getUseragencies()) > 0) {
foreach ($agency->getUseragencies()as $user) {
// ... notifications are generated
if(!in_array($user->getId(), $users)) {
$users[] = $user->getId();
}
}
foreach($users as $userId) {
$manager->addNotification($userId, $notif);
}
}
}
or a simpler, lower-cost version:
$sent = array();
foreach($document->getAgencies() as $agency) {
if(count($agency->getUseragencies()) > 0) {
foreach ($agency->getUseragencies()as $user) {
// ... notifications are generated
if(!in_array($user->getId(), $sent)) { // check if user has already been sent the notification
$manager->addNotification($user, $notif); // send the notification
$sent[] = $user->getId(); // add user to the 'sent' list
}
}
}
}
Alternatively, you could save yourself a lot of trouble by writing a custom DQL Query (possibly in your UserRepository class) to fetch the list of user from database directly. This would remove a lot of complexity in the code by removing the need for a loop altogether.
You need to add a condition to handle values already in the array. Try adding something like the condition below.
foreach($user as $user){
if(!in_array($value, $list, true))

Update a field after Linking / Unlinking Many-Many records in SilverStripe

I have created a Customer DataObject by extending Member. Customer has a many_many data relation with a Package DataObject.
I would like increment/decrement a Credits field in the Customer DataObject when a Package is linked / unlinked through the CMS based on the Limit field in the Package table.
Customer
class Customer extends Member {
private static $db = array(
'Gender' => 'Varchar(2)',
'DateOfBirth' => 'Date',
'Featured' => 'Boolean',
'Credits' => 'Int'
);
private static $many_many = array(
'Packages' => 'Package'
);
public function getCMSFields() {
$fields = new FieldList();
$config = GridFieldConfig_RelationEditor::create();
$config->removeComponentsByType('GridFieldAddNewButton');
$packageField = new GridField(
'Packages',
'Package',
$this->Packages(),
$config
);
$fields->addFieldToTab('Root.Package', $packageField);
Session::set('SingleID', $this->ID);
$this->extend('updateCMSFields', $fields);
return $fields;
}
}
Package
class Package extends DataObject {
private static $db = array(
'Title' => 'Varchar(255)',
'Limit' => 'Int'
);
private static $belongs_many_many = array(
'Customers' => 'Customer'
);
}
When you create or delete many to many relationship just one record is modified in your database - the one in table which joins elements of both sides of the relationship. Therefore neither object the relationship is based on is updated. This is why methods like: onBeforeWrite, onAfterWrite, onBeforeDelete and onAfterDelete will not be called at all and you cannot use them to detect such change.
However, Silverstripe provides class ManyManyList which is responsible for all operations connected to many to many relationships. There are two methods which are of your interest: add and remove. You can override them and put inside action to do what you need. These methods are obviously called on each link or unlink operation no matter object types are, so you should make some filtering on classes you are particularly interested in.
The proper way to override the ManyManyList class is to use Injector mechanism, so as not to modify anything inside the framework or cms folder. The example below uses relationship between Members and Groups in Silverstripe but you can easily adopt it to your need (Customer -> Member; Package -> Group).
app.yml
Injector:
ManyManyList:
class: ManyManyListExtended
ManyManyListExtended.php
/**
* When adding or removing elements on a many to many relationship
* neither side of the relationship is updated (written or deleted).
* SilverStripe does not provide any built-in actions to get information
* that such event occurs. This is why this class is created.
*
* When it is uses together with SilverStripe Injector mechanism it can provide
* additional actions to run on many-to-many relations (see: class ManyManyList).
*/
class ManyManyListExtended extends ManyManyList {
/**
* Overwritten method for adding new element to many-to-many relationship.
*
* This is called for all many-to-many relationships combinations.
* 'joinTable' field is used to make actions on specific relation only.
*
* #param mixed $item
* #param null $extraFields
* #throws Exception
*/
public function add($item, $extraFields = null) {
parent::add($item, $extraFields);
if ($this->isGroupMembershipChange()) {
$memberID = $this->getMembershipID($item, 'MemberID');
$groupID = $this->getMembershipID($item, 'GroupID');
SS_Log::log("Member ($memberID) added to Group ($groupID)", SS_Log::INFO);
// ... put some additional actions here
}
}
/**
* Overwritten method for removing item from many-to-many relationship.
*
* This is called for all many-to-many relationships combinations.
* 'joinTable' field is used to make actions on specific relation only.
*
* #param DataObject $item
*/
public function remove($item) {
parent::remove($item);
if ($this->isGroupMembershipChange()) {
$memberID = $this->getMembershipID($item, 'MemberID');
$groupID = $this->getMembershipID($item, 'GroupID');
SS_Log::log("Member ($memberID) removed from Group ($groupID)", SS_Log::INFO);
// ... put some additional actions here
}
}
/**
* Check if relationship is of Group-Member type.
*
* #return bool
*/
private function isGroupMembershipChange() {
return $this->getJoinTable() === 'Group_Members';
}
/**
* Get the actual ID for many-to-many relationship part - local or foreign key value.
*
* This works both ways: make action on a Member being element of a Group OR
* make action on a Group being part of a Member.
*
* #param DataObject|int $item
* #param string $keyName
* #return bool|null
*/
private function getMembershipID($item, $keyName) {
if ($this->getLocalKey() === $keyName)
return is_object($item) ? $item->ID : $item;
if ($this->getForeignKey() === $keyName)
return $this->getForeignID();
return false;
}
}
The solution provided by 3dgoo should also work fine but IMO that code does much more "hacking" and that's why it is much less maintainable. It demands more modifications (in both classes) and needs to be multiplied if you would like to do any additional link/unlink managing, like adding custom admin module or some forms.
The problem is when adding or removing items on a many to many relationship neither side of the relationship is written. Therefore onAfterWrite and onBeforeWrite is not called on either object.
I've come across this problem before. The solution I used isn't great but it was the only thing that worked for me.
What we can do is set an ID list of Packages to a session variable when getCMSFields is called. Then when an item is added or removed on the grid field we refresh the CMS panel to call getCMSFields again. We then retrieve the previous list and compare it to the current list. If the lists are different we can do something.
Customer
class Customer extends Member {
// ...
public function getCMSFields() {
// Some JavaScript to reload the panel each time a package is added or removed
Requirements::javascript('/mysite/javascript/cms-customer.js');
// This is the code block that saves the package id list and checks if any changes have been made
if ($this->ID) {
if (Session::get($this->ID . 'CustomerPackages')) {
$initialCustomerPackages = json_decode(Session::get($this->ID . 'CustomerPackages'), true);
$currentCustomerPackages = $this->Packages()->getIDList();
// Check if the package list has changed
if($initialCustomerPackages != $currentCustomerPackages) {
// In here is where you put your code to do what you need
}
}
Session::set($this->ID . 'CustomerPackages', json_encode($this->Packages()->getIDList()));
}
$fields = parent::getCMSFields();
$config = GridFieldConfig_RelationEditor::create();
$config->removeComponentsByType('GridFieldAddNewButton');
$packageField = GridField::create(
'Packages',
'Package',
$this->Packages(),
$config
);
// This class needs to be added so our javascript gets called
$packageField->addExtraClass('refresh-on-reload');
$fields->addFieldToTab('Root.Package', $packageField);
Session::set('SingleID', $this->ID);
$this->extend('updateCMSFields', $fields);
return $fields;
}
}
The if ($this->ID) { ... } code block is where all our session code happens. Also note we add a class to our grid field so our JavaScript refresh works $packageField->addExtraClass('refresh-on-reload');
As mentioned before, we need to add some JavaScript to reload the panel each time a package is added or removed from the list.
cms-customer.js
(function($) {
$.entwine('ss', function($){
$('.ss-gridfield.refresh-on-reload').entwine({
reload: function(e) {
this._super(e);
$('.cms-content').addClass('loading');
$('.cms-container').loadPanel(location.href, null, null, true);
}
});
});
})(jQuery);
Inside the if($initialCustomerPackages != $currentCustomerPackages) { ... } code block there are a number of things you can do.
You could use $this->Packages() to fetch all the current packages associated to this customer.
You could call array_diff and array_merge to get just the packages that have been added and removed:
$changedPackageIDs = array_merge(array_diff($initialCustomerPackages, $currentCustomerPackages), array_diff($currentCustomerPackages, $initialCustomerPackages));
$changedPackages = Package::get()->byIDs($changedPackageIDs);
The above code will add this functionality to the Customer side of the relationship. If you also want to manage the many to many relationship on the Package side of the relationship you will need to add similar code to the Package getCMSFields function.
Hopefully someone can come up with a nicer solution. If not, I hope this works for you.
note: Didn't actually check does the model work but by visually checking this should help you:
On the link you provided you are using
$customer = Customer::get()->Filter...
That returns a DataList of objects, not a singular object unless you specify what is the object you want from the DataList.
If you are filtering the Customers then you want to get a SPECIFIC customer from the DataList, e.g. the first one in this case.
$customer = Customer::get()->filter(array('ID' => $this->CustomerID))->first();
But You should be able to get the singular DataObject with:
$customer = $this->Customer();
As you are defining the Customer as "has_one". If the relation was a Has many, using () would get you a DataList of objects.
Protip:
You don't need to write our own debug files in SilverStripe. It has own functions for it. For example Debug::log("yay"); what writes the output to a file and Debug::dump("yay") that dumps it directly out.
Tip is that you can check what is the object that you accessing right. Debug::dump(get_class($customer)); would output only the class of the object.

Symfony2: How to create custom query methods on related entities

I have a User entity which has an ArrayCollection of Positions. Each Position has for sure a user_id property.
Now i want to get all positions from a user (to get all i would do $user->getPositions()) that are matching a specific query, for example have a date property that matches the current date. Therefor i want to do something like $user->getCurrentPositions() and it should return a subset of the positions related to that user.
How is that possible?
EDIT:
What i really wanna do is something like this in my controller:
$em = $this->getDoctrine()->getManager();
$users = $em->getRepository('fabianbartschWhereMyNomadsAtBundle:User')->findAll();
foreach ($users as $user) {
$positions = $user->getCurrentPositions();
foreach ($positions as $position) {
echo $position->getLatitude().'<br>';
}
}
I wanna iterate over all users and from each user i want to have the relevant positions. But that isnt possible from the repository i guess, as i get the following message: Attempted to call method "getCurrentPositions" on class ...
If you are using Doctrine you can use the built-in Criteria API which is meant for this purpose exactly.
Collections have a filtering API that allows you to slice parts of data from a collection. If the collection has not been loaded from the database yet, the filtering API can work on the SQL level to make optimized access to large collections.
Ok i found out, its for sure possible with Repositories:
Entity\User.php
/**
* #ORM\Entity(repositoryClass="fabianbartsch\WhereMyNomadsAtBundle\Entity\UserRepository")
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
Entity\UserRepository.php
/**
* UserRepository
*/
class UserRepository extends EntityRepository
{
public function getCurrentPositions()
{
$query = $this->getEntityManager()
->createQuery(
"SELECT p
FROM xxx:Position p
WHERE p.start <= '2014-08-17' AND p.end >= '2014-08-17'"
);
try {
return $query->getResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return null;
}
}
}
In the user object only related position entries are affected by the query, so is no need to join user entity with the position entity. Pretty simple, should just try out instead posting on stackoverflow, sry guys :P

Symfony2 remove and save many to many relations

I need your help today. I'm working on a small application using Symfony 2.1 but I have a base problem, I have to tables with a many to many relation which creates a third table:
class Usuario implements UserInterface {
/**
* #ORM\ManyToMany(targetEntity="Alood\BackBundle\Entity\Alergeno", inversedBy="usuarios")
* #ORM\JoinTable(name="UsuariosProductos",
* joinColumns={#ORM\JoinColumn(name="usuario_user", referencedColumnName="user")},
* inverseJoinColumns={#ORM\JoinColumn(name="alergeno_id", referencedColumnName="id")}
* )
**/
protected $alergenos;
}
public function __construct(){
$this->alergenos = new ArrayCollection();
}
public function getAlergenos() { return $this->alergenos; }
and:
/**
* #ORM\ManyToMany(targetEntity="Alood\BackBundle\Entity\Usuario", mappedBy="alergenos")
**/
protected $usuarios;
Then I need to remove the non selected Alergenos, this is my controller:
$alergenosUser = $em->getRepository("BackBundle:Usuario")->find($usuario);
$resultSym = array_diff($alergenosUsuarioIds, $alergen);
foreach($resultSym as $result) {
$alergenosUser->getAlergenos()->remove($result);
}
$em->persist($alergenosUser);
$em->flush();
Could you please help me to figure out what I'm doing wrong? Thanks you so much!
In order to remove an item from a collection use the following:
$collection->removeElement($item);
The remove($key) function will remove by key while removeElement($item) removes the item from the collection if found. Have a look at the ArrayCollection code here.
Be aware that doctrine will only check the owning side of a relation for changes.
It is not clear what the $alergenosUsuarioIds and $alergen variables represent but you might be mistaken about the usage of the remove() method of ArrayCollection.
You need to give it an index, not the id of the entity you want to remove. You can also use the removeElement() method and pass it the entity.
For instance you can do something like this :
$elements = $alergenosUser->getAlergenos();
foreach ($elements as $element) {
if ($element->getId() == $id_from_array_diff_or_whatever) {
$elements->removeElement($element);
}
}
or
$elements = $alergenosUser->getAlergenos();
foreach ($elements as $key => $element) {
if ($element->getId() == $id_from_array_diff_or_whatever) {
$elements->remove($key);
// or
unset($elements[$key]);
}
}
You can also use the matching() but I'm not sure it's available with the version shipped with symfony2 2.1.
So your problem can be solved doing the relation yourself.
ManyToMany doesn't really exist because as you say a third table is created. You want to delete elements only in this third table.
So you have to build the relation yourself to delete directly an element in the third table.
So first create the third entity.
Do two relation ManyToOne from the third entity to the two others entities.
Then you just have to simply remove an element of the third entity you just created.

Symfony Entity check for ArrayCollection if already exists, then do update

For a Collection, I want to check if a new added Part already exists in the database. And if so, that it'll be overwritten with the new value.
/**
* Add Part
*/
public function addPart(\MyBundle\Entity\FooTypePart $Part)
{
$part->setProduct($this);
$this->part[] = $part;
return $this;
}
/**
*/
public function removePart(\MyBundle\Entity\FooTypePart $part)
{
$this->part->removeElement($part);
}
/**
* Get Part
* #return \Doctrine\Common\Collections\Collection
*/
public function getPart()
{
return $this->part;
}
/**
* Set Part
*/
public function setPart($part)
{
$this->part = $part;
return $this;
}
The Part Entity has: ID, Category_id (FK), Product_id (FK), Part (Collection)
It is possible at the moment to add a new Part with the same name, also when there is already a Part with the same Product_id AND Category_id.
Making Part unique isn't the fix, because Part can be used for many Products/Categories.
The following example already exists in the database, with a different 'Part'. So it should do a update command.
<?php
$part = new FooTypePart();
$part->setCategory($specification);
$part->setProduct($product);
$part->setPart('DifferentNamingThenCurrentOne');
$xx->addSpecificationValue($part);
How? :-)
Just use the UniqueEntity validator to find an already existant Item in the collection.
You can specify uniqueness using multiple properties or a repository method. This way you can search for items only having a unique combination of name, product-id and category-id.
Create a validation group i.e. "unique" and find the non-unique/existant entity in your collection by looking for invalid entities.
... then update the existing entity with your new value. You will probably need some extra logic because there might be multiple fields with the same name added to your form collection.

Resources