codeigniter unit testing queries - php

I'm new to codeIgniter and unit test, and i want to test this query that return a multidimentionnal array
public function get_ficheFrais($selected_visiteur_np) {
$query = $this->db->select('ff.mois')
->from('fichefrais ff')
->join('utilisateur u', 'u.id = ff.idVisiteur', 'inner')
->where('ff.idVisiteur', $selected_visiteur_np)
->where('ff.idEtat', 'VA')
->get();
return $query->result();
}
this is the var_dump of the query:
array (size=1)
0 =>
object(stdClass)[42]
public 'mois' => string '201211' (length=6)
but i can't find out how to write it in the test function. i've tried this:
$this->visiteur_model->get_ficheFrais('a131');
$data['ficheFrais'] = $this->visiteur_model->get_ficheFrais('a55');
$this->unit->run($data['ficheFrais'], (array ('mois'=>'201211')), "testing get_ficheFrais function");
$this->load->view('visiteur/v_tests');
can anyone help or has a better solution on how to test queries that return arrays?

I think were you are going wrong is you are expecting your get_ficheFrais function to return an array but its actually returning an object. If you want it to return an array you want to change it from
return $query->result();
to
return $query->result_array();
So bearing in mind it returns an object at the moment you want to change your unit test to
$this->unit->run($data['ficheFrais']->mois, 201211, "testing get_ficheFrais function");

Related

count() parameter must be an array or an object when there is no record in the database

I am using Codeignator, I have a function in the model which is
function getList()
{
$result = $this->db->where(['members.member_type'=>1,'members.is_Approved'=>1,'members.is_status'=>1])
->order_by("members.member_id", "desc")
->from('members')
->join('admin_users','admin_users.admin_user_id = members.created_by')
->get()
->result();
if($result)
{
return $result;
}
else
{
return 0;
}
}
Above code, I am using to display the all the list on view page which is working. Now I have one more view page and I am displaying the total number of the users using the same code. So I added the code
$data['primary_data'] = count($this->List_model->getList());
print_r($data['primary_data']);
It's also working but when there are no records then I am getting the error
count() parameter must be an array or an object that implements countable in codeigniter
Would you help me out in this?
CodeIgniter's $this->db->result() method returns the query result as an array of objects, or an empty array on failure.
Just return the result as it is. If there are results it will return an array of objects. If no result found, it will return an empty array. So the counting on the result will work all the time.
function getList() {
return $this->db->where(['members.member_type'=>1,'members.is_Approved'=>1,'members.is_status'=>1])
->order_by("members.member_id", "desc")
->from('members')
->join('admin_users','admin_users.admin_user_id = members.created_by')
->get()
->result();
}
You can return empty array instead of 0 in getList() method.
return [];
can write this:
return $result ? $result : [];

covert array of object into a single object php

This is a headache issue for long time. I have the following code in codeigniter php active record.
$this->db->select('*');
$this->db->from('orders');
$this->db->join('order_detail', 'order_detail.order_id = orders.o_id');
$this->db->join('profiles','profiles.id = orders.buyer_id','left');
$this->db->where('orders.o_id', $order_id);
$this->db->group_by('orders.o_id');
$query = $this->db->get();
$order_details_by_order_id_result = (object)$query->result();
result
var_dump($order_details_by_order_id_result);exit; //see below
object(stdClass)[34]
public 0 =>
object(stdClass)[37]
public 'xx_id' => string '13' (length=2)
public 'yy_iud' => string '22' (length=10)
public 'order_total' => string '25.00' (length=5)
public 'shipto_fname' => string 'dan' (length=3)
public 'shipto_lname' => string 'theman' (length=6
on my controller i called the above function as follow:
$order_details = $this->orderdetails->get_orderdetail_of_buyer($oid);
$data['order_details'] = $order_details; //pass this to the view
$this->load->view('dashboard/order_detail_view',$data);
and i want to send the result to the view (/order_detail_view.php)
Order #o_id ;?>
$order_details->o_id; //why on earth this expression show me error
i guess the problem is
object(stdClass)[34]
public 0 =>
How could i ever solve this because i fetch only one order at a time. Thanks
row() method will return a single row for you query.
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$row = $query->row();
echo $row->xx_id;
}
In CodeIgniter, $query->result() returns an array of objects, each element represents the row in the result set. No need to convert it into object; use...
$result = $query->result();
if ($result) {
$order_details_by_order_id_result = $result[0];
}
... instead.
For your original code, there's actually no way to get the content object: PHP objects that are results of conversion from array do not play nicely with numeric keys (you can read more about this here).

What would cause CI to falsely return null values?

I'm running this in CI, and $query->result_array() is returning null values, however, when I use $this->db->last_query(), and print out the query, then run it in a MySQL client (Sequel Pro), I get values back as I would expect. What would cause this to return NULL when doing it through CI? I've got other queries that look very similar and work just fine, but this one's being fussy.
Here is the code for the query:
public function get_current_eval($pers_id = NULL, $assID = NULL){
$query = $this->db->select('person.last_name, person.rest_of_name, person.id')
->from('person_course_conceptual_assessment AS pcca')
->join('person', 'person.id = pcca.faculty_id')
->where('pcca.assessment_id', $assID)
->get()
->result_array();
return $query;
}
This is what it's returning
Array(
[0] => Array
(
[last_name] =>
[rest_of_name] =>
[id] => 000000000
)
)
Here's what $this->db->last_query() returns:
SELECT person.last_name, person.rest_of_name, person.id
FROM (person_course_conceptual_assessment AS pcca)
JOIN person ON person.id = pcca.faculty_id
WHERE `pcca`.`assessment_id` = '123456'
I copied that straight to the MySQL GUI and pasted it just as a normal query, runs fine, and returns correct values for all 3 params. Any ideas as to what would cause this for this specific instance?
Try some thing like
Check can get $variables like $assID
public function get_current_eval($pers_id, $assID){
$this->db->select('p.last_name, p.rest_of_name, p.id');
$this->db->from($this->db->dbprefix .'person_course_conceptual_assessment pcca', 'left');
$this->db->join($this->db->dbprefix . 'person p', 'p.id = pcca.id', 'left');
$this->db->where('pcca.assessment_id', $assID);
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return false;
}
}

How to use "where in" method in codeigniter?

Hello Everybody i have problem with my program i need check multi number in my database but when i test it just show only one result my code :
/*in mt View*/
$data = array(
'name' => 'search_id',
'id' => 'search_id',
'placeholder' => 'numbers_test',
'autofocus' =>"autofocus",
'rows' => '20'
);
echo form_textarea($data,set_value('search_id'));
/* in my model */
$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));
return $this->db->get();
i waiting your help for this problem
If you are getting input as comma separated ids like in string 1,5,4,8 etc from $this->input->post('search_id') then update your code like this
/* in my model */
$this->db->select('*');
$this->db->from('personal_info');
// Explode string into array to make where_in clause work
$this->db->where_in('p_id', explode(',', $this->input->post('search_id')));
return $this->db->get();
as official docs suggest you need to provide array of options in IN clause
You have to return the result of the query. Make changes,
/* In my model */
$this->db->select('*');
$this->db->from('personal_info');
$this->db->where_in('p_id', $this->input->post('search_id'));
$query = $this->db->get();
return $query->result_array(); // You've to return the result of the query
Also as #Saqueib said in comments, try some debugging when in doubt,
echo $this->db->last_query(); exit;
OR
echo '<pre>'; print_r($query->result_array()); exit;

How do I pass data from my model to my view as an object and as an array?

I have the following query, which returns a single row.
MODEL:
function get_guide_first(){
$this->db
->select('*')
->from('guides_entries')
->order_by("guide_name", "asc")
->limit(1, 0)
->join('guides_content', 'guides_content.guide_id = guides_entries.guide_id');
$query = $this->db->get();
return $query->result_array();
}
Here is my controller:
CONTROLLER:
public function get_first_guide(){
$page_data['guide']=$this->guides_model->get_guide_first();
$this->load->view('guidepage', $page_data);
}
I then run a foreach() loop in my view (or controller sometimes).
VIEW:
foreach($guide as $row){
echo $row['guide_content'];
$activecat=$row['guide_slug'];
}
Now what I would ideally like to do is return an array back from my model but even using:
return $query->row();
returns an object rather than an array.
1)How can I return this row as a 2d array?
e.g. so that I could echo a result in the controller as guide['col_name'] rather than guide()->col_name.
2)how would I pass the object to my view so that I could use $guide->col_name from there (I would need to pass this along eith the $page_data array)
If i'm reading your question right: You can use row_array instead of result_array
I think:
return $query->result_array();
returns an
array(
0 => array(
'blah' => 'blah',
'smile' => 'smile'
)
);
to return just an associated array.
row array will return
array(
'blah' => 'blah',
'smile' => 'smile'
)
Use row_array() in your model.. that will retrun associated array.
function get_guide_first(){
$this->db
->select('*')
->from('guides_entries')
->order_by("guide_name", "asc")
->limit(1, 0)
->join('guides_content', 'guides_content.guide_id = guides_entries.guide_id');
$query = $this->db->get();
return $query->row_array();
}

Categories