phalcon MVC Model - model-view-controller

I had a problem with the mvc model updating a row.
The task was:
in table table_name set column_name = column_name + 1
If i would use the model like
model->find(...)
model->column_name = model->column_name + 1
model->save
then another script could have updated the column in the mean time:
model->find(...)
model->column_name = model->column_name + 1 // lets say loaded 9 and set to 10
// at this point another script may just loaded the old value 9
// and saved it to n.
// my current model would save to to 10 which would be wrong
model->save
To prevent that Im using the server:
$result = $this->getWriteConnection()->query(
"UPDATE `table_name` SET
`column_name` = #new_value := `column_name` + 1
WHERE ...;"
);
I then select the #new_value session var on another call.
What Im searching now is a way to do that with the mvc model.
Any way to do that?

Related

Auto Increment ID in SQL [duplicate]

This question already has answers here:
Auto Increment after delete in MySQL
(18 answers)
Closed 12 months ago.
I want to know how can I make my SQL Database table give an ID starting from 0 and with every new add to check if there is an old ID free .
Exemple :
I added 10 items then I deleted almost everyone and I kept item with ID=5.
After adding a new item , it gets id 11 not 0.
Note : I'm editing my DB with Laravel API.
SQL AUTO INCREMENT adds 1 to the biggest incrementable column value.
To fill a free integer slot you need to check for it manually, but that kinda defeats the purpose of auto-incrementing.
Anyway here is how to check for the first free ID value:
function getUnusedId($ids_array)
{
$max_id = max($used_ids); // can be 25 for example
// [1:(id|1), 2:(id|2), 3:(id|3), 4:(id|5)] =>> ID 4 is missing
for($i = 1; $i <= $max_id; $i++){
if($used_ids[$i] > $i){
return $i;
}
}
return $max_id + 1; // You can echo that all IDs are used.
}
// The IDs must be ordered for this solution
$used_ids = Model::orderBy('id')->pluck('id')->toArray();
$free_id = getUnusedId($used_ids);

add data at runtime in multiple rows in QTP/UFT

I would like to add data at runtime in multiple rows but instead it creates a new column and add data to that. I've used below code.
For i = 1 To 2 Step 1
datatable.SetCurrentRow(i)
Dim f_name,l_name
f_name=Datatable.Value("todayDate","date") + Datatable.Value("MonthName","date")
l_name=datatable.Value("This_Year","date")
Datatable.AddSheet("Emp_name").AddParameter"Emp_first_Name",f_name
Datatable.AddSheet("Emp_name").AddParameter"EMP_Last_Name",l_name
Next
AddParameter will add a new parameter each time - that's why you're seeing duplicate columns.
There's lots of ways around this - one way is to set up your table+headers before the loop then add the content with the loop.
for example:
Datatable.AddSheet("Emp_name")
Datatable.GetSheet("Emp_name").AddParameter "Emp_first_Name",""
Datatable.GetSheet("Emp_name").AddParameter "EMP_Last_Name",""
For i = 1 To 2 Step 1
datatable.SetCurrentRow(i)
Dim f_name,l_name
f_name="hello"
l_name="world"
Datatable.GetSheet("Emp_name").SetCurrentRow i
Datatable.Value("Emp_first_Name", "Emp_name") = f_name
Datatable.Value("EMP_Last_Name", "Emp_name") = l_name
Next
Output looks like this:

Codeigniter updating data with OR condition in ID

Hello I am a beginner in using Codeigniter. What I want is to update a data in Codeigniter with an OR condition to the ID.
Here is my MySQL query that I want to make happen:
Update message_received SET is_read = 1 WHERE msg_id = 3 OR parent_id = 3
I tried something like this:
$this->query->update_array('message_received', array('msg_id' => $_POST['read_my_message'],'parent_id' => $_POST['read_my_message']) , array('is_read'=>1));
You can use or_where to apply OR condition in CodeIgniter, I've written a possible solution for your question(Reference).
See if it helps you.
$msg = $this->input->post('read_my_message'); // get the post data
$this->db->set('is_read', 1); // set the value of column
$this->db->where('msg_id', $msg); // first condition
$this->db->or_where('parent_id', $msg); // second contion
$this->db->update('message_received'); // table name
// Produces:
/* UPDATE `message_received` SET `is_read` = 1 WHERE `msg_id` = '$msg' OR `parent_id` = '$msg' */
You can apply OR. The references https://www.codeigniter.com/userguide3/database/query_builder.html

Page views mysql

Codeigniter I want to increase +1 when loading page. but the query does 0
'views + 1' and I did 10 no problem.
It didn't make sense to throw the query for views.
function get_views()
{
$this->db->set('views', 'views+1');
$this->db->where('Id', 1);
$this->db->update('pages'); // gives UPDATE mytable SET field = field+1 WHERE id = 2
}
You need to pass a third parameter as false like below:
$this->db->set('views', 'views+1', FALSE);
The Third Parameter tells CodeIgniter not to protect the generated query with backticks. In your case the final query will be
UPDATE pages SET views = views + 1 WHERE Id = '1';

how to get id from one table an add another table in laravel 4

I am try to add id from one table to another table to give a relationship to the tables.
i have 2 table ..
classes.(id,cls,divn)
id cls divn
1 1 A
2 1 B
4 2 A
5 2 B
6 2 C
7 3 A
2.teacher(id teachername,tclsid,tsub)
Now i want ,im enter teachername and select class,division ,subject on select box using html tag.
if im select class and division check to classes table.and get id from selected class & division to insert into teacher table tclsid feld.
how its possible??
im trying to select multiple class and division
im using controller.php is here
$clss =implode(',', Input::get('tcls'));
$divn =implode(',', Input::get('tdivn'));
$teachers =new ForumTeacher();
$teachers ->tname=Input::get('tname');
$teachers->tmobile=Input::get('tmobile');
$teachers ->ttype=Input::get('ttype');
$teachers ->tsubject = implode(',', Input::get('tsubject'));
$cs = ForumClass::where('cls', $clss)->where('divn', $divn )->first();
$teachers->tcls= $cs->id;
but not work this code $teachers->tcls= $cs->id;` ...
i want teacher table like this
id teachername tclsid tsub
1 xyz 1,5 maths
2 poq 5,7 english
if xyz teacher select class &division are 1,2 & A,B(so id is 1,5)
if poq teacher select class &division are 2,3 & B,A (so id is 5,7)
but i got an error
ErrorException (E_UNKNOWN)
Trying to get property of non-object
error code line is
$teachers->tcls= $cs->id;
Your $teachers var is holding a new, unsaved ForumTeacher model. You have not assigned the tcls value in $teachers. So the $teachers['tcls'] is not set, Add:
$teachers->tcls = $cs->id;
I don't understand what your are trying to do with:
$clsid[$cs->id] = $teachers['tcls'];
If you set the tcls attribute of $teachers first, what you are doing is the same as:
$clsid[$cs->id] = $cs->id;
Don't forget to save the model when you are done setting its attributes:
$teachers->save();
Get id from one table an add another table in laravel 4
$cs = ForumClass::where('cls','=', $clss)->where('divn','=', $divn )->first();
$teachers->tcls= $cs->id;
Reread your question and code and from what I understand you are getting multiple class, division combinations and subject for multiple teachers. You either have to have one teacher and one cls and one divn and one subject or the same number of these corresponding to different teachers. Otherwise, I am confused by your model, so I am assuming you have one of each per teacher instance.
Then you need to iterate over the inputs and create one teacher at a time or create an array and batch create them. I'll do the iteration for simplicity of following it:
// assuming all input arrays have the same count() of elements and
// are all in the same order
$tclss = Input::get('tcls');
$tdivns = Input::get('tdivn');
$tnames = Input::get('tname');
$tmobiles = Input::get('tmobile');
$ttypes = Input::get('ttype');
$tsubjects = Input::get('tsubject');
$iMax = count($tclss);
for ($i = 0; $i < $iMax; $i++)
{
$tcls = ForumClass::where('cls', $tclss[$i])->where('divn', $tdivns[$i])->get();
if (is_null($tcls))
{
// cls, divn combination is missing from your table
// if you want to add it then do so or handle the error
$tcls = new ForumClass();
$tcls->cls = $tclss[$i];
$tcls->divn = $divns[$i];
$tcls->Save();
}
$teacher = new ForumTeacher();
$teacher->tname = $tnames[$i];
$teacher->tmobile = $tmobiles[$i];
$teacher->ttype = $ttypes[$i];
$teacher->tsubject = $tsubjects[$i];
$teacher->tcls = $tcls->id;
$teacher->save();
}
I did not test this but you get the idea of what to do.

Resources