I got two zend forms which belongs to one table in the database, broke it up in order to make sure the user does not find it lazy to finish it but I have a problem with my code in my model. I keep getting this error even though I tried numerous solutions.
Fatal error: Call to a member function save() on a non-object
public function smmedetailssmmedetails($companyname, $companytradingname)
{
$data = array(
'companyname' => $companyname,
'companytradingname' => $companytradingname,
);
return $this->insert($data);
}
public function smmesdetails2smmedetails($smmeid, $numberemployees, $ownership)
{
$row = $this->find($smmeid)->current();
$row->numberemployees = $numberemployees;
$row->ownership = $ownership;
return $row->save(); //problem lies on this line
}
Tried using the following codes/methods
$this ->row->save();
$row->save();
$this ->row->save();
return $this -> row->save();
According to the error you're getting, none of the method invocations you are trying will work.
You should first check what
$this->find($smmeid)->current();
returns using either a debugger, var_dump or print_r.
In your case, it seems that either null or something that is not an object is returned.
This means, that $row is not an object (should be), but probably is null. This means that find() method didn't get any result from database.
You should check $smmeid value and check if a such a row exists in the database.
Use var_dump function to check any variable in any line. This is how you can debug your problem.
Hey guys I got it using this method below, I marked codeblur answer because it was close thanks for the print_r information:
Example #1 print_r() example
<pre>
<?php
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
print_r ($a);
?>
</pre>
return ($row);//correct answer
Related
I'm trying to figure out a way to call a method of a specified member (Class A) coming from an array of members in class B.
<?php
class A
{
public function do_something()
{
echo "class A did something";
}
}
class B
{
private $arr = array();
private $current_index = 0;
public function add_new_A()
{
$new_a = new A;
array_push($this->arr, (object) [
$this->current_index => $new_a
]);
$this->current_index++;
}
public function get_an_A_by_index($index)
{
return $this->arr{$index};
}
public function do_something_with_A_member_inside_array($index)
{
self::get_an_A_by_index($index)->do_someting();
}
}
$b = new B;
$b->add_new_a();
$b->add_new_a();
echo print_r($b->get_an_A_by_index(0));
echo "\n";
$b->do_something_with_A_member_inside_array(0); // returns error
// console:
// stdClass Object ( [0] => A Object ( ))
// Uncaught Error: Call to undefined method stdClass::do_something();
?>
To wrap things up, I want to know if my approach is considered bad and/or if there is something I can do to fix the error. Before you disagree with my code, take a look at my php code I'm actually working on. That's all for my question.
Now about why I want to call a method of a member A inside . For my assignment I want a program that does something seperately with the method do_something of class a. So far, the only way to do that is by storing seperate members of A.
I'm not sure if I'm approaching this wrong or not, because I'm coming from Java. So, The first thing I came up with was the approach shown above. When I do this in Java, it works fine. But php is different from Java and I'm still learning how php works since I'm new to it.
Your code isn't far off, you've just got an issue with the way you're building up the collection of A objects.
array_push($this->arr, (object) [
$this->current_index => $new_a
]);
This is creating a data structure that I'm pretty sure isn't what you expect. You'll end up with an array full of stdClass objects, each with a single A member and its own internal index:
Array
(
[0] => stdClass Object
(
[0] => A Object
(
)
)
)
You're then retrieving the stdClass object and trying to run the method on that, hence the Call to undefined method stdClass::do_something... error you're seeing.
Instead, all you need to do is this:
$this->arr[$this->current_index] = $new_a;
The rest of your code is just expecting an array of A objects, nothing nested any deeper.
I've put a full example here: https://3v4l.org/ijvQa. Your existing code had a couple of other typos, which are also fixed. You'll spot them easily enough if you turn on error reporting.
I'm sorry, this is yet another Trying to get a property of a non-object-question...
Here's some code and I just can't seem to figure out how this works (or not works rather):
$b = Model_Artist::query()->where('id', 18)->get_one(); // Fuelphp ORM query, returns \Orm\Model object
var_dump($b); // output: object(Model_Artist)[46] ... etc.
// definitely an object
var_dump($b->id); // [Error: Trying to get property of non-object] output: '18'
// umm ok, so maybe no object?
var_dump(is_object($b)); // output: bool(true)
// no, no, it is an object!
var_dump($b->id); // [Error: Trying to get property of non-object] output: '18'
// make up your mind, it's no object after all?
if ( is_object($b) ) {
var_dump($b->id); // output: '18' [No error!!]
}
// WAT?? It is only an object inside the if statement?
So the question is: why does PHP say that I try to get a property of a non-object in the first two cases, while is_object is true. And why does that suddenly change inside the if statement?
Either PHP is funny, or I'm doing something really really wrong?
$b is an array of objects. and so $b->id is not valid but reset($b)->id is.
I'm trying to create associative array of objects from row result set with member id as the key, but getting some error.
addATravelog() is just a function of the class UserLogsAndSOS(), whose objects i want in array.
Here is what I tried:
class UserArraySet {
private $arrayOfUsers = array();
function createArrayForTravelogs($result) {
While($row = $result->next()) {
if(array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
$this->arrayOfUsers[$row['id']]->addATravelog($row['title'], $row['blog']); //line 72
}
}
}
On calling createArrayForTravelogs() from the object I got the following error
Here is the error I got:
Notice: Undefined index: 1 in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Fatal error: Call to a member function addATravelog() on a non-object in C:\xampp\htdocs\site\classes\userprofile.php on line 72
Can someone please let me know how to achieve this, I want something like this:
Array (
[1] => objectUserLogsAndSOS1
[5] => objectUserLogsAndSOS2
....
)
where key is the member id from $row.
I also need to check if the key exists, then call a function of that particular object to add data to its member, if not then create an object and then call a function of that particular object to add data to its member.
Thanks
just read the error message: you only create UserLogsAndSOS if there is already an entry - otherwise you call addATravelog on null.
maybe you forgot the "!" in your if clause?
Because the array stays empty.
You only create a new UserLogsAndSOS when there already is an element with the provided ID in the arrayOfUsers. The exact opposite of what you probably wanted.
You're probably missing a ! to reverse the array_key_exist result.
if(!array_key_exists($row['id'], $this->arrayOfUsers)) {
$this->arrayOfUsers[$row['id']] = new UserLogsAndSOS();
}
You missed and '!' I think this is causing the error
In the building process of an array I'm trying to call a function, checkIfNodeExists(). PHP executes this function and gives me the expected result but he still gives me a "Notice", and I don't want any kind of errors in my code.
function checkIfNodeExists($input) {
if (isset($input)) {
return (string) $input;
} else {
return 'null';
}
}
$array['sections'][] = array (
'ident' => $section->attributes()->ident,
'title' => $section->attributes()->title,
'objective' => checkIfNodeExists($section->objectives->material->mattext)
);
Notice: Trying to get property of non-object in /var/www/OLAT_Connection/QTI-XML-Parser.php on line xx
Now, if I check if "objective" exists OUTSIDE the array, PHP doesn't give me a notice. But this results in more lines of code because I have to work with an extra variabele and more IF-structures and so on...
Is there any other possibility without adding too much lines of extra code?
The problem is that when you call checkIfNodeExists you send it a value, and by sending it the value you also execute the value. So isset() will work on the result of the expression $section->objectives->material->mattext and not the expression itself.
This would work:
$array['sections'][] = array (
'ident' => $section->attributes()->ident,
'title' => $section->attributes()->title,
'objective' => isset($section->objectives->material->mattext) ? (string)$section->objectives->material->mattext : 'null'
);
It seems that $section->objectives->material->mattext is not properly defined. I would start looking there to see if you have errors in the initialisation of the object.
It would be better if you posted more code up so we can see what exactly is going on in this script.
The solution may require more code (albeit unlikely in this case), this is by no means a bad thing. Less code is not necessarily better or more efficient! Obviously it goes without saying that fewer lines of code will (most of the time) execute faster, but this does not make it more secure or efficient
UPDATE
You may be able to simply do this instead of calling another function:
'objective' => isset($section->objectives->material->mattext) ? (string)$section->objectives->material->mattext : null
I have not tested this and cannot remember whether you can place conditional statements inline, so cannot be sure whether it will work but if it does then this will be more efficient, and it is less code!
if you add "#" when you call the function the error aren't show
function checkIfNodeExists($input) {
if (isset($input)) {
return (string) $input;
} else {
return 'null';
}
}
$array['sections'][] = array (
'ident' => $section->attributes()->ident,
'title' => $section->attributes()->title,
'objective' => #checkIfNodeExists($section->objectives->material->mattext)
);
Define $section->objectives->material->mattext
I am trying to extract ONLY the PlanDetails where PlanDetail.company_id = Company.id AND PlanDetail.id' => $id.. ( you can see the conditions in my controller below)..
Controller:
function pd_list_by_company($id = null) {
$this->recursive = 2; // I am going to use containable to trim this.
return $this->PlanDetail->find('all',
array('conditions' =>
array('AND' =>
array('PlanDetail.company_id' => 'Company.id',
array('PlanDetail.id' => $id)))));
}
Test View:
$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company');
debug($planDetailsByCompany );
Output result of my debug??
Array()
If I remove the conditions and just have the find all, I get all PlanDetails as expected, so I know the data is being passed.. SQL debug dump even shows the query:
WHERE ((`PlanDetail`.`company_id` = 'Company.id') AND (`PlanDetail`.`id` IS NULL))
And yes, I did notice the $id is NULL, and I know the value needs to be there.. So maybe my question is why is the $id value not being passed to the controller even though I can see the PlanDetail.id value on a find('all') w/ out the conditions??
Thanks for any tips.
Since $id seems to be null, I would assume that you call the function without the parameter. And you don't get an error message, because as far as PHP is concerned the parameter is optional. In this case it's clearly required, so you should make it a required parameter in your function declaration:
function pd_list_by_company($id) {
Also you could simplify the return statement, you do not need the AND:
return $this->PlanDetail->find('all',
array('conditions' =>
array('PlanDetail.company_id' => 'Company.id','PlanDetail.id' => $id)
)
);
To answer the question why is the $id not being passed is because you're not passing it
To pass say $id of 2 you need to do the following in your requestAction
$this->requestAction('/planDetails/pd_list_by_company/2');
Seems to me that your code should just be
return $this->PlanDetail->find('array('PlanDetail.id' => $id));
Assuming you have the $this->PlanDetail->recursive flag set to > 0, your Model should already know about and return the associated data for any 'Company' table.....
I'm used to an old (1.3) version of CakePHP but the find() function is pretty basic and is designed to only return one row.
and yes, you definitely need to call the function with the id appended to the url, eg.
$planDetailsByCompany = $this->requestAction('/planDetails/pd_list_by_company/999');