Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用datamapper的多对多关系中第三个表字段的Where条件_Php_Codeigniter_Codeigniter Datamapper - Fatal编程技术网

Php 使用datamapper的多对多关系中第三个表字段的Where条件

Php 使用datamapper的多对多关系中第三个表字段的Where条件,php,codeigniter,codeigniter-datamapper,Php,Codeigniter,Codeigniter Datamapper,我有三张桌子- users(id,name,dob) books(id,title,author) issuedbooks(id,book_id,student_id,issue_date) 用户和图书之间的关系是多对多的,这导致了第三个表issuedbooks 我的模特是- class student extends DataMapper{ var $table="students"; var $has_many=array( "books"=>arra

我有三张桌子-

users(id,name,dob)
books(id,title,author)
issuedbooks(id,book_id,student_id,issue_date)
用户和图书之间的关系是多对多的,这导致了第三个表issuedbooks

我的模特是-

class student extends DataMapper{
    var $table="students";
    var $has_many=array(
        "books"=>array(
            "class"=>"book",
            "join_table"=>"issuedbooks",
            "join_self_as"=>"student",
            "join_other_as"=>"book",
            "other_field"=>"students"
        )
    );
}

class book extends DataMapper{
    var $table="books";
    var $has_many=array(
        "students"=>array(
            "class"=>"student",
            "join_table"=>"issuedbooks",
            "join_self_as"=>"book",
            "join_other_as"=>"student",
            "other_field"=>"books"
        )
    );
}
此表issuedbooks有如下条目-

id      student_id      book_id  issue_date
1       2               1        2013-07-18 
2       2               4        2013-07-16 
3       1               5        2013-07-18 
4       2               6        2013-07-18
现在我必须找出所有那些由id为2的学生选择的书,发行日期为2013年7月17日

我试过了,但没有结果

$student=new student();
$student->get_by_id('2');
$student->books->include_join_fields()->get();
foreach($student->books as $book):
$book->where_join_字段($student,'issue_date>,“2013-07-17”)->get()

echo$book->title.“$book->加入发行日期。”
”; endforeach;

请帮帮我,我哪里出了问题?

这是我解决这个问题的方法,我想很简单。
而且您不必在多对多表(issuedbooks)中创建ID列。
issuedbooks
book\u id
issuedbooks
student\u id
必须是主键

class SomeModel extends CI_Model
{
    public function getBooksByStudentIdAndDate($students_id, $date)
    {
        $students_id = (int) $students_id;
        $date = (date('Y-m-d', strtotime($date)) === $date) ? $date : false;

        if( ! $students_id OR ! $date)
            return array();

        $this->db->where('issuedbooks.students_id', $students_id);
        $this->db->where('issuedbooks.issued_date', $date);

        $this->db->select('
            users.name, 
            books.title, 
            issuedbooks.issued_date as date
        ');
        $this->db->join('users','issuedbooks.students_id=users.id');
        $this->db->join('books','issuedbooks.book_id=books.id');
        $this->db->order_by('issuedbooks.issued_date', 'asc');

        return $this->db->from('issuedbooks')->get()->result_array();
    }
}

我会这样做:

$student=new student();
$books = $student->where('id',2)
             ->where_related_book('issue_date','2013-07-17')
             ->get()

foreach($books as $book){
    ...

总的来说,我认为你的模型有点过于复杂了。只要遵守标准,就不需要在关系中指出联接表等

@PrateekShukla,为什么需要使用DataMapper如果CodeIgniter按照公司标准提供基本的QueryBuilder而没有任何凌乱的代码它提供了数据库错误未知列“books\u books.issue\u date”在“where子句”中选择
students
*FROM(
students
)左外连接
issuedbooks
books\u issuedbooks ON
students
id
books\u issuedbooks
左外连接
books
books ON
books\u books
id
books\u issuedbooks
id
其中
students
id“2”和
图书\u图书
发行日期>“2013-07-17”
$student=new student();
$books = $student->where('id',2)
             ->where_related_book('issue_date','2013-07-17')
             ->get()

foreach($books as $book){
    ...