Symfony, Move data from em1 to em2 - symfony

I use Symfony 2.8.
I have a huge table with a lot of datsa i don't need anymore. I want to move datas in another database with a symfony command.
I have 2 entities managers:
em_archive = mydb_archive
em_default = mydb
I have the code:
$emDefault = $this->getContainer()->get('doctrine')->getManager('em_default');
$emArchive = $this->getContainer()->get('doctrine')->getManager('em_archive');
$repoArchive = $emDefault->getRepository('MyBundle\Datas', 'em_archive');
$repoDefault = $emDefault->getRepository('MyBundle\Datas', 'em_default');
$dataTest = $repoDefault->getOneDataTest();
$dataTest->setOldId($dataTest->getId());
$dataTest->setId(null);
$emArchive->persist($dataTest);
$emArchive->flush();
With the code i have the error "Notice: Undefined index: 000000004618b9830000000172fdd8f3"
Is there a problem with one of my entity links (onetomany, manytoone) ?

Entities are mapped to an Entity Manager by the class type, so you cannot have the same class mapped into two entity manager. This is by design, as you can see from proxy classes, which contains references to the UnitOfWork and/or to the owning EntityManager.
Also, the performance of load-hydrate-persist-dehydrate-flush is very poor, you get lot of memory leak issues, and so on.
So, you can't use the ORM layer, but you can still use Doctrine DBAL.
See DBAL insert for an example of "mapping" syntax, or use good old-fashioned queries.

Related

Doctrine 2 base entities like those in doctrine 1?

I'm starting a new project with Symfony2/Doctrine2 and I generated my entities from the DB with the mapping:convert and mapping:import commands.
My entities are fine, but I only have the generated entities. Back in Doctrine1/Symfony1, you had your entities generated twice : once in the doctrine folder, an almost empty class just extending the second one in doctrine/base folder where all the doctrine internals were (getters/setters, fields...)
This way, you could add your own methods in the first file where they remained untouched even if you generated again the entities (only the doctrine/base files were modified by the generator).
Am I missing something ?
Well the generate command only generates getters and setters that are undefined and appends them to the (entity) file. This means that your custom getters/setters will never be overwritten. Does that answer your question?

How to use TransactionScope while following mvc4 dependency Injection

As i am new to Dependency Injection concept. I am following Database first approach and using Entity Framework.
I have created IRepository Interface containing all crude operation.
and i have class Repository class implementing this`interface
I am creating the classes and interfaces of db tables and implementing these two into them.`How can I use Transaction scope while Insertion and Update.
And How to use joins in controller while following this pattern
If this three tables present three linked entities, you should use navigation properties in your E-F mapping. In this case, you create one entity with navigation properties and save it. Transaction will be addwd automatically.
If your entities is not linked with navigation properties, you should have in your repository special method, that will add all three entities to db and in this method you should use TransactionScope

Doctrine and Symfony: magic methods and caching

I'm currently implementing the doctrine result cache so I've set
result_cache_driver: apc
into my configuration.
Then I've correctly got query cache working inside the Repository, using for example
->setResultCacheId(sprintf('posts_for_user_%d', $userId))
First problem come when I used these things in doctrine:
$repository->findOneBy(array)
$repository->findBy(array)
which can maybe easily overridden in the repository.
The problem which I can't get past is to use the ParamConverter to use doctrine caching and also entities association.
For example, if I have a Team entity with a OneToMany relation to Player I usually do
$team->getPlayers()
I don't have the control over the caching of that query. Is that possible in some way?
When you run methods like find/findBy or process over PersistentCollection doing $team->getPlayers(), there is UoW which loads the data using EntityPersister and ObjectHydrator to hydrate an object. These objects have no support of result cache driver.
In the other hand, when you use DQL or QueryBuilder, your code products Query object that extends AbstractQuery. If you look inside AbstractQuery::execute you will see this pretty piece of code which makes using of result cache driver possible
$cache = $queryCacheProfile->getResultCacheDriver();
$result = $cache->fetch($cacheKey);
if (isset($result[$realCacheKey])) {
return $result[$realCacheKey];
}
So my suggestion - try load your entities using QueryBuilder and leftJoins on children Collections.
$this->createQueryBuilder('x')->select('x, p')->leftJoin('x.players', 'p')->....;
It'll create the possibility of using result cache driver.

Shared PKs and ObjectContext errors

I am using EF 4.3 in an ASP.NET WebForms application. I've started with model first approach with context object of type ObjectContext and POCO code generator (via T4).
At the beginning the Context was created at the beginning of every service method. While working on performance I decided to switch to context per web request. Unfortunately I have encountered an issue with Table-per-Type inheritance. I have two entities: Offer and OfferEdit. They are in a one to one relationship, and both share the same primary key. Basically an OfferEdit (OfferEdits table) is created once an Offer is being edited.
I query the context for a particular Offer entity more then once during web request. The error I get trying to execute:
var offer = Context.Offer.Where(o => o.Id == offerId).FirstOrDefault()
when this offer is already loaded to Context.Offer.EntitySet is
All objects in the EntitySet 'RuchEntities.Offer' must have unique primary keys.
However, an instance of type 'Ruch.Data.Model.OfferEdit' and an instance of type'Ruch.Data.Model.Offer' both have the same primary key
value,'EntitySet=Offer;Id=4139'.
Will appreciate all advice.
Sounds like you are misusing TPT inheritance. To make it clear EF inheritance works exactly same way as in .NET - the entity can be either of type Offer or OfferEdit. You can convert OfferEdit to Offer but it is still OfferEdit. It can never be of both types which means you can never have Offer entity with Id same as OfferEdit entity because same key cannot be used by two entity instances. You also never can change instance of Offer to OfferEdit because .NET doesn't allow you changing type of existing instance.

Mocking DbEntityEntry.CurrentValues.SetValues() in EF4 CTP5 Code First

I am trying to use the DbEntityEntry.CurrentValues.SetValues() method to facilitate updating an existing entity with values from a non-entity DTO (see: http://blogs.msdn.com/b/adonet/archive/2011/01/30/using-dbcontext-in-ef-feature-ctp5-part-5-working-with-property-values.aspx)
I'm having trouble removing the dependency on DbEntityEntry though (for mocking, testing). Here is an example of what I would like to do:
var entity = dbSet.Find(dto.Id);
var entry = context.Entry(entity);
entry.CurrentValues.SetValues(dto);
context.SaveChanges();
I've also considered:
EntityType entity = new EntityType() { Id = dto.Id };
context.Attach(entity);
var entry = context.Entry(entity);
entry.CurrentValues.SetValues(entity);
context.SaveChanges();
From what I've been able to find both seem reasonable when working with an actual DbContext, but when I abstract the context to an IMyContext I lose the capability to get a DbEntityEntry for an entity, thus losing the SetValues option.
Is there any way to work around this issue, or do I need to bite the bullet and manually set modified properties on the entity from the DTO (potentially a lot of boilerplate for entities with many properties)?
(I'm fairly new to EF and this is my first StackOverflow question, so please be gentle)
If you have never used it before, this would be a great use for AutoMapper (also available via NuGet). I am unaware of how to solve your IMyContext issue and would also resort to mapping the properties. But instead of doing so manually, I would allow AutoMapper to do the heavy lifting.

Resources