简体   繁体   English

Symfony2:自定义实体?

[英]Symfony2: custom entity?

A solution to the problem raised in this question where a Client entity is split across two databases - one local, one foreign, is to build the local client such that it contains the foreign client as an object and has getters/setters to maintain the foreign client object. 解决此问题的方法是 ,将一个Client实体拆分到两个数据库中-一个本地,一个国外,是建立本地客户端,使其包含该外部客户端作为对象,并具有getter / setter来维护该外部客户端客户对象。 Local relationships are maintained in the local Client entity. 本地客户实体中维护本地关系。 The question now is how best to create the Client entity so that it can be used in more than one place. 现在的问题是如何最好地创建Client实体,以便可以在多个地方使用它。

Current Client controller method 当前的客户端控制器方法

public function getClient($id = null) {
    if (!empty($id)) {
        $localEm = $this->getDoctrine()->getManager();
        $foreignEm = $this->getDoctrine()->getManager('split');

        $client = $localEm->getRepository('ManaClientBundle:Client')->find($id);
        $foreignId = $client->getCid();
        $foreignClient = $foreignEm->getRepository('ManaSplitBundle:Client')->find($foreignId);

        $client->setForeignClient($foreignClient);
    } else {
        $client = new Client();
        $client->setForeignClient(new ForeignClient());
    }
    return $client;
}

Attempts to do this or something like it in the Client Repository fail. 尝试在客户端存储库中执行此操作或类似操作失败。 As do attempts to inject the following service into a standalone class. 尝试将以下服务注入独立类中也是如此。 This fails with the error about argument 1 of the constructor being missing. 由于缺少有关构造函数的参数1的错误,此操作失败。

services.yml snippet: services.yml代码段:

      foreign_client:
        class: Mana\ClientBundle\DataCombine\ClientCombine
        arguments:
          localEm: @doctrine.orm.entity_manager
          foreignEm: @doctrine.orm.split_entity_manager

ClientCombine class snippet: ClientCombine类代码段:

namespace Mana\ClientBundle\DataCombine;

use Doctrine\ORM\EntityManager;

class ClientCombine {

    private $localEm;
    private $foreignEm;

    public function __construct(EntityManager $localEm, EntityManager $foreignEm) {
        $this->localEm = $localEm;
        $this->foreignEm = $foreignEm;
    }
...
}

I use two entities Beverage and Beer. 我使用饮料和啤酒这两个实体。 Each has its own entity manager. 每个人都有自己的实体管理器。 Beer is in frosty and beverage is in freezer. 啤酒在霜冻中,饮料在冰柜中。 My strategy is to get a list of both types of entities, and pair them up, on the beverage side, and match them up. 我的策略是获取两种实体的列表,并在饮料方面将它们配对,然后进行匹配。 Similar functionality exists for a single entity. 对于单个实体存在类似的功能。 However, if you deal with a list of entities you need to do them in bulk, or you will have an extra query FOR EACH ENTITY ON THE LIST . 但是,如果您处理的是实体列表,则需要批量处理,否则您将在列表上有一个额外的查询每个实体 This is bad. 这不好。

The following has, I believe all non-essential parts stripped out. 我相信,以下所有非必要部分都已剔除。 This is a painful and cumbersome approach. 这是一种痛苦且麻烦的方法。 I was able to abandon it by going to a schema-based approach, because you can specify schema in doctrine orms. 我可以通过使用基于模式的方法来放弃它,因为您可以在原则规范中指定模式。 However, this code did do the job for me. 但是,这段代码确实为我完成了工作。

You have been warned, repeatedly, ( by me in other Q&A pairs, actually. ) 一再警告您(实际上,在其他问答环节,我曾警告过您)。

A super controller, of which your controllers should extend to get the marriage functionality. 超级控制器,您的控制器应该对其进行扩展以获得结婚功能。

    <?php

    namespace beverage\beverageBundle\Controller;

    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;

    class ThawController extends Controller
    {
        public static function defrost( $controller, $beverages ) {
            $em = $controller->getDoctrine()->getManager( 'frosty' );
            $beverageIds = array();
            foreach ( $beverages as $k =>$v ) {
                array_push( $beverageIds, $v->getIceid() );
            }

            $qb=$em->getRepository( 'frostyfrostyBundle:Beer' )
            ->createQueryBuilder( 't' )
            ;
            if ( array() == $beverageIds ) {return null;}
            $qbeers=$qb->where( $qb->expr()
                ->in( 't.id', $beverageIds ) )
            ->getQuery()
            ->getResult();
            foreach ( $qbeers as $k => $beer ) {
                $id=$beer->getId();
                $beers[$id]=$beer;
            }
            foreach ( $beers as $k => $beer ) {
            }
            foreach ( $beverages as $k => $beverage ) {
                $beverage->ice( $beers[$beverage->getIceid()] );
            }

            return $beverages;
        }

        public static function thaw( $controller, $beverage ) {
            $beer= null;
            $em = $controller->getDoctrine()->getManager( 'frosty' );
            $beer=$em->getRepository( 'frostyfrostyBundle:Beer' )
            ->createQueryBuilder( 't' )
            ->where( 't.id = '.$beverage->getIceid() )
            ->getQuery()
            ->getSingleResult();
            $beverage->ice( $beer );
            return $beverage;
        }
    }

and entity code: 和实体代码:

    <?php

    namespace freezer\freezerBundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use frosty\frostyBundle\Entity\Beer as beer;

    class Student {
        private $id;
        private $iceid;

        public function getId() {
            return $this->id;
        }

        public function setIceid( $iceid ) {
            $this->iceid = $iceid;

            return $this;
        }

        public function getIceid() {
            return $this->iceid;
        }

        public function __construct( beer $beer=null
                                   , $manyToMany = null ) {
            if ( $beer instanceof \frosty\frostyBundle\Entity\Beer ) {
                $this->ice( $beer, $manyToMany );
            }
        }

        public function setBeer( \frosty\frostyBundle\Entity\Beer $beer=null){
            $this->beer = $beer;

            return $this;
        }

        public function getBeer() {
            return $this->beer;
        }

        public function ice( snowflake $snowflake=null
                           , $manyToMany = null ) {
            if ( $snowflake instanceof 
                 \frosty\frostyBundle\Entity\Beer ) {
                $methods=get_class_methods( get_class( $snowflake ) );
                $methods=array_filter( $methods
                                     , function( $item ) use ( &$methods ) {
                                           next( $methods );
                                           if ( "__" == substr($item 0,2)) 
                                               return false;
                                           if ( "remove" == substr($item,0,6))
                                               return false;
                                           if ( "get" == substr($item,0,3)) 
                                               return false; 
                                           return true;
                                        } );

                $amethods=array_filter( $methods
                                      , function( $item ) use ( &$methods ) {
                                            next( $methods );
                                            if ( "set" == substr($item,0,3)) 
                                                return false;
                                            return true;
                                       } );

                $methods=array_filter( $methods
                                     , function( $item ) use ( &$methods ) {
                                           next( $methods );
                                           if ( "add" == substr($item,0,3)) 
                                               return false;
                                           return true;
                                       } );

                foreach ( $methods as $k => $v ) {
                    $this->{$v}( $snowflake->{str_replace( "set"
                                                         , "get"
                                                         , $v )}() );
                }

                foreach ( $amethods as $k => $v ) {
                    if ( $manyToMany ) {
                        $results=$snowflake->{str_replace( "add"
                                                         , "get"
                                                         , $v )}();
                        foreach ( $results as $key =>$value ) {
                            $this->{$v}( $value );
                        }
                    }
                }
                $this->setIceid( $beer>getId() );
            }
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM