【问题标题】:hasManyThroughrelationship return wrong SQL sentencehasManyThroughrelationship 返回错误的 SQL 语句
【发布时间】:2015-01-05 21:26:30
【问题描述】:

我想获取一些没有直接关系的表的数据。上下文如下:

//** account_table **// Normal user, who can buy any book
account_id
account_info

//** book_table **// Books
book_id
account_id
author_id

//** author_table **// Author, who can write a lot of books (they will have more functions, this is the why i choose to create other table)

author_id
book_id
author_desc

我使用的模型是:

class Account extends Eloquent implements UserInterface, RemindableInterface {
    protected $table = 'account_table';
    protected $primaryKey = 'account_id';

    public function Book()
    {
        return $this->hasMany('Book', 'account_id');
    }

    public function Author()
    {
        return $this->hasManyThrough('Author', 'Book','account_id','book_id');
    }
}

class Book extends Eloquent implements UserInterface, RemindableInterface {
    protected $table = 'book_table';
    protected $primaryKey = 'book_id';

    public function Account()
    {
        return $this->belongsTo('Account','account_id');
    }

    public function Author()
    {
        return $this->hasOne('Author', 'author_id', 'author_id');
    }  
}

class Author extends Eloquent implements UserInterface, RemindableInterface {
    protected $table = 'author_table';
    protected $primaryKey = 'author_id';

    public function Book()
    {
        return $this->belongsToMany('Book', 'author_id');
    }
}

所以要获取这些信息,我使用以下代码:

$user = Account::find(166); //user
dd($user->Author);

输出会抛出这样的错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'book.id' in 'on clause' (SQL: select `author_table`.*, `book_table`.`account_id` from `author_table` inner join `book_table` on `book_table`.`id` = `author_table`.`book_id` where `book_table`.`account_id` = 166)

似乎它正在寻找我的数据库中不存在的字段(id),但是我已经在模型中定义了所有primaryKeys。在这种情况下我应该怎么做才能获得author_desc 信息?

编辑(其他示例):

当我使用以下代码时,它会检索到相同的错误:

$user = Account::find(166);
echo Book::find(3);

【问题讨论】:

  • 我想说这里还有其他东西在起作用...我只是按照您的描述设置了一个新项目,它对我来说效果很好。
  • 我已经恢复了这个问题,因为应用程序现在相当大:/。我应该在数据库表中定义一个 FK 吗? (不仅在 laravel 中)@user1960364

标签: php mysql laravel laravel-4 orm


【解决方案1】:

您已经使用 hasOne() 方法定义了 Book 与 Author 的一对一关系,并使用 belongsToMany() 方法定义了 Author 与 Book 之间的多对多关系。相反,这些应该是反向关系。尝试用belongsTo() 方法替换belongsToMany() 方法。像这样的:

class Account extends Eloquent implements UserInterface, RemindableInterface {
    protected $table = 'account_table';
    protected $primaryKey = 'account_id';

    public function Book()
    {
        return $this->hasMany('Book', 'account_id');
    }

    public function Author()
    {
        return $this->hasManyThrough('Author', 'Book','account_id','book_id');
    }
}

class Book extends Eloquent implements UserInterface, RemindableInterface {
    protected $table = 'book_table';
    protected $primaryKey = 'book_id';

    public function Account()
    {
        return $this->belongsTo('Account','account_id');
    }

    public function Author()
    {
        return $this->hasOne('Author', 'author_id', 'author_id');
    }  
}

class Author extends Eloquent implements UserInterface, RemindableInterface {
    protected $table = 'author_table';
    protected $primaryKey = 'author_id';

    public function Book()
    {
        return $this->belongsTo('Book', 'author_id');
    }
}

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 2013-10-06
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多