PHP: Propel will save only first column - php

I've been struggling with this problem for a while now and can't figure out reasonable explanation for this behaviour...
I have in MySQL a table called "notifications" and columns "package_id", "mail_sent_at" and "sms_sent_at", and appropriate Propel model for that.
I'm setting "sms_sent_at" column with current time and saving it. Works perfectly.
$not = new Notification();
$not->setPackageId($package_id);
$not->setSmsSendedAt(time())->save();
The result for this is:
Now I want to add one more thing - set mail_sent_at column. So what I do:
$not = new Notification();
$not->setPackageId($package_id1);
$not->setMailSendedAt(time())->save();
$not->setSmsSendedAt(time())->save();
And here's the magic: only mail_sent_at column was set.
Additional notes:
after var_dump'ing i can see that dumped object has both properties set after executing these operations
var_dump'ing object retrieved from propel query by package_id after these operations executed doesn't have the second column set. As it is in DB.
Same behaviour happens other way around if I first set different column - first saved column goes into DB, second don't.
Why is that happening and how can I fix it?
Ps. Yeah, I know it's "sent", not "sended"
EDIT
Finally I found the solution. In case someone is having similar problem:
check if your primary key in propel schema has autoIncrement="true". If not - add it!
What was happening was: I've created a new object with id=null, saved it to DB using implicitly SQL INSERT (since it's new record ) and while working on the same object modified it and saved it to DB once again, but this time imlpicitly by SQL UPDATE (Propel figures out that I inserted this object once). The problem was, that after first saving I didn't get id for my object set (even though there was ID set in DB). So second SQL contained "UPDATE...WHERE id = NULL".

Related

Laravel Save Method Isn't Updating Record

In one of my controllers, I am needing to update a specific record in another table. I have code to find the table, update the specific column, and save it. My problem is that the record is never updated. I have ensured that the column is fillable.
Here is the code that is giving me problems:
$portfolio = Portfolios::where('id', $request->portfolioID)->first();
$portfolio->totalValue = $portfolio->totalValue + $cd->currValue;
//dd($portfolio);
$portfolio->save();
At the die and dump that is commented out, I can compare the attributes shows the correct value and the original value was the previous value, which is still retained after the save.

Is there a simple way to read auto-assigned values on insert using php mysqli?

I have an in-house PHP class that I use for Object Relational Mapping. I would like to add an improvement to it though, and I'm not sure how to go about it:
When a new record is created, it has a flag marking it as such. When the record is saved, it checks for that flag; if the flag is set, then any fields that were flagged as "auto_increment", are assigned the mysqli::insert_id value.
What I'd like to do is update this so that ~any~ field that the database updates on the save (e.g. TIMESTAMP DEFAULT NOW()), gets assigned back to the object.
So, the only way that comes to mind is as follows:
Save the record
Grab the auto-increment field if applicable
Reload the record with another SELECT
I guess that would work, but it seems a bit roundabout to me. It also depends on none of the key fields being automatically assigned.
Is there any way to get auto-assigned values other than insert_id from the mysqli object after saving the record?

Read from one database and write to another php/laravel

I'm learning Laravel and would know howto read data from a db and write it automatically to a second db.
First I read from db1 and it works:
$paciente_q = Pacientes::on('db1')->find($id);
Then I wish to move the data to an identical table on db2 (assigned in the configuration)
Pacientes::create($paciente_q);
The error is that I pass an object and "::create" wants an array. I converted it to an array but didn't work. The only option that I can find is to create an array with the data and then make the ::create. But I think that there should be an easier way. I'm talking about 10 columns.
What could I do if we talk about hundreds of columns?
Your approach didn't work probably because by default mass assignment is prevented for security reasons; you need to manually set the model's fields that are mass assignable in the fillable property of the model (that should be an array) - if you do not care about that security or are sure that you'll never directly mass-assign user input to your models you can make all the fields mass assignable by setting the guarded property of the model to an empty array.
Once that's done, your code is mostly correct, just convert the model to an array and don't forget to select the second database when creating the model, like so :
// the model to insert, converted to an array - get() would also work but first() ensures we get only one record even if the primary key is messed up and there are multiple values with the same ID
$paciente_q = Pacientes::on("db1")->find($id)->first()->toArray();
// create the same model on the second database
Pacientes::on("db2")->create($paciente_q);
Now, if you want to do it occasionally for a few rows then the above approach is suitable, otherwise you may look at bulk insertion, here's an example for copying the entire table from your first database to the second one :
// an array with all the rows
$patients = Pacientes::on("db1")->all()->toArray();
// get the model's table name
$table = with(new Pacientes)->getTable();
// bulk insert all these rows into the second database
DB::connection("db2")->table($table)->insert($patients);
Note that here we're not using Eloquent for inserting them, so we must first get the table's name from an instance of the model; if the table's name on the second database is different from the first then adjust the $table variable accordingly.
The solution was to change the get() to first() because we were searching for one item. I read wrong the first solution from #André... sorry! Should learn to read instead of Laravel!
$paciente_q = Pacientes::on('db1')->where('numerohistoria',$numerohistoria)->first()->toArray();
Pacientes::create($paciente_q);
Now it works!! Thanks to all and specially to #André !

Yii insert data

I'm a fairly new yii user, and I have a little project I'm doing. I'm not using Yii's model generator since my queries are pretty custom and I'm still not quite at home with the whole yii active record thing, so I'm sticking to the query builder for now. I've grasped the basic ideas of making sql statements, but I think I'm going to run into a problem with one I need to do.
Basically, I'm inserting data into one table, and I need to get the value of the id column of the just inserted data. The easiest way would be to just do an insert and get the max value of the id column, but I'm pretty sure that's not the correct way to do it - since someone else can insert stuff "at the same time" and I might end up getting the wrong value - and I need the right one because I will be inserting it into another table.
I've seen that the return value of the insert() function is an integer, the number of inserted rows so I can't use that.
Basically, is there a way to get the data I just inserted to a table that's 100% correct & safe - even if someone else inserted something to the table "at the same time"?
You want getLastInsertId() of the CDbConnection class:
$id = Yii::app()->db->getLastInsertId();
if you have saved the model, you can access it with
$model->id
assuming that id is the auto increment id field in your database.
obviously, before the model is saved you can't get it's id

Should I be able to update a MySQL enum type with a text value?

I'm trying to update the value in a MYSQL enum field from PHP via Doctrine (5.3 and 1.2 respectively).
I get an error when I try and do this:
$q = Doctrine_Query::create()
->update('StMessages')
->set('status','new')
->where('message_id = ?',$msg_id);
I get a sql state error telling me that the column 'new' does not exist. If I enter 3 instead of new (presumably the internal index of the 'new' value), then the query works. In fact it happens in a SQL client too so perhaps this is a quirk of this version of MySQL? Its 5.1.45.
Anyone know if this is how MySQL is supposed to treat enums or if this is more likely a Doctrine issue? I have 'use_native_enum' set to true.
Given a table
create table test (
enumfield enum('a','b','c')
);
you should be able to do
update test set enumfield='a';
which is the whole point of the num field - not having to mess with indexes and whatnot.
What's the exact definition of your enum field?
I think this was actually an issue relating to the quoting of strings when updating the field's values. I'm not sure if Doctrine or I was at fault.

Categories