S
S
slip312021-03-29 10:53:20
symfony
slip31, 2021-03-29 10:53:20

How to use Doctrine to persist related entities?

Hello. I have one project written in pure PHP.
I create a Company entity in it using a handler (well, my implementation of CommandBus). Each company has a service that it provides - an array of company_services. There was a question about connection to all this Symfony. But ok - I connected the commandbass with Messenger, made the value object using Embeddable, the question remained how to add the company's services to the repository.

/**
 * @ORM\Entity(repositoryClass=CompanyRepository::class)
 * @Table(name="company")
 */
final class Company {

    /**
     * @Embedded(class = "Id",columnPrefix=false)
     */
    private Id $id;

    /**
     * @Embedded(class = "Inn",columnPrefix=false)
     */
    private Inn $inn;
    /**
/**
     * @ORM\OneToMany(targetEntity="App\Company\Domain\Entity\CompanyServices", mappedBy="company")
     * @Assert\NotNull
     */
    private array $company_services;
    public function __construct(
        Id $id,
        Inn $inn,    
        Address $address,
        array $company_services,
        DateTimeImmutable $date
    )
  
}

And this is how I need to store it in the handler
public function __invoke(CommandInterface $command): void
    {
        $company = new Company(
            $id = Id::next(),
            new Inn($command->getInn()),
            new Address(
            ...
            ),
            $command->getCompanyServices(),
            $date = new DateTimeImmutable()
        );
        $this->repository->add($company);
            }
    }


Company is saved, everything is ok. But I still need to store the $company_services array as a related entity. On pure php, everything is clear - I just save it in a cycle with PDO in the repository, but not so much with symphony. The documentation uses setters and then everything is saved by flush(). But I don't have setters, I have constructors. I can create something like a setter in Company. But what do I do with it next? As I understand it, in a Feng Shui symphony, this is to create an entity CompanyService, to which, accordingly, a repository, but how to save all this without setters in CompanyService and from the handler? Well, I don't understand the documentation.public function addCompanyServices($services) {}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2021-03-29
@slip31

Persistence by Reachability: Cascade Persist
There are additional semantics that apply to the Cascade Persist operation. During each flush() operation Doctrine detects if there are new entities in any collection and three possible cases can happen:

  • New entities in a collection marked as cascade: persist will be directly persisted by Doctrine .
  • New entities in a collection not marked as cascade: persist will produce an Exception and rollback the flush() operation.
  • Collections without new entities are skipped.

https://www.doctrine-project.org/projects/doctrine...
/**
 * @OneToMany(targetEntity="Article", mappedBy="topic", cascade={"persist", "remove"})
 */
 private $articles;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question