I am using codeigniter for my project and couldn't find what's wrong with my code. This is my controller code
$products = $this->products_model->getProductsQuantity($id);
$q = $warehouse_product->quantity;
object notation is working but getting error: Trying to get property of non-object
$q = $warehouse_product['quantity'];
array notation not working and getting php error: Fatal error: Cannot use object of type stdClass as array in products\controllers\products.php on line xx
Here is my model function
public function getProductsQuantity($product_id)
{
$q = $this->db->get_where('products', array('product_id' => $product_id), 1);
if( $q->num_rows() > 0 )
{
return $q->row();
}
return FALSE;
}
Please help me to fins where is the problem in my code.
You should look at this
if you are limiting your results to 1 as in your get_where instruction then return
$q->row_array() // for array
And
$q->result_array() // for object
And in Controller do this
$products = $this->products_model->getProductsQuantity($id);
$quantity = $products->quantity; // Object notation
And
$quantity = $products['quantity']; // Array notation
Related
I have a very annoying problem to solve. I have a method that searches for a code of a sale, it is:
public function findByCode(string $code, string $columns = "*"): ?Sales
{
$find = $this->find("code = :code", "code={$code}", $columns);
return $find->fetch(true);
}
When I try to call him that:
$sales = (new Sales())->findByCode(client()->code);
It shows me the following error:
Uncaught TypeError: Return value of Source\Models\Sales::findByCode() must be an instance of Source\Models\Sales or null, array returned in
How to solve this?
You are making the findByCode function restrict to return data with type Sales and when you are using return $find->fetch(true); it will return some array of that's type is not compatible with Sales so PHP is complaining here about that. so just make a new object of Sales and return it.
The code should be some thing like this:
public function findByCode(string $code, string $columns = "*"): ?Sales
{
$find = $this->find("code = :code", "code={$code}", $columns);
$result = $find->fetch(true);
return new Sales($result);
}
So the Sales class can get the data and assign them to its properties and act as a DTO or something.
I trying to paginate my JSON response but got error like this
Call to undefined method stdClass::count()
My JSON response from the Laravel API by using guzzle ......
here is my controller code
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents());
$total = $content->count();
$paginationRecord = CollectionPaginate::paginate($content, $total, '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord->data]);
}
$content = json_decode($response->getContents());
$total = $content->count();
I am not entirely sure why you think the result of json_decode would have a count method? The JSON decoding always results in a generic object (stdClass) since there's no way for the PHP interpreter to know it represents an available class.
The ->count method is available on Countable implementations (such as ArrayCollection). If you expect a Countable class, then you can either have a factory to build your object from JSON or try to cast the stdClass to ArrayCollection.
Otherwise, if your JSON data is a valid array, you can try to use
$decoded = json_decode($data, true)
meaning it will decode it to an array rather than an object, which enables you to do
count($decoded)
$content is an object, not a collection or array you can use php method count with array get $total
Please Change to
public function index()
{
$response = $this->client->get('getUserIndex')->getBody();
$content = json_decode($response->getContents(),true );
$total = count($content);
$paginationRecord = CollectionPaginate::paginate($content, $total, '15');
return view('configuration.comuserprofiles.ComUserProfilesList', ['paginationRecord' => $paginationRecord->data]);
}
I Have this Code
$tempid = DB::table('local_founders')->select('entry_by')->where('founder_id', $id)->get();
if($tempid[0] != Auth::user()->id){
return Redirect::to('founder')->with('messagetext','Record Not Found !')->with('msgstatus','error');
}
but Results in this Error:
Object of class stdClass could not be converted to int
$tempid = DB::table('local_founders')->select('entry_by')->where('founder_id', $id)->get();
returns a collection, if you need the first element, change to this:
$temp = DB::table('local_founders')->select('entry_by')->where('founder_id', $id)->first()
now $temp is an stdClass, after that you can access it easily like any other stdClass object as follows: $temp->entry_by
so I think you have to change your code as follow:
$local_founder = DB::table('local_founders')->select('entry_by')->where('founder_id', $id)->first();
if($local_found->entry_by != Auth::user()->id){return Redirect::to('founder')->with('messagetext','Record Not Found !')->with('msgstatus','error');}
i create my own function with count of data.
Here is my function.
function Test($data){
global $acc;
$count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
But i got This is error : "PHP Catchable fatal error: Object of class stdClass could not be converted to string
In This is Line $count = $acc->get_var("SELECT Count(*) FROM _users WHERE ID='$data'");
I try var_dump in function and Other ezSQL methods (query,num_rows) but i got every same error.
--
Edit:
Problem solved. I wrong posted $data
Please make sure that
$data
is not an array
its better practice to pass $acc by reference
function Test($data, &$acc){
$count = $acc->query("SELECT Count(*) FROM _users WHERE ID='$data'");
if($count == 0)
{
return true;
}else{
return false;
}
}
I'm trying to find out why I'm getting a Trying to get property of non-object. I'm not completely skilled with objects and arrays but i'm trying. Here's the code and the error message. Any ideas on how to fix this issue?
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/sitemodel.php
Line Number: 208 and 215
function getSubMenuPages()
{
$this->db->select('site_menu_structures.id');
$this->db->from('site_menu_structures');
$this->db->where('site_menu_structures.short_name', 'mainnav');
$query = $this->db->get();
$menu_id = $query->row()->id;
$this->db->select('site_menu_structures_links.id, site_menu_structures_links.short_name, is_category');
$this->db->from('site_menu_structures_links');
$this->db->where('site_menu_structures_links.menu_structure_id', $menu_id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray = $query->result();
foreach ($linksArray as $key => $link)
{
if ($link->is_category == 'Yes')
{
$linksArray->{$key}->child_links;
$this->db->select('site_menu_structures_links_children.link_name');
$this->db->from('site_menu_structures_links_children');
$this->db->where('site_menu_structures_links_children.site_menu_structures_links_id', $link->id);
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$linksArray->{$key}->child_links = $query->result();
}
}
}
}
return $linksArray;
}
My guess would be $linksArray is an array, not an object so the line
$linksArray->{$key}->child_links;
will not work. In any case, this line does nothing so why have it at all?
Where you assign a value to this "property", try this instead
$linksArray[$key]->child_links = $query->result();
"Trying to get property of non-object"
this type of errors only exist if you try to treat a variable as an object instance and you actually failed to create that instance successfully try to check this part of the code if this is really an object or not:
$linksArray->{$key}->child_links
this is on the bottom of your code.