DBTable creates new row instead off updating the existing - php

I try to update an DB entry with now data, but i am just creating an new entry:
$client =$this->clientTable->find($id);
$client->CompanyName = $request->getPost('CompanyName');
$this->clientTable->update();
$this->_redirect('client/index');

Zend_Db_Table_Abstract::find() method returns Zend_Db_Table_Rowset object. You should use method which will return you Zend_Db_Table_Row object and use it.
For example:
$clientRow = $this->clientTable->fetchRow(array('id' => $id));
$clientRow->CompanyName = $request->getPost('CompanyName');
$clientRow->save();
If your's table primary key name is not 'id', change it to suitable value in the first line of code above.

Related

Eloquent / Laravel: How to get last insert/update ID/instance of updateOrCreate()?

The title is mostly self-explanatory. Eloquent has a method called
updateOrCreate()
documented here: https://laravel.com/docs/5.5/eloquent#other-creation-methods
In some cases this is really useful. However after doing updateOrCreate() I need either the updated/created object or its primary key or its id.
Of course I could do MyModel::where(...)->first() and give all those data again but this is clumsy and may be some expensive request.
However updateOrCreate() only returns true or false.
Any ideas?
The method will return ID of created or updated object, so just do this:
$object = Model::updateOrCreate(['name' => 'John'], ['age' => 25]);
$id = $object->id;
Query for the last one
$lastRecord = MyModel::last();
Or
$lastRecord = MyModel::orderBy('id', 'DESC')->first();

How to put a value into a Pointer column?

I'm working with 'Parse.com' and I would like to store a value into a column in a Class that points to another Class.
For example:
$user = new ParseUser();
$user->set("username", "Batman");
$user->set("password", "darkknight");
$user->set("vehicle_ID", "UgTuNHEQEZ"); //pointer
Here, "vehicle_ID" is the name of the column. It's a pointer to the column id in the Class "Vehicle".
The error I get is that Parse expected the name of the Class I'm referring to. So, if I change the last line and do:
$user->set("Vehicle", "UgTuNHEQEZ");
The new user is created BUT the "vehicle_ID" is empty. Why is vehicle_ID not populated?
Double handicapped by not knowing php and discovering to my dismay that the parse.com API reference is a link to the github source, I did notice in the source a constructor function as follows:
public function __construct($className = null, $objectId = null,
$isPointer = false
) {
...
This suggests:
$vehicle = new ParseObject("OtherClass", "UgTuNHEQEZ", true);
$user->set("vehicle_ID", $vehicle);

Symfony2 Doctrine2 how to add object if not fetched from db

Check this example: http://symfony.com/doc/current/book/doctrine.html#fetching-objects-from-the-database
What I got is:
$results = $applicationsRepo->findByInName($appInNames);
Where appInNames is an array looking like this:
array(
app1_name => app1_name,
app2_name => app2_name,
app3_name => app3_name,
...
)
I want to create an entity object when it's not found. How to check if app1_name was returned and if not create one ?
If you need to get an array of all entities that have inName attribute set to either of app1_name, app2_name, etc with a default value if it does not exists, you can use findOneBy instead of findBy and create your entity if the result is NULL.
May not be the most efficient method because of the loop but it give you what you need :
$results = array();
foreach($appInNames as $appInName) {
$app = $applicationsRepo->findOneByInName($appInName);
if(!isset($app)) {
// Create you entity here
}
$results[$appInName] = $app;
}
A more efficient method may be to write a custom Repository and use the queryBuilder to add all your OR conditions. You'll get all the existing entities in one query, you will then have to parse the result to create the missing entities.

How to make a "find and update" query in Paris

I am using Paris with Idiorm and I am having problems finding in the documentation a clear instruction on how to find and update a table.
I don't want to insert a sql query into the script. Is there any other way?
Paris is an Active Record implementation based on Idiorm.
Idiorm is an object-relational mapper and fluent query builder.
I am interested in doing something like count = count + 1 all in one go
I found this on their github site:
Updating records
To update the database, change one or more of the properties of the object, then call the save method to commit the changes to the database. Again, you can change the values of the object's properties either by using the set method or by setting the value of the property directly:
$person = ORM::for_table('person')->find_one(5);
// The following two forms are equivalent
$person->set('name', 'Bob Smith');
$person->age = 20;
// Syncronise the object with the database
$person->save();
Creating new records
To add a new record, you need to first create an "empty" object instance. You then set values on the object as normal, and save it.
$person = ORM::for_table('person')->create();
$person->name = 'Joe Bloggs';
$person->age = 40;
$person->save();
After the object has been saved, you can call its id() method to find the autogenerated primary key value that the database assigned to it.
Checking whether a property has been modified
To check whether a property has been changed since the object was created (or last saved), call the is_dirty method:
$name_has_changed = $person->is_dirty('name'); // Returns true or false
According to the documentation on the github page, in idiorm you can update a record by doing the following:
$person = ORM::for_table('person')->find_one(5);
// The following two forms are equivalent
$person->set('name', 'Bob Smith');
$person->age = 20;
// Syncronise the object with the database
$person->save();
Or to do it in 'paris' you do:
$user = Model::factory('User')->find_one($id);
$user->name = 'Paris';
$user->save();

Codeigniter how to map a class sent to a model

I have a SectorModel with this function:
public function update(Sector $sector) {
$this->db->where('sector_id', $sector->getScetor_id());
return $this->db->update(_SECTOR_, $sector);
}
There are times that I’ll change only the name of the Sector object:
$Sector = new Sector();
$Sector->setSector_name = 'test';
$this->SectorModel->update($Sector);
The generated select looks like:
UPDATE realestate_sector SET sector_name = 'teste', sector_description = NULL
It will update but will set all other properties to NULL because it was not set on my object.
Right now, I have to fill the whole object before sending it.
Is there a way to map the Sector class and update only what was sent on the object?
Thanks in advance for any help.
Sorry for any typos, my English is not good =)
Just loop through all your object's properites and then if any is NULL just drop it with unset.
Here is your model's method edited to achieve that:
public function update(Sector $sector)
{
foreach($sector as $k=>$v)
{
if($v === NULL)
unset($sector->$k)
}
$this->db->where('sector_id', $sector->getScetor_id());
return $this->db->update(_SECTOR_, $sector);
}
Here you can find some info about iterating objects in PHP
The easiest to do this would be to rather use a array - docs here http://codeigniter.com/user_guide/database/active_record.html#update - you just create a array of all the columns with their values that you want to update and perform a $this->db->update('mytable', array('name' => 'test'), array('id' => $id)); call. This will only update the columns you specified in the First array. With the second array acting as your WHERE expression.
The only reason I can think of as to why your other values are being set to NULL is because in your example you create a new instance of the class and the other values must either have been set to nothing or are set to NULL. It would (If this is the case) be better to get a record from the table and then change and values on the populated record and pass that to the function to update.
Hope that helps.

Categories