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