Join in datamapper in codeigniter

184 Views Asked by At

How to Create INNER JOIN query in datamapper in codeigniter?

Country : id(int) | Name(varchar)

User : id(int) | name(varchar) | country_id(int)

Required Query SELECT * FROM User u INNER JOIN Country c ON(c.id = u.country_id) WHERE u.name LIKE %abcd%

1

There are 1 best solutions below

4
JDurstberger On

You have to set the properties in you Model accordingly:

class User extends DataMapper {

    var $has_one = array("country");

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}

class Country extends DataMapper {
    var $has_many = array("user");

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}

Official Doc

You then can query related models like this:

$u = new User();
$u->like_related_country('name', 'abc')->get();

You can find all query-options here