我使用两个实体 Beverage 和 Beer。每个都有自己的实体管理器。啤酒是冰镇的,饮料是冷冻的。我的策略是获取这两种类型的实体的列表,然后在饮料方面将它们配对,然后将它们配对。单个实体也存在类似的功能。但是,如果您处理实体列表,则需要批量处理,否则您将有一个额外的查询FOR EACH ENTITY ON THE LIST。这是不好的。
以下已经,我相信所有非必要的部分都被剥离了。这是一种痛苦而麻烦的方法。我可以通过采用基于模式的方法来放弃它,因为您可以在学说规范中指定模式。但是,这段代码确实为我完成了这项工作。
你已经被反复警告过,(实际上是在其他问答对中被我警告过。)
一个超级控制器,你的控制器应该扩展它以获得婚姻功能。
<?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() );
}
}
}