php - 如何在php中创建一对多关系

标签 php mysql sql

我之前在 laravel 工作过。所以最近我搬到了核心 php,所以我正在尝试一对多关系。但它总是返回单独的记录。 我有以下表格

作者

id | username | created_by | updated_by

书籍

id | author_id | book_name | book_image |

查询

select * from authors left join books  on author.id=books.author_id

我正在看我的结果,有点像这样

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Norm

            [books] => Array
                (
                  [0] =>Array
                    (
                    [id] => 4
                    [book_name] => great wall
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )

                    [1] =>Array
                    (
                    [id] => 6
                    [book_name] =>new book
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )
                )
        )
    [1] => Array
        (
            [id] => 2
            [name] => Norm

            [books] => Array
                (
                  [0] =>Array
                    (
                    [id] => 2
                    [book_name] => amazing star
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )

                    [1] =>Array
                    (
                    [id] => 3
                    [book_name] =>way of journy
                    [created_at] => 2015-09-11 04:45:07
                    [updated_at] => 2015-09-11 04:45:07
                    )
                )

        )


but in core php i am getting  ouput


Array
(
    [0] => Array
        (
            [id] => 1
            [username] => david
            [created_by] => 
            [user_id] => 1
            [books] => book1
        )

    [1] => Array
        (
            [id] => 2
            [username] => david
            [created_by] => 
            [user_id] => 1
            [books] => book2
        )

    [2] => Array
        (
            [id] => 3
            [username] => david
            [created_by] => 
            [user_id] => 1
            [books] => book3
        )


    [3] => Array
        (
            [id] => 5
            [username] => miller
            [created_by] => 
            [user_id] => 2
            [books] => new1
        )

    [4] => Array
        (
            [id] => 6
            [username] => miller
            [created_by] => 
            [user_id] => 2
            [books] => new2
        )

)

所以我想在很多书上显示作者姓名。谁能帮帮我,我该怎么做

最佳答案

class Author extends Model
{
    /**
     * Get the books for the author.
     */
    public function books()
    {
        return $this->hasMany('App\Book');
    }
}

...

class Book extends Model
{
    /**
     * Get the atuhor that owns the book.
     */
    public function post()
    {
        return $this->belongsTo('App\Author');
    }
}

-- Laravel Documentation

关于php - 如何在php中创建一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728294/

相关文章:

sql - 为什么alter 命令被称为DDL 而不是DML?

sql - 使用某些参数查找没有子项的父记录

php - track kind div 的输出 src

php - MySQL查询使用另一个页面的查询结果 - php

mysql - 使用 mysqldump 以不同方式将一个数据库复制并覆盖到另一个数据库

php - 使用设置参数选择 'random' 结果

MySQL 更改 View 安全性

php - 如何用字典数据库解析一个词/短语与 2 个词(在 PHP 中)

php - Smarty::$_tpl_vars - 未定义属性 - Smarty 3 中支持 _tpl_vars

c# - 在 Silverlight (C#) 中访问 MySQL 数据库