SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    delete cascade with doctrine

    I am a bit confused with this: https://doctrine-orm.readthedocs.org...oving-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?

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by nimasdj View Post
    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()?
    When you delete a client, all its orders will also be deleted when you flush, yes.

    Quote Originally Posted by nimasdj View Post
    Or vice versa I have to put it in Order class with mapping to Client?
    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.

    Quote Originally Posted by nimasdj View Post
    or it works with ManyToMany mapping?
    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.
    "First make it work. Then make it better."

  3. #3
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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...

  4. #4
    SitePoint Addict nimasdj's Avatar
    Join Date
    Dec 2007
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.\n";
    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...ociations.html if I understood correctly it seems that yes, this is the only way to insert? Did I unerstand that page correctly?

    ========================================
    Code:
    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.\n";
         exit(1);
     }
     $order = new Entities\Order;
     $order->setClient($client);
     $order->setFields($_POST);
     $em->persist($order);
     $em->flush();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •