Delete cascade with doctrine

I am a bit confused with this: https://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#removing-entities
My Client entity is OneToMany to Orders entities. If in Client class with mapping metadata to Order I use (‘onDelete’ => ‘CASCADE’), when I add a client all its orders will be removed when I run flush()? Or vice versa I have to put it in Order class with mapping to Client? or it works with ManyToMany mapping?

When you delete a client, all its orders will also be deleted when you flush, yes.

You could put (‘onDelete’ => ‘CASCADE’) on the Order’s mapping side, so that when you delete an order, it would also delete the related client. Though, that’s probably not the behavior you want.

I’m reasonably confident that this would cause the join table’s entry to be deleted. Though, Doctrine does that already, so an (‘onDelete’ => ‘CASCADE’) here is probably redundant.

Thanks, I see thst onDelete is not suffisient and have to add cascade => (‘persist’ ,‘remove’) too. I did not yet test if this array is sufficient if I remove onDelete. As about your another point, when order is deleted the client should not be deleted but invoice, invoice items and transactions, and when invoice is deleted, just invoice items and transactions. and of course if client is deleted these all should be deleted…

I am trying to insert a new Order row with doctrine, in Order entity I have clientId property that is ManyToOne to Client entity, because each client may have several orders.
When I try to persist/flush a new Order, I cannot set a value just by
$this->clientId = 1;
But if I set mapping and functions below then I can add an order with clientId with:

$client = $em->find(‘Entities\Client’, 1);
if ($client === null) {
echo "No client found.
";
exit(1);
}
$order = new Entities\Order;
$order->setClient($client);
$order->setFields($_POST);
$em->persist($order);
$em->flush();

Now my question is that if this is the only way to insert a new order? Isn’t it possible to simply set a clientId to add a new order?

Below is my mapping/functions I said above. and it works fine as I said, I am asking if there is no easier way to set clientId to add a new order without calling client entity before?

From here https://doctrine-orm.readthedocs.org/en/latest/reference/unitofwork-associations.html if I understood correctly it seems that yes, this is the only way to insert? Did I unerstand that page correctly?

========================================


In Client.php, that is OneToManyClient, we have to have something like this:

    public function __construct() {
         $this->orders = new ArrayCollection();
     }
 
    public function addOrder($order)
     {
         $this->orders[] = $order;
     }
 
    public function setFields($fields)
     {
         foreach ($fields as $key=>$value) {
                  $this->$key = $value;
         }
     }
 $metadata->mapOneToMany(array( 'fieldName' => 'orders', 
                               'targetEntity' => 'Entities\\\\Order', 
                               'mappedBy' => 'client', 
                               'joinColumns' => array( 0 => array( 'name' => 'client_id', 
                                                                   'referencedColumnName' => 'client_id', 
                                                                   'nullable' => true, 
                                                                   'columnDefinition' => NULL, 
                                                      ), )
                                  
                             ));

 
==
 In Order.php we have this:
 
    public function setClient($client)
     {
         $client->addOrder($this);
         $this->client = $client;
     }

 
    public function setFields($fields)
     {
         foreach ($fields as $key=>$value) {
                  $this->$key = $value;
         }
     }
 $metadata->mapManyToOne(array( 'fieldName' => 'client', 
                               'targetEntity' => 'Entities\\\\Client', 
                               'inversedBy' => 'orders', 
                               'joinColumns' => array( 0 => array( 'name' => 'client_id', 
                                                                   'referencedColumnName' => 'client_id', 
                                                                   'nullable' => true, 
                                                                   'columnDefinition' => NULL, 
                                                      ), )
                                  
                             ));
 
==
 
Then to insert $_POST order data to db for client_id = 1, we can do this:
$client = $em->find('Entities\\Client', 1);
 if ($client === null) {
     echo "No client found.\
";
     exit(1);
 }
 $order = new Entities\\Order;
 $order->setClient($client);
 $order->setFields($_POST);
 $em->persist($order);
 $em->flush();