Multiple account-types? - php

I'm currently using Symfony2 and doctrine. and I'm working on a "create-account" page. There will be 2 different kinds of accounts avaliable on the page, one for normal-usersand one for companies.
I've got an entity (table) called Account this table contains the username, passwordand saltcolumns.
If a company logs in I want to have the company information from a company-table and if a normal-userlogs in I want the information from the user-table.
I've been thinking about having a column named usertype which tells me what type it is, then if the usertype is a company retrieve the company-information-entity and if it's a userretrieve the user-information-entity (corresponding to the id of the account).
Is this a good way to solve this problem? or should I have 2 different tables? AccountUser and AccountCompany?

If you have many columns in Company table that don't exist in the User table and vice versa the best way to solve your problem is to use class-table inheritance which will create three tables: Account, User, Company. User and Company must be subclasses of Account entity.
According to link above you should have code like this:
namespace MyProject\Model;
/**
* #Entity
* #InheritanceType("JOINED")
* #DiscriminatorColumn(name="usertype", type="string")
* #DiscriminatorMap({"user" = "User", "company" = "Company"})
*/
class Account
{
// ...
}
/** #Entity */
class User extends Account
{
// ...
}
/** #Entity */
class Company extends Account
{
// ...
}

Related

Doctrine optional OneToOne entity deletion

I have two entities, a Flight and a Booking.
They are configured in a bi-directional one-to-one relationship, with the Flight underlying table holding the foreign key to the Booking underlying table.
The idea is that a Flight instance must be linked to a Booking, but a Booking can exist without a Flight.
/**
* #ORM\Entity
*/
class Flight{
/**
* #ORM\OneToOne(targetEntity="Booking", inversedBy="flight")
* #ORM\JoinColumn(name="bookingID", referencedColumnName="ID", nullable=false)
*/
private Booking $booking;
/// Rest of code removed for readability
}
/**
* #ORM\Entity
*/
class Booking{
/**
* #ORM\OneToOne(targetEntity="Flight", mappedBy="booking")
*/
private ?Flight $flight;
/// Rest of code removed for readability
}
I do not want to use cascading operations since a Flight can be removed without the Booking being removed (this is an extension of the idea stated above).
An issue arises when I try to delete both the Flight and the Booking.
Picture the following code:
// $flight holds a reference to $booking, as per the code above
$em->remove($flight);
$em->remove($booking);
$em->flush();
It would result in the following queries (logged by Doctrine)
START TRANSACTION
DELETE FROM flights WHERE ID = ?
INSERT INTO flights (redacted) VALUES (redacted)
DELETE FROM bookings WHERE ID = ?
ROLLBACK
Which I simply cannot understand.
If I remove the Booking directly through the underlying PDO Handle, it works perfectly, which means that is has to be a mapping issue, but I cannot see where I did something wrong. It's been driving me crazy for hours.

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

Sonata Admin Bundle listbox sort order

I have 2 entities one referenced to another with many to one relation. For example, User and City.
I need listbox with Cities in User edit page to be sorted by Name, not by Id.
How can I do it?
In your Entity, you can specify the ordering .
Example :
<?php
/**
* #ManyToMany(targetEntity="Group")
* #OrderBy({"name" = "ASC"})
*/
private $groups;

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

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;

Is it possible to get the ID of entity after persist and before Flush in Doctrine2?

I have the User Login Registration Page.
Now on the same Page i have one more Forms i.e UserInterests.
Now i have the PostPersist function which creates new UserProfile after User is Persisted
Now UserProfile is linked with User ID and UserInterests is linked with UserProfile ID
Now the client wants UserInterests on same User page but i have problem that UserProfile is not yet created. Now how can persist them. Is there any way
I don't think you can get the ID before flushing.
You could create an association between the models, that way Doctrine will take care of the id's when saving and you can retrieve your UserInterests with something like:
$user->getProfile()->getInterests();
So you would have your User model with a property that holds your UserProfile:
/**
* #OneToOne(targetEntity="UserProfile")
* #JoinColumn(name="profile_id", referencedColumnName="id")
**/
private $profile;
and your UserProfile class should have a property to hold the UserInterests model.
/**
* #OneToOne(targetEntity="UserInterests")
* #JoinColumn(name="interests_id", referencedColumnName="id")
**/
private $interests;
You can now create an empty $userProfile model (to link the others together, the actual filling can be done in your postPersist function) and a $userInterests model, associate them by
$interests = new UserInterests();
// create an empty UserProfile, and fill it in your PostPersist function,
// that way it can already be used to link the User and UserInterests
$profile = new UserProfile();
$profile->setInterests($interests);
$user->setProfile($profile);
Now Doctrine will fill in the ids when persisting and you don't need to worry about them.
More information here

Categories