Note the upgraded forum! If you are experiencing issues logging in, you may need to reset your password which should send an email. If the email doesn't arrive, be sure to check your spam folder just in case.

find_all() results

edited December 2010 in Bug Reports
Doing a print_r on the results from this brings back a metric load of data.

How to get the succinct results from teh query where rows are objects or arrays (keyed by article id would be nice)

I've used the example as a base for a products search. So an article is a product and the categories has been multiplied for attributes.

I'm now searching via drop downs of those attributes as


$terms['pcd'] = $this->input->post('pcd'); $terms['width'] = $this->input->post('width'); $terms['diameter'] = $this->input->post('diameter'); foreach ($terms as $criteria => $term) { $to_model = $criteria.'s_to_products_model'; $this->load->model($to_model); if ( ! $term) { $results[$criteria] = $this->$to_model->find_all(); } else { $results[$criteria] = $this->$to_model->find_all(array($criteria.'_id' => $term)); } }

At the end of this I have all the products matching each piece of criteria, sweet.

But I need a way to also make the product rows returned unique across all three results[] arrays. Trying:

foreach ($results[$criteria'] as $product) { $listings[] = $product; #if this was an array of the row keyed by id I'd be golden }

Doesn't work as I hoped. My solution to this that does work is below. Feels wrong though, I can see from $this->db->last_query() that at the point marked in code the common_query in the model has selected the necessary product info already but I can't seem to get at it in $product.


$terms['pcd'] = $this->input->post('pcd'); $terms['width'] = $this->input->post('width'); $terms['diameter'] = $this->input->post('diameter'); foreach ($terms as $criteria => $term) { $to_model = $criteria.'s_to_products_model'; $this->load->model($to_model); if ( ! $term) { $results[$criteria] = $this->$to_model->find_all(); } else { $results[$criteria] = $this->$to_model->find_all(array($criteria.'_id' => $term)); } if ( ! empty($results[$criteria])) { foreach ($results[$criteria] as $product) { # last query here $product_ids[] = $product->product_id; } } } $product_ids = array_unique($product_ids); if ( ! empty($product_ids)) { $this->load->model('products_model'); foreach ($product_ids as $product_id) { $product_array[] = $this->products_model->find_one_by_id($product_id); } } print_object($product_array); #win.


So it works at least but is there a better way I can use the Fuel models?

Comments

  • edited 9:42AM
    The reason for the print_r problem is because the result objects have a reference to the CI object which creates this sort of circular reference when doing a print_r. I'd love to know away around this but haven't found one as of yet.

    To help get around that, you can use the debug_data() method on the model class. Also, for a single returned object, simply doing an echo on it will return the values() of that object (modified the toString object method).

    With regards to returning an array using a column as a key, there is a find_all_assoc and a find_all_array_assoc, where the first parameter is the column you want to use as the key for your returned array.
    http://www.getfuelcms.com/user_guide/libraries/my_model/table_class_functions
  • edited December 2010
    debug_data()!! Thanks.

    Requirements have changed and I no longer need to store the per criteria results only matches across all so will rewrite.

    I noticed in the UG for find_all_assoc() and find_all_array_assoc() it says:

    "...The second parameter, assoc_key, will fill the key value ..."

    But it's the first parameter. The descriptions are duplicate too, copy paste bug..
  • edited 9:42AM
    Thanks. I've fixed that as well as help clarify those find methods a little more.
Sign In or Register to comment.