How can every user have his private doctrine entries in a symfony2? - php

I am creating an application with guests, venues, events and guest-categories in symfony2 with doctrine. Being in touch for 3 weeks only with symfony2, I have accomplished to create the schema tables for all entities in sql, their between dependencies and crud for each one. My problem is more theoretical than practical.
I will create a bundle for security (from the Security bundle in the book or FOSUserBundle). Then I will set up three roles:
Administrator
Organiser
Guest
Each guest will have its own profile and each organiser will be "attached" to his guests - in simple words, the Organizer X will only communicate and view Guest X-1, X-2 etc. - he won't be able to view Y-1. It might seem very simple and funny to you that I am asking a question like that but... I am self taught :)
The question is: How can I attach an event to an organiser and its guests? The simplest method I can imagine is : set a unique token (or any self generated code) for every guest, event, category, event and join the tables.
If guest John with token=vsfv2435r3frwf24t5grf has any events/categories/venues
find the same token in the event or category or venue and assign permissions.
Does it sound logical or foolish ?

You need to look at doctrine association mapping (one to many, many to many, etc.. )
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html
They are quite easy to set up (you can do it in Symfony with a annotations or in your yml schema file) and will let you do the kind of things you are describing easily, take a look at the Symfony documentation for databases for more info
http://symfony.com/doc/current/book/doctrine.html

You could just create cross table for Organiser and Guest indicating which event they do attend / organise. Each Guest would also have have cross table to Organiser. This way you could easily assign organisers to guests and filter by it (I'm assuming guests could attend multiple events etc).
In Doctrine world it'd look like this:
Event
...
/**
* #ORM\ManyToMany(targetEntity="Organiser", inversedBy="events")
*/
protected $organisers;
/**
* #ORM\ManyToMany(targetEntity="Guest", inversedBy="events")
*/
protected $guests;
Organiser
...
/**
* #ORM\ManyToMany(targetEntity="Event", mappedBy="organisers")
*/
protected $events;
/**
* #ORM\ManyToMany(targetEntity="Guest", mappedBy="organisers")
*/
protected $guests;
Guest
...
/**
* #ORM\ManyToMany(targetEntity="Event", mappedBy="guests")
*/
protected $events;
/**
* #ORM\ManyToMany(targetEntity="Organiser", inversedBy="guests")
*/
protected $organisers;

Related

Doctrine & Symfony3 OneToOne relations, inverse side does not exist

I am looking for a clear explanation of how the relations work in Doctrine 2. I've been trying now for days to set up a OneToOne relation in Symfony3, and have read basically every thread and all the documentation, and i simply do not understand. The last few hours i've been bruteforcing my way into a OneToOne relation, with no luck.
Here's the problem i'm dealing with:
My user entity has a Team (which is itself an Entity)
/**
* #ORM\OneToOne(targetEntity="Gluckez\bballBundle\Entity\Team",
mappedBy="Owner")
* #ORM\Column(name="team_id")
*/
private $team;
And My Team has an Owner, which refers to the User:
/**
* #var User $Owner
* #ORM\OneToOne(targetEntity="Application\Sonata\UserBundle\Entity\User",
inversedBy="team_id")
* #ORM\JoinColumn(name="Owner_id", referencedColumnName="id")
*/
protected $Owner;
Now in my User table, i can see the relations, which do point to the right team. but doctrine complains about the inverse side User#team, that does not exist (even though i'm pretty sure i've defined it?)
I've tried countless times to change the inversedby, mappedby, name, referencedcolumnName, as well as dropping and recreating the database and tables. updating the doctrine schema's. i'm out of idea's.
Not a single answer out there, nor the documentation of doctrine points me in the right direction. I realize that there's a lot of threads out there on this, but none of them are clear, or even answered.
Try it like this:
Owner entity
/**
* #ORM\OneToOne(targetEntity="Gluckez\bballBundle\Entity\Team", mappedBy="owner")
*/
private $team;
Team entity
/**
* #ORM\OneToOne(targetEntity="Application\Sonata\UserBundle\Entity\User", inversedBy="team")
*/
protected $owner;
As reference: Association Mapping

Symfony2 - Sharing users between multiple applications

I am currently searching for a way to share users between multiple Symfony2 applications for one of our customers. In the past 3 years, we made a few applications (4) for them that have different purposes, but always use the "same" user model and data.
Currently, we have 4 separate databases, where the "users" table is kinda the same on all the applications, except for the many-to-many relationships. At first, I was thinking about adding a second entity manager (and connection), and putting the users in a separate database. All the applications would be able to use this and all the users would have the same credentials. But how do I handle the many-to-many relationships?
To give an example, on application A you have a many-to-many relation from "Users" to "Clients", but the "Clients" table doesn't exist in application B/C/D. On application B, you have a many-to-many relation from "Users" to "Suppliers", but the "Suppliers" table doesn't exist in application A/C/D and so on. Moving the "Clients" or "Suppliers" table to the shared database isn't really an option either, because other entities (which are not shared) are also having relations to those tables.
I basically need to find a way to map many-to-many relationships on the "shared user" model/database which are unique for each application. Is there a way to achieve this with multiple databases? Should I go for some other approach?
All info is welcome. Thanks in advance.
Option 1
Using different connections, this doesn't seem to be possible with Doctrine out of the box .
Similar questions have been answered already:
Using Relationships with Multiple Entity Managers
Entities associations across different managers
As stated in the first answer, you could do the following:
keep the object graphs disconnected by saving the identifiers of the related objects (old style) instead of a reference to them, then manually get the objects through services.
But if your want Doctrine to actually be aware of the associations, you need to persist the associated entities in the same database, or your mapping will just generate errors. That means you would need to duplicate the User entity.
Option 2
In the very specific case where you can use the same connection (that is, with multiple databases of the same DBMS on the same host and with the same user), there seems to be a way, but I haven't tested it:
https://techpunch.co.uk/development/using-multiple-databases-with-symfony2-and-doctrine2
The idea is to prefix each table with the database name, as if it were a schema name, like this:
This entity is mapped to the «User» table in the database «users»:
<?php
namespace Demo\UserBundle\Entity;
use DoctrineORMMapping as ORM;
/**
* #ORMTable(name="users.User")
*/
class User
{
/* ... */
}
This one is mapped to the «Post» table in the database «posts»:
<?php
namespace Demo\PostBundle\Entity;
use DoctrineORMMapping as ORM;
/**
* #ORMTable(name="posts.Post")
*/
class Post
{
/* ... */
}
Then you can make associations as usual:
class Post
{
/**
* #ORM\ManyToOne(targetEntity="\Demo\UserBundle\Entity\User")
**/
private $user;
/* ... */
}
The author also links to an example project on github:
https://github.com/lobsterdore/symfony2-multiple-db-example

Auto database query from User entity in Symfony2

In Symfony2, I have these columns in a user entity:
*
* #ORM\OneToOne(targetEntity="User", inversedBy="lovername")
* #ORM\JoinColumn(name="lover", referencedColumnName="id", onDelete = "SET NULL")
*/
private $lover;
/**
* #ORM\OneToOne(targetEntity="User", mappedBy="lover", cascade={"persist"}, fetch="EXTRA_LAZY")
**/
private $lovername;
The project is a dating website, where a user can send messages only to one user, which he/she chose. The column lover contains the id of user he/she chose to talk to.
It works properly, but it generates an additional SQL query. First Symfony downloads the row about the logged user and then every information about user which 'love' him, even if that information is useless. I need it only when user sends messages.
It is some way to write it/build database better? How do I control auto-generating queries in Symfony2?
I've also faced a similar problem. It seems like LazyLoad doesn't work properly for OneToOne relationships in doctrine. See this question

Reference Many sonata admin Bundle

In my project I'm using two documents : User and Quizz. In fact I have Unidirectional relation between this two documents ( the user references one or more Quizzs).
In my Back end I'm using Sonata mongoDb Amin Bundle, And when i'm creating a User I want to add a Quizz to this User. It appears in the form, I add the Quizz , the quizz is created but there is no reference in the User Document.
User Document :
/**
* #var ArrayCollection
* #MongoDB\ReferenceMany(targetDocument="\ATS\QuizzBundle\Document\Quizz", cascade={"all"})
*/
protected $quizz = array();
And here is the UserAdmin :
->add('quizz', 'sonata_type_collection',array('by_reference' => false),
array('admin_code' => 'sonata.admin.quizz')) ;
I'm wondering where is the origin of this problem, any one can help please? thank you

Doctrine2: association mapping for a table with multiple foreign keys

A friend of mine and me are working on a multilingual web-application for storing recipes. We use Doctrine 2.1.1 for persistence. At the moment I stuck generating models for an entity and hope someone can give me a hint, or show up a best practice for our case.
I'll try to explain our ERD broadly. Feel free to ask, if there is something unintelligible. Our application has the Entities recipe, ingredients, units (measure units for ingredients) and categories. Each of these entities is stored in an own table. Each entity can be translated in multiple languages. Storage of the translations is intended in a table called (you name it) translations.
Now it will be a bit tricky… we have an extra table called mnemonic. We use it for identifying recipes, ingredients, categories and units of measure in a global context… best analogy to this is a GUID I think. The mnemonic also helps us to map between recipes, ingredients etc. and their translations.
The mnemonic table consists of five rows: id (the primary key) and four additional meta-data rows: ingredient_id, recipe_id, category_id and unit_id referencing a 1:1 relationships to primary key of their relatives.
I'm asking myself how cam I map the relationship between the mnemonic entity and recipes, ingredients an so on.
Or (to be more specific) how can the category_id in the mnemonic table be automatically populated with the value of the the primary key of the category table?
I tried first this kind of association mapping in my Category model:
class Category
{
/**
* #var integer $id
*
* #Column(name="id", type="bigint", nullable=false)
* #Id
* #GeneratedValue(strategy="IDENTITY")
* #OneToOne(targetEntity="Mnemonic", mappedBy="category")
* #JoinColumn(name="id", referencedColumnName="category_id")
*/
private $id;
and in the Mnemonic model:
class Mnemonic
{
/**
*
* #OneToOne(targetEntity="Category", inversedBy="mnemonic")
* #JoinColumn(name="category_id", referencedColumnName="id")
*/
private $categoryId;
That didn't work out — Doctrine dont' produce any errors, but populates only the category table.
So I thought I could write some code in the __construct method of the Category model, where I would create a Mnemonic object, set his categoryId according to the ID of Category model and bind it to doctrine's entity manager. But that's kind of ugly — I think model classes should not be responsible for persistence.
During the writing of my question I thought, that the solution could be a kind of a Factory class. It would take all the persistence logic out of the model and could handle special cases.
What do you think? What is the best solution?
Best regards in advice
Paul
Have you thought about adding an event listener for onFlush? so essentially on flush, you check what object you are persisting, then execute some code such as creating the Mnemonic object.
Info about registering onFlush events can be found on the doctrine website at the following link:
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/events.html#onflush

Categories