Symfony2:自定义实体?

时间:2013-04-18 20:25:46

标签: php symfony doctrine

this question中提出的问题的解决方案,其中客户端实体被分割为两个数据库 - 一个本地,一个外部,是构建本地客户端,使其包含外部客户端作为对象并具有getter / setters维护外部客户端对象。本地关系在本地客户端实体中维护。现在的问题是如何最好地创建客户端实体,以便它可以在多个地方使用。

当前客户端控制器方法

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;
}

尝试在客户端存储库中执行此操作或类似操作失败。尝试将以下服务注入独立类。这失败了,缺少关于构造函数的参数1的错误。

services.yml片段:

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

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;
    }
...
}

1 个答案:

答案 0 :(得分:1)

我使用两个实体饮料和啤酒。每个人都有自己的实体经理。啤酒是 冷淡的,饮料在冰箱里。我的策略是获得两种类型的列表 实体,并将它们配对,在饮料方面,并匹配它们。类似 单个实体存在功能。但是,如果你处理一个列表 您需要批量执行这些实体,或者您将有一个额外的查询 FOR EACH 列表中的实体。这很糟糕。

以下情况,我相信所有非必要部分都被剥离了。这是一个 痛苦而繁琐的做法。我能够通过去一个放弃它 基于模式的方法,因为您可以在doctrine orms中指定模式。 但是,这段代码确实为我做了工作。

你一再受到警告(实际上是我在其他Q& A对中)。

一个超级控制器,你的控制器应该扩展以获得结婚 功能。

    <?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;
        }
    }

和实体代码:

    <?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() );
            }
        }

    }