I have a strange issue; my code is working on my localhost, but when I try to use it online. It's showing an error in the return line:
Trying to get property of non-object
Here is the code:
public static function displayContenuAsString ($id,$class="traduction survol_video",$div="div") {
return "<$div id=\"contenu_$id\" class=\"$class\"></$div>".Contenu::model()->findByPk($id)->valeur;
}
No issues in the code Contenu::model()->findByPk($id)->valeur; Just check for the row in database with primary key.
According to documentation findByPk return the record found or Null if none is found. So you need to add check before using model values in this way:
public static function displayContenuAsString ($id,$class="traduction survol_video",$div="div") {
$contenu = Contenu::model()->findByPk($id);
$valeur = $contenu !== null ? $contenu->valeur : 'Empty';
return "<$div id=\"contenu_$id\" class=\"$class\"></$div>".$valeur;
}
Related
I just started using CodeIgniter 2 and probably I have overseen something obvious but I can't figure it out.
My MySQL Database has a table named Documents with a column named online_since.
Function _getMaxOnlineSince() should get the maximum online_since value and return it as a string.
In application/models/browse_model.php:
private function _getMaxOnlineSince() {
$this->db->select_max('online_since');
$oQuery = $this->db->get('documents');
return $oQuery->num_rows() > 0 ? $oQuery->row()->online_since : false;
}
And I get an error message in the Browser:
Fatal error: Uncaught Error: Call to a member function num_rows() on
boolean
because of $oQuery is false: var_dump($oQuery) returns bool(false)
var_dump($this->db->select_max('online_since')) returns object(CI_DB_mysqli_driver)#14 (73) {...}
The Codeigniter 2 application worked on the old server with PHP5.3 but after the update to PHP 7 it crashes on this point.
Why does $this->db->get('documents') return false and how can I fix it?
I could manage to get this function work with sql queries:
private function _getMaxOnlineSince() {
$oQuery = "select max(online_since) from documents";
$sMaxOnlineSinceDate = $this->db->query($oQuery);
return $sMaxOnlineSinceDate->num_rows() > 0 ? $sMaxOnlineSinceDate->row()->online_since : false;
}
But there are many more places where I use
$this->db->get('documents')
I suppose in my case CodeIgniter has trouble to evaluate this statement for some reason. I'll debug further.
You can modify your function _getMaxOnlineSince() in below manner
private function _getMaxOnlineSince() {
$this->db->select_max('id','online_since');
$res1 = $this->db->get('documents');
if ($res1->num_rows() > 0) {
return $res1->row();
}
else {
return false
}
return NULL;
}
Also please check whether the table name documents is correct. Hope this will help you
Facing this error:
ErrorException in CartController.php line 35: Trying to get property of non-object
This is the code:
public function index()
{
$this->data['details'] = Cart::content();
$this->data['shipping'] = Shipping::where('region_category_id',session('location'))->where('type',session('type_komoditi'))->first();
$regType = session('regType');
$regId = session('id_wilayah');
$qReg = RegionCategory::find($regId);
if($regType == 'children') {
$this->data['minimalWeight'] = $qReg->minimal_weight;
//$this->data['minimalBuy'] = $qReg->min_buy;
}
$this->data['minimalBuy'] = $qReg->min_buy; //this is line 35
$this->data['regType'] = $regType;
\Session::put('price',Cart::total());
\Session::put('totalPrice',Cart::total());
\Session::put('paycode',0);
return view('client.carts.index',$this->data);
}
when I delete the line 35 the error is gone, but that code is important for using minimal buy filter.
How to resolve this error?
That's error cause $qReg->min_buy return null.
You can fix by return default value like this:
$this->data['minimalBuy'] = $qReg->min_buy ?? 0;
That's mean if is null return 0
It's saying that $qReg is not an object and you are trying to get the value from it using min_buy property.
Try printing the object $qReg using print_r($qReg); and exit the script after that.
print_r($qReg);
exit();
And check whether that object has the property min_buy or not.
Why is $this->data['minimalBuy'] = $qReg->min_buy; after if block ? It's being overwritten even if if($regType == 'children') is false.
I need to return email of the user like in example:
public function getUserEmailAttribute()
{
$user = User::find($this->used_by);
return $user->email;
}
When I use $this->used_by it's throw me this error:
Trying to get property of non-object (View: /home/mertindervish/Documents/school/app/views/admin/invitation/list.blade.php)
But when I use a string like '2', '3' it's working. Is there any problem with my code? I tried to var_dump $this->used_by and it's return string(1).
It turns out that one invitation didn't have a valid used_by value.
To prevent such an error in the future, add a null check:
public function getUserEmailAttribute()
{
$user = User::find($this->used_by);
if(is_null($user)) return null;
return $user->email;
}
I do not want to be asking a question that has already been answered, however I have done a ton of research and I am stuck. Any help would be appreciated.
I am trying to query and add database results to OpenCart's addproduct.tpl
In the MODEL file I have:
public function units() { //function that gets database info and returns it
$unit_variables = $this->db->query("SELECT unit FROM ". DB_PREFIX ."
weight_class_description");
if (!$unit_variables) {
die(mysql_error());
}
else {
foreach ($unit_variables->rows as $temp_var) {
print_r($temp_var['unit'], true);
}
}
}
In the CONTROLLER file I have:
$this->load->model('catalog/product'); //where my function is located
$this->model_catalog_product->units();
$weight_variables = units();
if (isset($this->request->post['weight_variables'])){
$this->data['weight_variables'] = $this->request->post['weight_variables'];
}
In the VIEW I have:
<?php echo $weight_variables ?>
I get the following error:
Call to undefined function units() in /path/to/controller/file on line etc.
Note: When I print_r($temp_var); instead of returning print_R($temp_var, true) and delete these lines of code $weight_variables = units(); if (isset($this->request->post['weight_variables'])){ $this->data['weight_variables'] = $this->request->post['weight_variables'] } in the controller file my model file will display the query results on the addproduct.tpl
units() is a METHOD of your object, yet you're calling it as a standalone regular function:
$weight_variables = units();
^^^^^^^
Shouldn't it be:
$weight_variables = $this->model_catalog_product->units();
instead? And note that as-written, your method doesn't actually return anything, so $weight_variables will simply get assigned null..
I have two functions getCompanyDetails and getHostingDetails
The first database getCompanyDetails works fine but the getHostingDetails shows
Trying to get property of non-object
getCompanyDetails:
Controller: $data['companyName'] = $this->quote->getCompanyDetails()->companyName;
Model:
public function getCompanyDetails()
{
$this->db->select('companyName,companySlogan,companyContact,
companyEmail,companyWebsite,companyPhone,
companyFax,companyAddress');
$this->db->from('companyDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
getHostingDetails:
Controller:
$data['hostingRequired'] = $this->quote->getHostingDetails()->hostingRequired;
Model:
public function getHostingDetails()
{
$this->db->select('hostingRequired,domainRequired,domainToBeReged,
domaintoBeReged0,domainTransfer,domainToBeTransfered,
domainToBeTransfered0,currentHosting');
$this->db->from('hostingDetails');
$result = $this->db->get();
if($result->num_rows()<1)
{
return FALSE;
}else{
return $result->row();
}
}
Well, one method returns an object from $result->row() and the other false. You can't call a method on false.
false is returned when no record is found. So you need to check the return value before using it.
Well in your get functions chances is your code might return you false if there is no rows returned. You might want to check before retrieving the details. Example:
$details = $this->quote->getHostingDetails();
if($details){
$data['hostingRequired'] = $details->hostingRequired;
}
The problem is probably how you use those functions in your controller. If any of them returns FALSE, then
$this->quote->getHostingDetails()->hostingRequired;
is going to give you errors. Try
if ($row = $this->quote->getHostingDetails()) {
echo $row->hostingRequired;
}