Search code examples
phpmysqlsqlcodeigniterpropel

How do I do a Propel query with 3 tables or more?


I want to do this:

    SELECT * FROM `mydb`.`table1`
        INNER JOIN `mydb`.`table2`
            ON `table2`.`table1_id` = `table1`.`id`
        INNER JOIN `mydb`.`table3`
            ON `table3`.`id` = `table1`.`table3_id`
        WHERE `table2`.`myfield2`='string1'
        AND `table3`.`myfield3` = 'string2';

query on Propel, but can't find the right way to do it. With only two tables join() seems to work fine like this

$query = Table1Query::create()
            ->join('Table2')
            ->where('Table2.myfield2 = ?',  $string1)
            ->filterByMyfield3($string2)
            ->findOne(); 

But how does it work with three or more tables?


Solution

  • Of course it is possible. You have described the mapping in a config file. Example :

    $review = ReviewQuery::create()
    ->joinWith('Review.Book')
    ->joinWith('Book.Author')
    ->joinWith('Book.Publisher')
    ->findOne();
    
    
    $book = $review->getBook()  
    $author = $book->getAuthor();      
    $publisher = $book->getPublisher(); 
    

    http://propelorm.org/documentation/04-relationships.html