Model on cakephp return me Array - php

Im working in the model on cakephp with this method
public function updateUserStatus($uus){
if(isset($calor["valor1"])){
$query1= $this->query("SELECT time from users where time = "'.$valor["valor1"].'";");
return "test".$query1;
}
}
But when I return "test".$query1 ; the view show me testArray
My question is How can i Get the value time from the query?
thank for your help

That's because you're concatenating a string with an array, and PHP will change the array to a string, which will be Array. $query1 is an array containing your data, so just do:
return $query1;
And access your fields with the variable that will store the returned value.
If you want 'test' to be associated with $query1, then it's better to:
return array('test' => $query1);

Related

Find values ​in the database starting from a session id

I need display the value in the ga845_clients table ​​in the atacado column, that have 's' or 'n' recorded, but they return blank.
Then when I call <?php echo $this->session->userdata('usu_id');?> in view, the ID is displayed.
I have checked that usu_id is the session ID and the same value from id of table ga845_clientes.
I created the following codes:
Model (Cliente_model.php):
public function getAtacadista($id) {
$this->db->select("atacadista");
$this->db->where('id', $this->session->userdata("usu_id"));
$query = $this->db->get('ga845_clientes');
return $query->result();
}
Controller (Cliente.php):
public function getAtacadista() {
$this->load->model('cliente_model');
$this->load->view("carrinho", $data);
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
}
View (carrinho.php)
echo 'test1:' .$data['atacadista'];
echo 'test2:' .$atacadista;
Remember, $data['atacadista'] dont exists on view, only $atacadista
Also move down the " load view " line, as everton suggested, they are swapped and $data need to be before.
And lastly, as a tip, session userdata can contain a bunch of info, and you should pass the name of the session, not of the value, maybe usu_id is a value inside an array, not the array itself.
You can try the following in your controller
public function getAtacadista() {
$this->load->model('cliente_model');
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
$this->load->view("carrinho", array("data"=>$data));
}
You should place the $data variable assign before the load view.
Because you are not sending your data to the view. change the sequence of your statements.
public function getAtacadista() {
$this->load->model('cliente_model');
$data['atacadista'] = $this->cliente_model->getAtacadista($id);
$this->load->view("carrinho", $data);
}

Updating multiple id from array list with eloquent

I've passed to the controller an array of id and it is collected inside the student variable. I want to update the database column "lecture_id_FK" for each id in the array. I'm not sure as to how to use the array id to find the students. New in laravel.
Controller
public function setLecture($lecture,$student)
{
$students = student::whereIn('student_id', $student)->get();
$students->lecture_id_FK = $lecture;
$students->save();
//if i type "return $student" will produce -> ai160064,ai160065
}
The whereIn method takes an array as the second argument. You can get all students by using the explode function. Following getting all the records you want to update, you can do an update on all of them with the update method in laravel. With that you might be left with some code like the following:
public function setLecture($lecture,$student)
{
$studentIds = explode(',', $student);
return student::whereIn('student_id', $studentIds)
->update(['lecture_id_FK' => $lecture]);
}

Before fetching data from DB checking row null or not

I fetching data from DB and passing to Google Map. But sometimes in address table, lat and lng rows are being empty. So if it happens and I use that half empty array Google Map is crushes. So when I fetching data in Controller is there any option like; if row empty go next row... For example:
public function index()
{
$Data = DB::table('allestates')->whereNotNull('lat')->whereNotNull('lng')->get();
return view('home', 'Data');
}
Of course this is not proper code but, I just want to show what I am trying. Is this possible?
You can use whereNotNull:
public function index()
{
$Data = DB::table('allestates')->whereNotNull('lat')->whereNotNull('lng')->get();
if (!empty($Data)){
}
}
The filter you used (where('lat','lng')) actually means "records where the field lat has the value 'lng'".
you can add check if a field is not null by using below code:
$Data = DB::table('allestates')
->whereNotNull('lat')
->whereNotNull('lng')
->get();
if (!empty($Data)){
}

How to add a new key in result of codeigniter model return result()

I have a function in my model that get all company details I am fetch this
I have to add a new key city_name in my return result() how can I add ?? I am very confused but I am not get any useable example.
function xxxx($search=array(),$page,$limit){
$this->db->select("*");
$this->db->from("xxx");
$this->db->join("xx","xx.xx=xxx.xx");
$this->db->limit($limit,$page);
$company_data = $this->db->get();
if($company_data->num_rows()){
return $company_data->result();
}
else{
return 0;
}
}
Change the following line:
return $company_data->result(); // It return Stc Class Object
to
return $company_data->result_array(); // Now it returns an array
Now you can use array_push() function, it inserts one or more elements to the end of an array.
Or simply use:
$company_data['index'] = value;
Keep your model same return $company_data->result().
In your controller, you need to iterate it for the number of results you get and add new key to your objects.(result() method returns Standard Class Object).
function yourControllerMethod()
{
$company_data = $this->your_model->xxx(search_array, $page, $limit);
if(!empty($company_data)
{
foreach($company_data as $cd)
{
$cd->city_name = "pune"; // or anything you want
}
}
var_dump($company_data);
}
Try this to add new key to your results.
Edit:
You asked that you don't have city_name key in your object.
Yes, you don't have. The above code adds a key in your object.
Just like array.
$temp = array();
$temp["id"] = 1;
In this code, initially the array is empty, the next statement add the id as key in the array.

Codeigniter how to map a class sent to a model

I have a SectorModel with this function:
public function update(Sector $sector) {
$this->db->where('sector_id', $sector->getScetor_id());
return $this->db->update(_SECTOR_, $sector);
}
There are times that I’ll change only the name of the Sector object:
$Sector = new Sector();
$Sector->setSector_name = 'test';
$this->SectorModel->update($Sector);
The generated select looks like:
UPDATE realestate_sector SET sector_name = 'teste', sector_description = NULL
It will update but will set all other properties to NULL because it was not set on my object.
Right now, I have to fill the whole object before sending it.
Is there a way to map the Sector class and update only what was sent on the object?
Thanks in advance for any help.
Sorry for any typos, my English is not good =)
Just loop through all your object's properites and then if any is NULL just drop it with unset.
Here is your model's method edited to achieve that:
public function update(Sector $sector)
{
foreach($sector as $k=>$v)
{
if($v === NULL)
unset($sector->$k)
}
$this->db->where('sector_id', $sector->getScetor_id());
return $this->db->update(_SECTOR_, $sector);
}
Here you can find some info about iterating objects in PHP
The easiest to do this would be to rather use a array - docs here http://codeigniter.com/user_guide/database/active_record.html#update - you just create a array of all the columns with their values that you want to update and perform a $this->db->update('mytable', array('name' => 'test'), array('id' => $id)); call. This will only update the columns you specified in the First array. With the second array acting as your WHERE expression.
The only reason I can think of as to why your other values are being set to NULL is because in your example you create a new instance of the class and the other values must either have been set to nothing or are set to NULL. It would (If this is the case) be better to get a record from the table and then change and values on the populated record and pass that to the function to update.
Hope that helps.

Categories