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
Related
I have two entities with One-To-Many relationship - User and Log.
class User {
/** #OneToMany(targetEntity="Log", mappedBy="user") */
private $logs;
}
class Log {
/**
* #ManyToOne(targetEntity="User", inversedBy="logs")
* #JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
}
What I want to achieve is following - Once I delete the User entity, instead of cascade deleting all of that User logs, I want to be able to save those logs and still be able to keep that user_id. I was thinking of creating a new database table old_logs with the same structure as logs table, where all of the data from User entity will be copied to before deleting the User entity, as I believe I can't keep the user_id pointing to non-existing user (with the use of foreign keys).
Is something like this possible through doctrine Entity class?
If you have a set of association mapped entities in Doctrine, sometimes you might want to retrieve those entities without its mapped associations being fetched and slowing the query down.
For example I have a set of entities which are association mapped in a chain of linked database tables. They are all OnetoMany associations and act as a hierarchy of prices in matrices on product pages. They can be represented as so:
SitePage->SiteMatrix->SiteItems->SiteItemPrices.
The associated mapping works perfectly, and when I use the findBy method to get the root SitePage object it contains arrays which represent the mapped entities down the chain. In other words the SitePage object contains all matrices, which contains all items which contains all prices. So far so good.
My problem is that every time I get a list of pages on my site, doctrine is walking the entire association mapping tree and returning me with the entire datatabase which is very slow. Sometime I want to just get my SitePage entity by ID and not contain all the mapped associations.
I have looked into lazy and extra lazy loading of associations but they only seem to affect certain functions, and not findBy etc. The official documentation is far from helpful:
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/tutorials/extra-lazy-associations.html
Other similar questions on Stack Overflow have gone unanswered:
Doctrine 2 Association Mapping Overhead?
Is there an easy way to fetch an entity without its mapped associations? The easiest way I can see currently is to create two entities for each database table, one with association mapping and one without for use in the separate situations where they are required. It seems odd to me that you cannot simply fetch an entity and specify whether you want to link to it to other entities or fetch it by itself.
Thanks for any information on the subject.
The JMSSerializer exclusion strategies can help you.
First, exclude all properties by default :
// ...
use JMS\Serializer\Annotation as JMS;
/**
* #ORM\Entity
* #JMS\ExclusionPolicy("all")
*/
class Foo
Then, choose to exclude or expose your properties :
/**
* #ORM\Column(type="string")
* #JMS\Expose
*/
protected $name;
Also, for your associations, you can use the MaxDepth to restrict the associated entries of your results. Example :
// Entity
/** #MaxDepth(1) */
private $selfAssociatedEntries;
// Controller
use JMS\Serializer\SerializationContext;
$serializer->serialize($data, 'json', SerializationContext::create()->enableMaxDepthChecks());
Like this, your entity will contains the $selfAssociatedEntries serialised, but the $selfAssociatedEntries doesn't have the $selfAssociationEntries property.
The serialisation is restricted to the first parent object.
Groups are a powerful feature that allows you to expose properties for some actions and exclude them for another :
/**
* #ORM\Column(type="string", length=255, nullable=true, unique=false)
* #JMS\Groups({"default"}) // Set the group to expose the property
*/
protected $name;
In your controller, set the group used for serialisation :
// Property 'name' is exposed
$serializer->serialize($data, 'json', SerializationContext::create()->setGroups(array('default')));
For more informations, look at Exclusion Strategies chapter of the documentation.
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
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;
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;