Suppose we have two tables: The author table and the book table. One author has multiple books. They are a one-to-many relationship. Now we want to get a list of books, and the list should contain the author of the book. Generally, a newbie performs the following operations: {code...} the above case will execute a query to retrieve all databases...
Suppose we have two tables: The author table and the book table. One author has multiple books. They are a one-to-many relationship. Now we want to get a list of books, and the list should contain the author of the book. The procedure is as follows:
$bookList = $db->query('select * from books');foreach($bookList as &$v) { $v['author'] = $db->query('select name from author where id =' . $v['author_id']);}
In the above case, a query is executed to retrieve the books on all database tables, but each book is executed to retrieve the author. If we have 25 books, we will perform 26 queries. This will bring a great deal of cost to the database. we should try to make a simple query.
The general solution isjoinOne SQL statement completes the above tasks.
I found that the ORM of this framework also made this low-level error when using the link query provided by thinkphp3.2.
class IndexController extends Controller { public function index(){ D('book')->relation(true)->select(); }}
Open the debugger and find that each book uses an SQL statement to query the author.
Is the framework designed in this way, and can only be solved by hand join?
Reply content:
Suppose we have two tables: The author table and the book table. One author has multiple books. They are a one-to-many relationship. Now we want to get a list of books, and the list should contain the author of the book. The procedure is as follows:
$bookList = $db->query('select * from books');foreach($bookList as &$v) { $v['author'] = $db->query('select name from author where id =' . $v['author_id']);}
In the above case, a query is executed to retrieve the books on all database tables, but each book is executed to retrieve the author. If we have 25 books, we will perform 26 queries. This will bring a great deal of cost to the database. we should try to make a simple query.
The general solution isjoinOne SQL statement completes the above tasks.
I found that the ORM of this framework also made this low-level error when using the link query provided by thinkphp3.2.
class IndexController extends Controller { public function index(){ D('book')->relation(true)->select(); }}
Open the debugger and find that each book uses an SQL statement to query the author.
Is the framework designed in this way, and can only be solved by hand join?
In this case, it is best to use join to solve multiple-to-one queries. Furthermore, it is really not good to use tp-related queries...
The method provided by the framework for operations on a single database is relatively simple, but it is recommended to write it yourself if complicated SQL queries are involved.
Is there a PHP framework or DB class that supports better Association queries.
Isn't it possible to create a view ??
M('book')->join('user on book.auther_id = user.id')->select();
You can perform the combined query in this way.