Change $where = array() to use IN clause

edited February 2012 in Modules
trying to change the $where expression to IN rather than = operator.

$where = array('blog_categories.id' => 1, 'blog_categories.id' => 2, 'blog_categories.id' => 3);

as

$categoryIds = array(1, 2, 3);
$where => array('blog_categories.id' => $categoryIds, 'blog_posts.published' => 'yes')

Can someone point me to the correct way using the get_posts_by_page() within Fuel_blog.php?

Comments

  • edited 8:37AM
    Perhaps try the following before calling the get_posts_by_page():
    $this->blog_posts_model->where_in('fuel_blog_categories.id', $categoryIds); $this->fuel_blog-> get_posts_by_page();
    Fuel_blog is just a wrapper around the different models (e.g. posts, categories, links etc). You can always just use the model directly if you need something else not provided by Fuel_blog.
  • edited 8:37AM
    well, let's make this simpler. how do can I change the AND to OR within the $where:

    $where = array('blog_categories.id' => 1, 'blog_categories.id' => 2);

    from

    `blog_categories`.`id` = 1 AND `blog_categories`.`id` = 2

    to

    `blog_categories`.`id` = 1 OR `blog_categories`.`id` = 2
  • edited 8:37AM
    There is an or_where_in() method on Active record you can use:
    http://codeigniter.com/user_guide/database/active_record.html
  • edited 8:37AM
    Thanks!
  • edited September 2012
    Are all the active record methods supported?

    I tried to use 'where_not_in' and recieved the following error: (using v 0.9.3)
    PHP Fatal error: Uncaught exception 'Exception' with message 'Method where_not_in does not exist.' in application/core/MY_Model.php:2322
  • edited 8:37AM
    The CI "db" object is part of the model just like a normal CI model. How were you calling it?
  • edited 8:37AM
    in my model:
    // use the Navigation model from the FUEL admin panel to identify $CI =& get_instance(); $CI->load->module_model(FUEL_FOLDER, 'navigation_model'); // #TODO: this should be made to look better, with option groups $where = array('parent_id' => 0); $CI->navigation_model->where_not_in('location', array('home','account','plan')); $nav_options = $CI->navigation_model->options_list('id','label',$where);

    the query I want to generate is:
    select id,label from fuel_navigation where parent_id = 0 and location not in ('home', 'account', 'plan');
  • edited 8:37AM
    FWIW I get the same error you see.

    Changing to:
    $CI->navigation_model->db->where_not_in('location', array('home','account','plan'));

    Stops the error, but has no effect on the query ran by options list.

    Another approach that does work:
    $nav_options = $this->navigation_model->options_list('id','label', 'parent_id = 0 AND location NOT IN ("home", "account", "plan")');
Sign In or Register to comment.