I have an afterSave function in my model, the function executes, but in an endless loop. what could be the reason?
The function inserts the data into my DB, but in multiple rows.
thanks.
my function:
public function afterSave($insert)
{
$modelProject = projects::find()
->where(['status' => 2])
->one();
$idProject = $modelProject->pId;
$candidateforproject = new candidateforproject();
// $candidateforproject->id = 3;
$candidateforproject->idProject = $idProject;
$candidateforproject->idCandidate = $this->prId;
$candidateforproject->idQuestionnaire = $this->id;
$candidateforproject->idStatus = 0;
$candidateforproject->insert();
$answerOne = questionsgrades::findOne($this->answer1);
$grade1 = $answerOne->grade;
$answerTow = questionsgrades::findOne($this->answer2);
$grade2 = $answerTow->grade;
$answerThree = questionsgrades::findOne($this->answer3);
$grade3 = $answerThree->grade;
$answerFour = questionsgrades::findOne($this->answer4);
$grade4 = $answerFour->grade;
$answerFive = questionsgrades::findOne($this->answer5);
$grade5 = $answerFive->grade;
$grade = $grade1 + $grade2 + $grade3 + $grade4 + $grade5;
$this->degree=$grade;
$this->save(['degree']);
}
return parent::afterSave($insert);
}
I can see that you are using $this->save(); in AfterSave action. This will cause calling AfterSave() again. So here is the place with loop.
Possible fix is to use $this->update(); instead of $this->save();
but, honestly, I think you should remaster you logic architecture.
Related
I am trying to store data by using id as primary key in Laravel 8, I used following code in controller :
public function store(Request $request)
{
$job = new Job();
$job->employeer_id = Auth()->user('employeer')->id;
$job->category = $request->category_name;
$job->job_context = $request->job_context;
$job->keywords = $request->keywords;
$job->title = $request->title;
$job->vacancy = $request->vacancy;
$job->deadline = $request->deadline;
$job->employment_type = $request->employment_type;
$job->location = $request->location;
$job->gender = $request->gender;
$job->age = $request->age;
$job->responsibilities = $request->responsibilities;
$job->education = $request->education;
$job->requirements = $request->requirements;
$job->additional_requirements = $request->additional_requirements;
$job->salary = $request->salary;
$job->benifits = $request->benifits;
$job->apply_instruction = $request->apply_instruction;
$job->save();
Category::where('category_name', '=' , $request->category_name)->increment('no_jobs', 1);
return redirect('/jobs');
}
The error says that :
Trying to get property 'id' of non-object
in this line :
$job->employeer_id = Auth()->user('employeer')->id;```
Instead of
Auth()->user('employeer')->id;
use
Auth::guard('employeer')->id();
or
auth('employeer')->user()->id;
Check if you get the user first
Auth::guard('employeer')->check();
Then put your code in condition if the user object exist.
So basically you should do this
if(Auth::guard('employeer')->check()){
$job = new Job();
$job->employeer_id = Auth::guard('employeer')->user()->id;
// rest of the code
}else{
// handle as you want
}
I created a function that iterates by a if statement over a list in order to find a match, when found I wanted to return the match value, but it only happen once, the return statements are at the end of the function and the if statement.
The question is, How can I avoid that this function stops after the first match?, is there another way?, other functions that im not using?
When i run this code I get this:
Anything
Not a match
Not a match
Here is my code:
class Class1(var self: String,var tipo: String,var element: String)
var test_class = Class1("","","")
fun giver(){
test_class.self = "Anything"
test_class.tipo = "Something"
test_class.element = "Nothing"
}
class Funciones(){
fun match_finder(texto: String): Any{
var lista = listOf<String>(test_class.self,test_class.tipo,test_class.element)
var lista_de_listas = listOf<String>("test_class.self","test_class.tipo","test_class.element")
var count = -1
var variable = ""
for (i in lista_de_listas){
count = count + 1
println(count)
if (texto == i){
lista_de_listas = lista
var variable = lista_de_listas[count]
return variable
}
}
return "Not a match"
}
}
fun main(){
giver()
var x = "test_class.self"
var z = "test.class.tipo"
var t = "test.class.element"
var funcion = Funciones()
var y = funcion.match_finder(x)
var c = funcion.match_finder(z)
var r = funcion.match_finder(t)
println(y)
println(c)
println(r)
}
You have some typos in your example. You query for test.class.tipo but in your lista_de_listas you have test_class.tipo with underline. The same is true for test.class.element.
But you should consider to use a Map instead of two list to the lookup:
fun match_finder(texto: String): Any{
val map = mapOf(
"test_class.self" to test_class.self,
"test_class.tipo" to test_class.tipo,
"test_class.element" to test_class.element
)
return map.getOrDefault(texto,"Not a match")
}
getting this error in my local server. but this code is running in online
where its not showing any error. now what can i do? error show empty(count($leadcount)) this condition
if(empty(count($leadcount))){
if(!empty($leadManagements)){
$LeadSend = $this->LeadSend->newEntity();
$inqData['requirement_id'] = $Requirements->id;
$inqData['lead_management_id'] = $leadManagements->id;
$inqData['send_date'] = Time::createFromFormat('Y-m-d', date('Y-m-d'));
$inqData['lead_type'] = 'booking';
$inqData['is_active'] = 1;
$inqData['is_delete'] = 1;
$LeadSend = $this->LeadSend->patchEntity($LeadSend, $inqData);
if($this->LeadSend->save($LeadSend)){
$expression = new QueryExpression('booking_lead_count = booking_lead_count + 1');
$expression2 = new QueryExpression('lead_sent_count = lead_sent_count + 1');
$this->LeadManagements->updateAll([$expression,$expression2], ['id' => $leadManagements->id]);
//lead_type send_date
$this->notificationBuyer($this->request->data);
$this->notificationSeller($this->request->data);
}
}
} else {
/// allready send inquery
}
Surely it should simply be either if(empty($leadcount)) or if(count($leadcount) == 0).
I am trying to delete a certain item from a database depending on conditions. Here is what I have:
while ($row = mysql_fetch_array($result)) {
$now = strtotime("now");
$dateArray = date_parse_from_format("n-j-Y", $row["date"]);
$event_date = strtotime($dateArray['year'].'-'.$dateArray['month'].'-'.$dateArray['day']);
// temp user array
$event = array();
if($event_date > $now) {
//Event is in the future
$pid_check =$row["pid"];
$event["pid"] = $row["pid"];
$event["name"] = $row["name"];
$event["longitude"] = $row["longitude"];
$event["latitude"] = $row["latitude"];
$event["pavement"] = $row["pavement"];
$event["traffic"] = $row["traffic"];
$event["environment"] = $row["environment"];
$event["image_b64"] = $row["image_b64"];
$event["date"] = $row["date"];
$event["time"] = $row["time"];
$event["type"] = $row["type"];
// push single product into final response array
array_push($response["events"], $event);
} else {
$result2 = mysql_query("DELETE FROM events WHERE pid = $pid_check");
}
}
But when I try this it comes up blank, when I comment out result2 it works but doesn't delete(duh). How can I get it to delete? Sorry if this is a simple question, my knowledge of the language is not much.
I think $pid_check is getting set only if the condition in your if is TRUE.
It's not getting set for the else branch.
One option is to relocate the assignment of $pid_check before the if test.
In my AIR app i am trying to get the names from sqlite database as an ArrayCollection . Here is my code.
private function visitorName():void {
var sqlText:String = "SELECT name FROM user";
visitorNames = new SQLStatement;
visitorNames.sqlConnection = dbConn;
visitorNames.addEventListener(SQLEvent.RESULT, visitornamesResult);
visitorNames.addEventListener(SQLErrorEvent.ERROR, errorHandler);
visitorNames.text = sqlText;
visitorNames.execute();
}
private function visitornamesResult(event:SQLEvent):Array {
var result:SQLResult = visitorNames.getResult();
var namesList:Array = new Array();
namesList = result.data;
datafield3.dataProvider = namesList;
return namesList;
}
What should i do to get the results to an ArrayCollection by calling the visitorName() function?
Is it possible to get return value from a nested function?I know the visitorName function should be changed to ArrayCollection type and should declare an ArrayCollection variable inside it .. but not so sure how to proceed .. any help appreciated ..
First of all, notice that visitornamesResult is an event handler, so you won't be able to get the return value of that function.
You will want to actually put the result somewhere when you get it (in the UI? or in a model?)
Let's keep it simple, and just assign it to your datafield3.dataProvider:
private function visitorName(): { ... /* same as before */ }
private function visitornamesResult(event:SQLEvent):void {
var result:SQLResult = visitorNames.getResult();
// this is how you create an ArrayCollection with a provided Array
var visitors: ArrayCollection = new ArrayCollection( result.data );
datafield3.dataProvider = visitors;
}