0

How can i do a very simple join in nette database, resulting in a query like this?

SELECT * FROM Book LEFT OUTER JOIN Author ON Book.author_id = Author.id

I have found endless tutorials on how to join combined with where clausules, order by, group by and what not, but not single tutorial on a simple join like this.

I forgot to specify, I use PHP, so i need something like $context->table('Book')->join('author');, but that of course doesn't work.

kajacx
  • 12,361
  • 5
  • 43
  • 70
  • I have tried using some condition that is always true, but that returns only data from the joined table (author in this case). And yes, translating SQL query to nette only to be translated bask seems useless, but I'm trying to invest some time that would pay of in the long run with more complicated query constructions. – kajacx Mar 05 '16 at 20:09

2 Answers2

1

You do not need join in this case, Nette provide you relationship between tables, so you can do something like this:

foreach($connection->table('book') as $book){
    echo 'Book name: '.$book->name.'<br>'
    echo 'Book autor: '.$book->author->name.'<br>' 
}

I expect then in book table there is column author_id which is foreign key for author table.

John
  • 2,494
  • 5
  • 21
  • 24
1

Question is already answerred, so I only add one note.

You can't do join using Nette Database, there is no join function. You can enforce join by referencing another table column in order for example, but it's pretty limited. If you need more complex join, you should better use Dibi instead of Nette Database.

Tomáš Jacík
  • 645
  • 4
  • 12