-2

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%

Tomas
  • 514
  • 4
  • 13
  • 37

1 Answers1

-1

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

JDurstberger
  • 4,127
  • 8
  • 31
  • 68