Update object instead of inserting with Doctrine using assignIdentifier() - php

I'm pretty new to Doctrine, but as I understand it, the assignIdentifier() method is supposed to tell Doctrine to update the relevant row into the database instead of inserting a new one.
I have an object that I'm building through a workflow, so the identifier has an id of null until I call $object->save(); which inserts it, and this does work.
If however I call $object->assignIdentifier($newobj->id); and then $object->save(); it does nothing - it does not insert a new row and does not update the old one.
If a certain condition is true, I want to pull a different record out of the DB and update that row instead of inserting the new one.
Am I understanding something wrong here?
Some code to illustrate:
if($this->object->payments > 0) {
$older_payment = Doctrine_Query::create()
->from('OldPaid p')
->where('p.dealid = ?', $this->object->transid)
->fetchOne()
;
$this->object->assignIdentifier($older_payment->id);
}
$this->object->save();

Like i got to know, save() will not update an existing record with autoincrement on ID.
I have the same problem using doctrine 1.2.
an idea i have use this one, the only workaroung i found:
$query = Doctrine_Query::create()->update('OldPaid');
$query->set($yourFieldname, '?', $yourValue);
$query->addwhere('p.dealid = ?', $this->object->transid);
$query->execute();
Thiw will function when a record is in the DN with the primaryKey dealid = $this->object->transid.
greeting m

Usually, if you retrieve a record, you can update it with the save() method. Doctrine recognizes this (since the PK doesn't change) and updates the record.
From the docs:
Updating objects is very easy, you
just call the Doctrine_Record::save()
method
Another way can be replace(), but I usually use just save() and does either the saving or the updating if the record already exists.
As far as I can read from the description of assignIdentifier() never used it myself) it will only work with retrieving an object by its ID, so updating something with this method will not work.

Related

Laravel: Is always read the newly written data after calling save() method of ORM?

Code:
$item1 = Item::find(1);
$item1->foo = 1;
$item1->save();
$another_item1 = Item::find(1);
dd($another_item1->foo);//Is this value always 1?
My question:
Is always read the newly written data after calling save() method of ORM? In my example, Is $another_item1->foo always 1?
If the answer to question 1 is not, how could I ensure I read the newly written data from the database?
Is always read the newly written data after calling save() method of ORM?
No, there is no SELECT ran after a INSERT or UPDATE statement in this case.
In my example, Is $another_item1->foo always 1?
Based on your own comment, Yes.
If the answer to question 1 is not, how could I ensure I read the newly written data from the database?
$model->save();
// Reload the current model instance with fresh attributes from the database.
$model->refresh();
// OR
// Reload a fresh model instance from the database.
$fresh = $model->fresh();
I think you may be confused about the find() function. find() is used to fetch one or many models by its / their primary key(s). The return value will either be a single model, a collection or null if the record is not found.
If you are looking to lookup multiple rows you need to run Item::get();
Uses
$Item = Item::find(1); // returns model or null
$Items = Item::find(array(1, 2, 3)); // returns selected Items in collection
$Items = Item::get(); // Returns all in collection
https://laravel.com/docs/5.5/eloquent

laravel update and select

I have a situation to update the row based on id and return the complete row. I can do it in two queries but i want to do it in one query only. I have write this...
$result= DB::table($this->table)
->whereRaw($where['rawQuery'], $where['bindParams'] ? $where['bindParams'] : array())
->increment($updateField);
if($result){
return DB::table($updateTable)
->select('id')
->where('campaign_id',$where['bindParams'])
->where('date',date("Y-m-d"))
->get();
}else{
throw Exception("Error in fetching data");
}
I copied this from what you commented in your question:
No i want to return the id of the updated row or complete row if possible
If you want to return the ID of the just updated 'row' you're talking about. You can use Eloquent to accomplish this.
You can use a route like this:
Route::put('demo/{id}', Controller#update);
Then in your update function in your controller you can get the ID.
Find the model with $model = Model::find(id);
do things with the data you get.
Like $model->name = Input::get('name');
Then use $model->save();
After saving you can do $model->id;
Now you get back the ID about the row you just updated.
Refer back to this question:
Laravel, get last insert id using Eloquent
But any way it'll always be at least 2 queries (a SELECT and an UPDATE in MySQL, however you do it)
You can check Laravel Eloquent if you want a "cleaner" way to to this.

Laravel: Create or update related model?

Please be gentle with me - I'm a Laravel noob.
So currently, I loop through a load of users deciding whether I need to update a related model (UserLocation).
I've got as far as creating a UserLocation if it needs creating, and after a bit of fumbling, I've come up with the following;
$coords = $json->features[0]->geometry->coordinates;
$location = new UserLocation(['lat'=>$coords[1],'lng'=>$coords[0]]);
$user->location()->save($location);
My issue is that one the second time around, the Location may want updating and a row will already exist for that user.
Is this handled automatically, or do I need to do something different?
The code reads like it's creating a new row, so wouldn't handle the case of needing to update it?
Update - solution:
Thanks to Matthew, I've come up with the following solution;
$location = UserLocation::firstOrNew(['user_id'=>$user->id]);
$location->user_id = $user->id;
$location->lat = $coords[1];
$location->lng = $coords[0];
$location->save();
You should reference the Laravel API Docs. I don't think they mention these methods in the "regular docs" though so I understand why you may have not seen it.
You can use the models firstOrNew or firstOrCreate methods.
firstOrNew: Get the first record matching the attributes or instantiate
it.
firstOrCreate: Get the first record matching the attributes or create it.
For Example:
$model = SomeModel::firstOrNew(['model_id' => 4]);
In the above example, if a model with a model_id of 4 isn't found then it creates a new instance of SomeModel. Which you can then manipulate and later ->save(). If it is found, it is returned.
You can also use firstOrCreate, which instead of creating a new Model instance would insert the new model into the table immediately.
So in your instance:
$location = UserLocation::firstOrNew(['lat'=>$coords[1],'lng'=>$coords[0]]);
$location will either contain the existing model from the DB or a new instance with the attributes lat and lng set to $coords[1] and $coords[0] respectively, which you can then save or set more attribute values if needed.
Another example:
$location = UserLocation::firstOrCreate(['lat'=>$coords[1],'lng'=>$coords[0]]);
$location will either contain the existing model from the DB or a new model with the attributes set again, except this time the model will have already been written to the table if not found.

Add element to doctrine ArrayCollection by Id

I find very annoying to have to fetch an object by id from database every time I want to add it to a relationship. Is there a way to add an object to a a relationship by id instead of adding the whole object?
This is my actual code:
...
$person = $em->getRepository("Person")->findOneById($id);
$me->getPersons()->add($person);
...
I would like to have something like this:
...
$me->getPersons()->add($id);
...
Then I would be saving one trip to the database! Which I like better! Is it possible?
You don't have to do that actually. You can get reference object like so:
$person = $em->getReference("Person", $id);
$me->getPersons()->add($person);
Doctrine will not make a query for Person but will instead return an reference proxy object for person with that id. If you, however do:
$person = $em->getReference("Person", $id); // 0 queries
$person->getId(); // Still 0 queries
$person->getSomeField(); // query fired
Doctrine will trigger lazy load if you try to get some field that has to be fetched from database.
See docsEntityManager::getReference method

How can I get back the id of my object after save it on symfony 1.4?

I was reading that if I make for example
$car = new Car();
$car->set('name','my car');
$car->set('color','orange');
and so on...
and if i make $car->save();
after that I will be able to make $car->getCarId(); (in case the id field was mapped as CarId, Ive been trying this, but I get null everytime, and the info is persisted on the DB, I dont know how to retrieve that information, any idea how to do this?
I depends on the ORM you are using:
For doctrine try:
$car->id; or $car->getId();
For propel only use:
$car->getId();
Note
These are only default values, if you defined something else then 'id' as your primary key field use the one you defined in the getter.

Categories