Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php symfony学说中的一对一关系运算_Php_Symfony_Doctrine Orm - Fatal编程技术网

Php symfony学说中的一对一关系运算

Php symfony学说中的一对一关系运算,php,symfony,doctrine-orm,Php,Symfony,Doctrine Orm,我不清楚如何处理来自一方的操作 让我们假设物体A在单面,物体B在单面 我想在创建一个对象B时,将许多对象A分配给它-我的解决方案是获取A对象并分配给它们 B对象 我希望在删除对象B时,将对象的所有引用设置为null A-通过研究,我发现我可能必须添加一个ondelete=“setNull” 桌上的功能 有没有更好的方法(或其他方法)来处理这种情况 use Doctrine\ORM\Mapping as ORM; class AObject { // ... /**

我不清楚如何处理来自一方的操作

让我们假设物体A在单面,物体B在单面

  • 我想在创建一个对象B时,将许多对象A分配给它-我的解决方案是获取A对象并分配给它们 B对象

  • 我希望在删除对象B时,将对象的所有引用设置为null A-通过研究,我发现我可能必须添加一个
    ondelete=“setNull”
    桌上的功能

  • 有没有更好的方法(或其他方法)来处理这种情况

    use Doctrine\ORM\Mapping as ORM;
    
    class AObject
    {
        // ...
    
        /**
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\BObject", inversedBy="a", onDelete="SET NULL")
         */
        private $b;
    }
    
    class BObject
    {
        // ...
    
        /**
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\AObject", mappedBy="b", cascade={"persist"})
         */
        private $a;
    
        public function __construct()
        {
            $this->a = new \Doctrine\Common\Collections\ArrayCollection();
        }
    
        /**
         * @return \Doctrine\Common\Collections\ArrayCollection
         */
        public getA()
        {
            return $this->a;
        }
    
        /**
         * @param \Doctrine\Common\Collections\ArrayCollection $a
         */
        public setA($a)
        {
            $this->a = $a;
        }
    
        /**
         * @param AObject $a
         * @return BObject
         */
        public addA($a)
        {
            $this->a[] = $a;
            $a->setB($this); // assign B object to A
    
            return $this; // For method chaining
        }
    
        /**
         * @param AObject $a
         */
        public removeA($a)
        {
            $this->a->removeElement($a);
        }
    }
    
    对于1)您仍然需要获取对象,您的代码无法神奇地知道将哪个A对象分配给新的B对象

    使用上面定义的类B,您可以编写

    $b = new BObject();
    $b
        ->addA($a1)
        ->addA($a2)
        ->addA(new AObject());
    $entityManager->persist($b);
    $entityManager->flush();
    
    A对象将引用B


    对于2)它是更好的解决方案,请将其处理到数据库级别。

    出于您的目的,您可以使用它

    对于示例,首先需要定义事件侦听器:

    # /src/FooBundle/Resources/config/services.yml
    services:
    
    # Event subscribers
    foo.bar.subscriber:
        class: FooBundle\EventListener\BarSubscriber
        tags:
           - { name: doctrine.event_subscriber }
    
    然后,创建一个:

    // FooBundle\EventListener\BarSubscriber.php
    namespace FooBundle\EventListener;
    
    use Doctrine\Common\EventSubscriber;
    use Doctrine\ORM\Event\LifecycleEventArgs;
    
    class BarSubscriber implements EventSubscriber
    {
        public function getSubscribedEvents()
        {
            return array(
                'preRemove',
                'postPersist',
            );
        }
    
        // This method will be executed on each entity, before it has been deleted
        // You can do here what u want in your second paragraph
        public function preRemove(LifecycleEventArgs $args) 
        {
            $entity = $args->getEntity();
    
            // Do not forget to check your entity type
            if (!$entity instanceof SomeInterface) {
                return;
            }
    
            // Some code that handle some actions on related entites
        }
    
        // This code will be executed after each entity creation
        // Here you can add more relations for your main entity
        public function postPersist(LifecycleEventArgs $args) 
        {
            $entity = $args->getEntity();
            if (!$entity instanceof TimestampableInterface) {
                return;
            }
    
            // Some actions on relations, after the establishment of the main object
        }
    }
    

    另外,您也可以使用,删除所有子实体

    ,只是为了清晰起见重新格式化