Mysql
 SQL >> Database >  >> RDS >> Mysql

Scrivere query UNION in CakePHP

Troppi sviluppatori si chiudono in gabbia con qualunque cosa un framework esponga. Non farlo — sfrutta ciò che ti offre. Se la funzionalità di cui hai bisogno non c'è, hai due strade:

  • Estendere una classe per aggiungere tu stesso la funzionalità

oppure

  • Scrivere codice personalizzato secondo le convenzioni del framework per ottenere ciò che ti serve

Troppo spesso le persone cercano di infilare un paletto quadrato in un buco rotondo e finiscono per fare montagne di lavoro extra che non fa che ingarbugliare il codice. Fai un passo indietro e chiediti perché hai adottato quel framework. Impone una struttura a un linguaggio non strutturato e ti dà una base solida e riutilizzabile su cui costruire — non è pensato per essere una gabbia.

AGGIORNAMENTO: dopo aver passato un momento su Complex Find Conditions, ho trovato la tua risposta:

$joins = array( array( 'table' => 'test_twos', 'alias' => 'TestTwo', 'type' => 'LEFT', 'conditions' => array( 'TestTwo.id = TestOne.id', ) ), array( 'table' => 'test_threes', 'alias' => 'TestThree', 'type' => 'LEFT', 'conditions' => array( 'TestThree.id = TestOne.id', ) ) ); $dbo = $this->getDataSource(); $subQuery = $dbo->buildStatement( array( 'fields' => array('*'), 'table' => $dbo->fullTableName($this), 'alias' => 'TestOne', 'limit' => null, 'offset' => null, 'joins' => $joins, 'conditions' => null, 'order' => null, 'group' => null ), $this->TestOne ); $query = $subQuery; $query .= ' UNION '; $joins = array( array( 'table' => 'test_twos', 'alias' => 'TestTwo', 'type' => 'LEFT', 'conditions' => array( 'TestTwo.id = TestOne.id', ) ), array( 'table' => 'test_threes', 'alias' => 'TestThree', 'type' => 'RIGHT', 'conditions' => array( 'TestThree.id = TestOne.id', ) ) ); $dbo = $this->getDataSource(); $subQuery = $dbo->buildStatement( array( 'fields' => array('*'), 'table' => $dbo->fullTableName($this), 'alias' => 'TestOne', 'limit' => null, 'offset' => null, 'joins' => $joins, 'conditions' => null, 'order' => null, 'group' => null ), $this->TestOne ); $query .= $subQuery; pr($query);