¿Es esto posible para unir tablas en la doctrina ORM sin necesidad de utilizar las relaciones?

StackOverflow https://stackoverflow.com/questions/2203027

  •  18-09-2019
  •  | 
  •  

Pregunta

Supongamos que hay dos tablas.

Table X--
Columns:
     id         x_value

Table Y--
Columns:
     id        x_id      y_value

Ahora no quiero para definir la relación de clases de doctrina y quiero recuperar algunos registros utilizando estas dos tablas mediante una consulta como la siguiente:

Select x_value from x, y where y.id="variable_z" and x.id=y.x_id;

Yo no soy capaz de encontrar la manera de escribir consulta como esta en ORM doctrina

EDIT:

estructuras Tabla:

Tabla 1:

CREATE TABLE IF NOT EXISTS `image` (
  `id` int(11) NOT NULL AUTO_INCREMENT, 
`random_name` varchar(255) NOT NULL,
 `user_id` int(11) NOT NULL,
  `community_id` int(11) NOT NULL,
  `published` varchar(1) NOT NULL,
  PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=259 ;

Tabla 2:

  CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
   `city` varchar(20) DEFAULT NULL,
   `state` varchar(20) DEFAULT NULL,
   `school` varchar(50) DEFAULT NULL,
   PRIMARY KEY (`id`)
 ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=20 ;

Consulta estoy utilizando:

        $q = new Doctrine_RawSql();

        $q  ->select('{u.*}, {img.*}')
        ->from('users u LEFT JOIN image img ON u.id = img.user_id')
        ->addComponent('u', 'Users u')
        ->addComponent('img', 'u.Image img')
        ->where("img.community_id='$community_id' AND img.published='y' AND u.state='$state' AND u.city='$city
        ->orderBy('img.id DESC')
        ->limit($count+12)  
        ->execute();        

error que estoy recibiendo:

 Fatal error: Uncaught exception 'Doctrine_Exception' with message 'Couldn't find class
 u' in C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Table.php:290 Stack trace: #0 
 C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Table.php(240): Doctrine_Table-   >initDefinition() #1 C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Connection.php(1127): 
Doctrine_Table->__construct('u', Object(Doctrine_Connection_Mysql), true) #2 
C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\RawSql.php(425): Doctrine_Connection-
>getTable('u') #3 C:\xampp\htdocs\fanyer\doctrine\models\Image.php(33):    Doctrine_RawSql-
>addComponent('img', 'u.Image imga') #4   C:\xampp\htdocs\fanyer\community_images.php(31): 
  Image->get_community_images_gallery_filter(4, 0, 'AL', 'ALBERTVILLE') #5 {main} thrown      in 
    C:\xampp\htdocs\fanyer\doctrine\lib\Doctrine\Table.php  on line 290
¿Fue útil?

Solución

Trate algo como esto: ---

$q = new Doctrine_RawSql();
$this->related_objects = $q->
        select('{o.name}')->
        from('tagset t1 JOIN tagset t2 ON t1.tag_id = t2.tag_id AND t1.object_id != t2.object_id JOIN object o ON t2.object_id = o.id')->
        addComponent('o','Object o')->
        where('t1.object_id = ?', $this->object->id)->
        groupBy('t2.object_id')->
        orderBy('COUNT(*) DESC')->
        execute();

Otros consejos

Puede escribir el SQL directamente a la driver.You db no se pondrá en contacto las matrices hidratadas o los objetos sin embargo. Pero viene muy bien a veces:

 $sql = "SELECT * FROM... ";        
 $pdo = Doctrine_Manager::connection()->getDbh();       
 $data = $pdo->query($sql)->fetchAll();

Puede modificar manualmente la clase de mesa en Symfony 1.4, pero esto debe hacerse con cuidado, porque no se está vinculando tablas en el nivel de base de datos que normalmente debería suceder.

class TableName extends BaseTableName
{
    public function setUp()
    {
        $this->hasOne('LinkedTableName', array(
             'local' => 'link_id',
             'foreign' => 'link_id'));
        parent::setUp();
    }
}

Posible duplicado: doctrina unirse sin relación

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top