i want to update a first record status into '2' based on button click.
Here is my code :
public function repay(Request $request,$loanid){
$obj = DB::table('schdls')
->where('schdls.loanid','=',$loanid)
->where('schdls.status','=',1)->first();
$obj->status='2';
$obj->save();
return redirect('/home')->with('message','updated');
}
But it says Call to undefined method stdClass::save() error,
What am i missing?
Check if $obj is null before assigning value on it.
Use dd/dump/var_dump function to check the value and the type of $obj after getting record from database.
Try using update(['status' => '2']); instead of save()
DB::table('schdls')
->where('status','=',1)->update(['status' => 2]);
Related
I'm using the TimestampBehaviour like this:
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'usr_date_added',
'updatedAtAttribute' => 'usr_date_modified',
'value' => new \yii\db\Expression('UTC_TIMESTAMP()'),
],
];
}
Then if access it through the authenticated user like this...
Yii::$app->user->identity->usr_date_modified;
...I get the expression instead of the actual value:
object(yii\db\Expression)#38 (2) {
["expression"]=>
string(15) "UTC_TIMESTAMP()"
["params"]=>
array(0) {
}
}
If I do it like this I correctly get the actual value:
$user = app\models\User::findOne(51);
echo $user->usr_date_modified;
Why is that? And how can I get the actual value through the identity property?
The value while not saved in the database holds the information about how this actual value should be created - in your case it uses UTC_TIMESTAMP() MySQL function that will be called at the moment of saving this object into DB.
When you are retrieving object already saved in the database it takes whatever has been saved there (in your case the result of this MySQL function), so it gives you actual timestamp.
Before saving the object there is no point of checking this value.
I realized that I have modified an attribute and then called the save() method. You would think it should then hold the actual value after calling save() but it doesn't - I guess because doing the UPDATE query the database doesn't return the value. You need to call the refresh() method (see documentation) to make another SELECT query to get the new value. So you would need to do it like this:
Yii::$app->user->identity->usr_last_access = gmdate('Y-m-d H:i:s');
Yii::$app->user->identity->save();
Yii::$app->user->identity->refresh();
I run a query using Laravel's DB first() which returns an object, when I check using dd() or vardump(). But when I try to print the value using echo ($promotion->pp_name); it gives error, but same property shows while dd($promotion->pp_name);
<?php dd($promotion->pp_name); ?> prints "urgent"
<?php echo ($promotion->pp_name); ?> but it gives "Trying to get property of non-object"
Full object dump results: <?php dd($promotion); ?>
{#196 ▼
+"ppo_id": 23
+"ppo_prj_id": 68
+"ppo_pp_id": 4
+"ppo_updated_date": "2014-05-20"
+"ppo_status": 1
+"pp_id": 4
+"pp_name": "urgent"
+"pp_dispText": "I want my project to be marked as an urgent project"
+"pp_amount": "5.00"
+"pp_updated_date": "2013-08-09"
+"pp_status": 1
}
and the function that return this object.
function getProjectPromotion($value='')
{
$project_id = $value;
$promotion = DB::table('project_promotion_option')
->join('project_promotion', 'project_promotion_option.ppo_pp_id', '=', 'project_promotion.pp_id')
->where('ppo_prj_id', '=' , $project_id )
->first();
return $promotion;
}
Are you calling this method in a loop? When you do a dd(), the script stops with the right result after the first loop run and all is good. But when you do echo in a loop, it continues and my guess is that at some point you pass some corrupted data that breaks the method.
We would need to see the code excerpt where you are calling the mentioned method to verify this hunch.
As Tadas Paplauskas suggest because the function call the query run in foreach each loop and in some iteration DB::first() method return null so echo gets error. i solved this by checking first if the value is set.
<?php if (isset($promotion->pp_name)) {
echo $promotion->pp_name;
} ?>
I do have the invitations table set up and in the database. I use it for other purpose such as adding more...
my goal: update the first record with a new value to a field:
I tried:
$invitation = new MemberInvitation();
$invitation1 = $invitation->find(1);
$invitation1->status = 'clicked';
$invitation1->save();
And also:
$invitation1 = \App\Model\MemberInvitation::find(1);
$invitation1->status = 'clicked';
$invitation1->save();
both ended with:
Creating default object from empty value
EDIT:
This piece of code worked and updated my records correctly -- I just can't do it via Eloquent's model:
\DB::table('member_invitations')->where('id', '=', $member_invitation->id)
->update(array('status' => 'clicked', 'member_id' => $member->id));
what am I missing?
Try this.
$invitation = MemberInvitation::findOrFail(1);
$invitation->status = 'clicked';
$invitation->save();
If that doesnt work, please show your model
find(1) doesn't mean "give me the first record", it means "give me the first record where id = 1". If you don't have a record with an id of 1, find(1) is going to return null. When you try and set an attribute on null, you get a PHP warning message "Creating default object from empty value".
If you really just want to get the first record, you would use the first() method:
$invitation = \App\Model\MemberInvitation::first();
If you need to get a record with a specific id, you can use the find() method. For example, to translate your working DB code into Eloquent, it would look like:
$invitation = \App\Model\MemberInvitation::find($member_invitation->id);
$invitation->status = 'clicked';
$invitation->member_id = $member->id;
$invitation->save();
The answer is obvious based on your reply from May 15th.
Your "fillable" attribute in the model is empty. Make sure you put "status" in that array. Attributes that are not in this array cannot up modified.
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
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');