I'm having problem with active record Update Batch on CodeIgniter.
On the documentation,
$this->db->update_batch('mytable', $data, 'title');
Where 'title' is the index for the update process. Is it possible to have two or more index to this active record?
I already tried
$this->db->update_batch('mytable', $data, 'title, title_2');
$this->db->update_batch('mytable', $data, array('title', 'title_2');
And it's not working.
Related
I am trying to increment a column and updateorcreate a record in one query by doing so:
$model = Model::updateOrCreate([
'email' => $email
], ['received_at' => $received_at])->incremet('received_count');
This does the job as I wanted it too. It updates or create a record and increments the received_count column.
But after the query, I wanted to get the updated/created row, but when I log $model, it only logs 0 or 1. I can confirm that this is because of the ->increment().
But to be honest, I don't know any way how to increment the received_count column other than how I currently did it.
How do I achieve so that it updates or create a record, at the same time increments the received_count column, and after all of this, returns the updated/created object?
As much as posssible, I want this all in one query. Getting the model should be a memory.
$model = Model::updateOrCreate(['email' => $email], ['received_at' => $received_at]);
$model->increment('received_count');
$model->refresh(); // this will refresh all information for your model.
or you can simply:
$model = Model::updateOrCreate(['email' => $email], ['received_at' => $received_at]);
tap($model)->increment('id'); // this will return refreshed model.
just fresh model after incremet:
$model = Model::updateOrCreate([
'email' => $email
], ['received_at' => $received_at]);
$model->incremet('received_count');
$model->fresh();
I have a one-to-many relationship in laravel. Author to Books. How would I update all records in specific book and remove those that are not in my array. For example, $book is array with author, page, publication and I want to update those records and at the same time detach those that are not there?
I used $book->update($array) but that failed. I also used sync and that failed because it can only be used with many-to-many
Here is some example of how to update many records in the model using where conditions.
Book::where('author', $author_name)
->update([
'price' => 10000, // Add as many as you need
]);
Book::where('author',$author)
->where('publication', $publication)
->update([
'price' => 10000,
]);
// Or More Dynamic
Book::whereAuhorAndPublication( $author_name, $publication )
->update([
'price' => 10000,
]);
it would be nice if you share an example of data and request data.
I don't get the difference between query builder's Replace and Update. Especially the documentation for Replace...
This method executes a REPLACE statement, which is basically the SQL standard for (optional) DELETE + INSERT, using PRIMARY and UNIQUE keys as the determining factor.
...but I see no indication of using a PK in the example. Am I missing some fundamental knowledge here? (I understand Update just fine).
Replace
$data = array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
);
$this->db->replace('table', $data);
Update
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
Thanks.
REPLACE
It's like insert. but if the primary key being inserted is the same one as another one, the old one will be deleted and the new one will be inserted.
UPDATE
Updates the current row that you tried to update.
how can I increment a value?
$app->db->update('videos', array(
'views' => 'views + 1'
), array(
'id' => $id
));
It doesn't work in many ways I tried.
A better approach could be to use the method executeUpdate()
$app->db->executeUpdate("UPDATE videos SET views=views+1 WHERE id=?", array($id));
Edit 2022
The API is working but marked deprecated. Use executeStatement() instead of executeUpdate()
You have to ask the db for the current value of views first, then increment the value and save the new value into db.
$result = $app->db->fetchAssoc('SELECT views FROM videos WHERE id = ?', array($id));
$result['views']++;
$app->db->update('videos', array('views' => $result['views']), array('id' => $id));
I'm using active record in CodeIgniter.
And I can insert data into database successfully.
But how to get the result of insert sql when fail?
Now it return a html say about the sql error.
I don't want this html content.
EDIT
$data = array(
'title' => 'My title' ,
'name' => 'My Name' ,
'date' => 'My date'
);
$this->db->insert('mytable', $data);
Simple code. But when the 'name' column has unique property. And I'm inserting a duplicate value to it. It return html content of sql error.
I just want it to return the error code and not output the html content.
To not get the DB error message make sure that you have $db['default']['db_debug'] = FALSE in the file /application/config/database.php.
Then after you've preformed your (attempted) insert you can run:
$num_inserts = $this->db->affected_rows();
If the result is 0, your insert failed and you can present an error message of your own choosing.
Use these functions
$errNo = $this->db->_error_number();
$errMess = $this->db->_error_message();
They will help