-
-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create entity from another (clone) #262
Comments
Hey @bastien70! Not possibly currently but is an interesting idea. Maybe something like: $post1 = PostFactory::createOne(['title' => 'original title']);
$post2 = $post1->clone(['title' => 'cloned title']); Not sure what What about relationships, should these be cloned? |
Mmmh I was thinking about this kind of scenario : We have an entity like this : class Category
{
private ?int $id = null;
private ?string $title = null;
private Collection $articles;
private ?DateTimeImmutable $createdAt = null;
public function __construct()
{
$this->articles = new ArrayCollection();
$this->createdAt = new DateTimeImmutable();
}
public function __clone(): void
{
$this->id = null;
$this->createdAt = new DateTimeImmutable();
}
} With a Next, one can imagine that one wishes to continue the modification of this new entity but from the factory. Maybe with something like : $currentCategory = ...;
$newCategory = clone $currentCategory; // new category but with same title and name
$newCategory = CategoryFactory::new()->from($newCategory)->createOne(['title' => 'cloned title]); At the end, we would therefore have a new category persisted in the database, created from a previous category, but to which we have modified the title. Obviously in reality, we can imagine that we have many more properties at the level of the entity, hence the usefulness of the clone. We can even go further by assuming that the The ideal would also be to be able to clone the relations (in the case of the Category entity of the example, it has several articles. If we can also duplicate these articles, that would be good, but I imagine that it would be done in outside of Foundry-related features). In the end, the only role of the bundle would be to add a |
I see. Yeah, what I was thinking might make more sense as Maybe a $newCategory = CategoryFactory::from($oldCategory) // clones $oldCategory, removes id field (in case you don't have a __clone method that does this), extracts existing properties as defaults
->createOne(['title' => 'cloned title'])
; Still not sure how we'd handle relationships though. |
So for the relations, in a "normal" code that we would put in the public function __clone(): void
{
$this->id = null;
$this->createdAt = new DateTimeImmutable();
$clonedArticles = new ArrayCollection();
/** @var Article $article */
foreach($this->articles as $article)
{
$articleCloned = clone $module;
$articleCloned->setCategory($this);
$clonedArticles->add($articleCloned);
}
$this->articles = $clonedArticles;
} (we imagine that the Article entity also has a |
I think the default should be (assuming no __clone method set):
A real WDYT? |
Delete the id, that's for sure, we agree. Afterwards concerning relationships, it's hard to say, we can't imagine a default behavior because... would there really be one? To be honest, it can really depend, so I don't know. Perhaps an integer in the method that would allow you to say the depth at which you want to clone? Example : We have the Category entity which has several Articles. Article entity has multiple Tags We can then imagine : => We pass What do you think ? |
Yeah, I think that makes sense. By default, follow the standard PHP I believe overriding the Of course, after calling What we don't want is for users to have to override the A followup iteration could add the depth option (I'd like to keep the feature as simple as possible to start). |
Yes ! :)
Yes too Yes the goal is not to give too much work to the user, at least not superficial thing |
Hello, is it possible to use the Factory to create some entity using a cloned entity ?
By example, I've the entity Foo with one entry in database, I want to create another entity Foo but with the clone of the last database entry, and just update after that some properties.
Is it possible with Foundry ?
The text was updated successfully, but these errors were encountered: