How to clone Doctrine 2 entity with relations? - doctrine-orm

I have an entity that has a few relations. I need to clone this entity with relations. How can I clone Doctrine 2 entity with relations?

Benjamin Eberlei answered this question before:
You can implement __clone() and handle the copy correctly in userland.
Source: https://groups.google.com/group/doctrine-user/browse_thread/thread/36edab6b2ac39204/1a39a8b0eaa213a8

Related

Symfony & Doctrine: Optional foreign key

Without scrutinizing why I want this (it may sound like a bad approach, but I have good reason) I want to know if there is a way in the standard-framework-edition 3.1+ to create a relational association to an entity that may not exist...
Firstly I do realize this determines the schema and that's fine. So if an entity does not exist, it doesn't create a foreign key and the field is always null, or if the target entity does exist, it creates the foreign key and the field works like a normal association...
Secondly, this only changes project to project, and may change down the line as an update to which I realize a manual schema update could be necessary.
Preferably without 3rd party bundle dependencies... hoping for the standard framework to do this,
Anybody?
Thanks in advance
Edit
I am using annotations in my entities with doctrine ORM
Furthermore
The simplest version of why I am doing this is because certain bundles are optional project-to-project, and bundle A may make use of entities in bundle B only if it is present. I have considered using services and if container->has then container->get, or the XML on-invalid="null" approach, but that doesn't address property persistence. I was happy with storing a non-mapped value as a custom relational field, which is fine, just lengthier and wondered if perhaps there was a way Doctrine could ignore a missing targetEntity...
Hm, perhaps I misunderstand your question, but this sounds like a normal 'nullable' association to me?
Create your assocation via annotation:
/**
*
* #var Child
* #ORM\ManyToOne(targetEntity="Child")
*/
private $child;
and use
setChild(Child $child = null)
{
$this->child = $child;
}
as a Setter to allow nullable values.
And your getter might look like:
getChild()
{
return $this->child;
}
In case there isn't any child it will return null.
I will keep the other answer as it responds to the question for a 'nullable association target' live data.
This is the answer for a 'nullable association target' meta data which is a different thing.
OP asks to provide a targetEntity in the metadata which cannot exist in his case, e.g. is not there in a different bundle (or whatever OP's mysterious reason might be).
In that case I recommend to build upon Doctrine's TargetEntityListener which is able to resolve the targetEntity during runtime and targetEntity can be set to an Abstract Class or an Interface:
/**
* #ORM\ManyToOne(targetEntity="Acme\InvoiceBundle\Model\InvoiceSubjectInterface")
* #var InvoiceSubjectInterface
*/
protected $subject;
InvoiceSubjectInterface will then be replaced during runtime by a specific class provided by config e.g.:
# app/config/config.yml
doctrine:
# ...
orm:
# ...
resolve_target_entities:
Acme\InvoiceBundle\Model\InvoiceSubjectInterface: AppBundle\Entity\Customer
So this should be eiter an extendable behaviour for providing no class or implementing an own solution.

Find all entities without a relation

My question heavily depends on this: Doctrine: Query only where relationship doesn't exist?
I wonder how the very same thing can be accomplished using Doctrine2. I am especially interested in good practices. Do I have to use the QueryBuilder or is it possible to use a findBy on a repository?
Eventually I used QueryBuilder like this (example from the question linked above):
// $entitiyManager: Doctrine EntityManager instance
$qb = $entityManager->createQueryBuilder();
$x = $qb->expr();
$qb->select('article')
->from('Article', 'article')
->leftJoin('article.category', 'category')
->where($x->isNotNull('category.id');

Doctrine : Relation with undefined/multiple entities

I need to create an like/dislike system which can be used on any entity. I'm going to create a Like entity with an 'Id', 'Entity' (can be anything), 'author' (ManyToOne with User class) and 'like' (boolean).
I just want to know if there is a good way to do it ?
I can't use the table inheritance (mappedsuperclass) because this entity will be part of a bundle (SocialBundle) which can be used on several project (It will be a vendor).
I have no code to show you because i'm still in the analysis part.
Thanks !
Create an interface for that entity and later you can map this interface to any entity using addResolveTargetEntity method. See this.

typo3 flow persist updated relation

I have an issue TYPO3 Flow updating my relations.
Am I wrong, that Flow should update changed relations automatically, so I don't have to update the related entities with the respective repository?
Example 1:
I have a model "Project" with multiple "Job" childs on attribute "jobs".
If I do:
$project->setJobs($collectionOfJobs);
$this->projectRepository->update($project);
then jobs are not updated correctly with the new project-id.
Example 2:
I wanted to realize a bidirectional one-to-one relationship between the models "Project" and "Briefing" and found out, that there is a known bug in TYPO3:
Bidirectional One-To-One Relationships in Flow
So I wanted to to fix it with setting the relation on the other side manually:
class Briefing {
/**
* #param \Some\Package\Domain\Model\Project $project
* #return void
*/
public function setProject($project) {
$this->project = $project;
$this->project->setBriefing($this);
$this->projectRepository->update($this->project); // FIXME: Bug? Flow should do this
}
but I had to update the relation with its repository by self. Shouldn't Flow do this automatically?
So do I really need to update each child with its repository by self or should Flow do this for me?
Environment:
- TYPO3 FLOW 2.3.3 (latest stable)
- Doctrine 2.3.6
- PHP 5.4.39-0+deb7u2
From the Flow manual:
When you add or remove an object to or from a repository, the object will be added to or removed from the underlying persistence as expected upon persistAll. But what about changes to already persisted objects? As we have seen, those changes are only persisted, if the changed object is given to update on the corresponding repository.
Now, for objects that have no corresponding repository, how are changes persisted? In the same way you fetch those objects from their parent - by traversal. TYPO3 Flow follows references from objects managed in a repository (aggregate roots) for all persistence operations, unless the referenced object itself is an aggregate root.
So if there is a repository for your entity, you must explicitely call the update method. This was different originally but changed for Flow 1.0.
Maybe you think that this should work because it worked in TYPO3 CMS Extbase < 6.2 until it was changed there, too.

Entity Framework 5 - Migrations and creating table from entity

I am using Entity Framework 5 RC and i have some code that require a specific table on the database. The entity is already created using Code-First.
a) Is there a way to tell EF to create the table if its not already created in the database ? If yes.. how ?
b) Also.. if the table already exist, can it handle entity changes such adding properties to the entity.. will it get reflected on the database ? (We still use the code-first approach here)
Thanks!
Use code first migrations (either automatic or code based). If you are adding table to existing database create initial migration first and than add your new entity. All links are for EF 4.3 but the usage is exactly the same in EF 5.
For reference for anyone else having this problem, I had the same problem and my solution is here
Entity Framework: Generating specific table if doesn't exist?