Using saveAll() to save multiple records in CakePHP, I am able to save them successfully in a table. But the problem arises while retrieving the IDs of those saved rows. LastInsertID() returns only a single last ID here. How can I get all the last inserted IDs which I have inserted using saveAll()?
afterSave function is called after each individual save in a saveAll execution, so you could do:
In your AppModel
class AppModel extends Model {
var $inserted_ids = array();
function afterSave($created) {
if($created) {
$this->inserted_ids[] = $this->getInsertID();
}
return true;
}
}
You can place this code into any model and it should work fine. Then to return the IDs after the saveAll in your controller, you would do so like this:
if($this->Post->saveAll($posts)) {
$post_ids=$this->Post->inserted_ids; //contains insert_ids
}
Hope it helps
Related
I am trying to insert a two related objects via Eloquent and I get an Integrity constraint violation error on one of the tables. Here is what I am trying:
The representation of the relationship:
class DataParticipant extends Model
{
public function data_config()
{
return $this->hasOne('App\Models\DataConfig');
}
}
class DataConfig extends Model
{
public function data_participant()
{
return $this->belongsTo('App\Models\DataParticipant');
}
}
Then, I am trying the following:
$dataParticipant = new DataParticipant();
$dataParticipant->ip = // ip
$dataParticipant->code = // code
$dataParticipant->study_name = // study
// Prepare the config child.
$dataConfig = new DataConfig();
$dataConfig->config = // config via json_encode
// Store the child associated to the parent.
$dataParticipant->data_config()->save($dataConfig);
The error is on data_participant_id column, which belongs to the DataConfig model. My guess is that there is no id on which the two can be linked. I want to ask you:
How can I accomplish this without having to first save the DataParticipant then retrieve it from the database and then store the DataConfig on the resulted id?
First save the first model
$dataParticipant->save ();
Then save the second model
$dataParticipant->data_config()->save($dataConfig);
This won't retrieve the data from DB at all, and $dataParticipant id will be automatically set with the last insert id
I am creating a code by which i can replicate the multiple rows of table and its related tables.
I am able to replicate parent table successfully, but while replicating the contents of child table it is modifying the values which i am unable to figure it out.
my controller code is
public function copyshowtime($cinema_id,$show_date)
{
$date=new Carbon($show_date);
$current_show_date=$date->format('Y-m-d');
$next_show_date=$date->addDay()->format('Y-m-d');
$movieshowtime=Movies_showtimes::with('showdata')->where([['cinema_id','=',$cinema_id],['show_date','=',$current_show_date]])->get();
foreach ($movieshowtime as $item)
{
$item->show_date=$next_show_date;
$item->show_id=NULL;
$newshowtime=$item->replicate();
$newshowtime->push();
foreach ($item->showdata as $sd)
{
$newshowdata = array(
'showdata_id' => NULL,
'show_id'=>$newshowtime->id,
'category_id'=>$sd->category_id,
'showdata_category'=>$sd->showdata_category,
'showdata_rate'=>$sd->showdata_rate
);
// print_r($newshowdata);
Movies_showdata::create($newshowdata);
}
}
}
When I am printing the complete array its showing proper data which i am trying to insert ,check below
But after inserting it into table I see this data in table, the values of show_id and showdata_rate is getting zero value and other columns getting correct data inserted.
I am unable to figure out this problem as I am nowhere changing the data to zero before inserting it into table.
Make sure show_id and showdata_rate are fillable in your Movies_showdata model:
class Movies_showdata extends Model {
protected $fillable = ['show_id', 'showdata_rate', ...other fillable fields ];
}
Now this, from what I can see, should have been simple.
I want to be able to delete multiple records from the database. I have the id's of all the records I wish to delete. I call the resource.destroy route using comma separated list of ids (id is of postgres type uuid), like so:
Request URL:http://foo.app/products/62100dd6-7ecf-4870-aa79-4b132e60c904,c4b369f1-d1ef-4aa2-b4df-b9bc300a4ff5
Request Method:DELETE
On the other end, my controller action looks like so:
public function destroy($id)
{
try {
$ids = explode(",", $id);
$org->products()->find($ids)->delete();
}
catch(...) {
}
}
This gives me the following error:
BadMethodCallException in Macroable.php line 81:
Method delete does not exist.
in Macroable.php line 81
at Collection->__call('delete', array()) in ProductsController.php line 251
at Collection->delete() in ProductsController.php line 251
at ProductsController->destroy('62100dd6-7ecf-4870-aa79-4b132e60c904,c4b369f1-d1ef-4aa2-b4df-b9bc300a4ff5')
I have verified that find() is returning a collection of products matching the specified ids.
What am I missing?
PS:
1. The model Product has several belongsTo relationships with other models.
2. The product.destroy code works fine if I pass it a single id
EDIT
I guess, I'm also trying to understand what the difference between:
$org->products()->find($ids)->delete()
and
$org->products()->whereIn('id', $ids)->get()->delete()
is? From what I see, both find and get are returning Collections
The issue is that you're calling delete() on a Collection, which does not have that method.
You have a couple options here.
Model Events
If you have event listeners for the deleting/deleted model events, you will need to make sure the deletion happens in a way that each model is loaded and then deleted.
In this case, you can use the destroy method on the model that takes a list of ids. It will load a new model for each id, and then call delete() on it. As you mention in a comment, it won't restrict the deletion to only those products in the organization, so you would need to filter out those ids before passing the list into the destroy() method.
public function destroy($id)
{
try {
$ids = explode(",", $id);
// intersect the product ids for the org with those passed in
$orgIds = array_intersect($org->products()->lists('id'), $ids);
// now this will only destroy ids associated with the org
\App\Product::destroy($orgIds);
}
catch(...) {
}
}
If you don't particularly like that approach, you will need to iterate your collection of organization products and call delete() on them individually. You can use a standard foreach, or you can use the each method on the collection:
public function destroy($id)
{
try {
$ids = explode(",", $id);
$org->products()->find($ids)->each(function ($product, $key) {
$product->delete();
});
}
catch(...) {
}
}
No Model Events
Now, if you don't have any model events that you need to listen for, things are a little easier. In this case, you can just call delete() on the query builder, and it will go straight to deleting the records without loading any model objects. So, you get cleaner code with better performance:
public function destroy($id)
{
try {
$ids = explode(",", $id);
// call delete on the query builder (no get())
$org->products()->whereIn('id', $ids)->delete();
}
catch(...) {
}
}
If you create a model of your products, it will help you with these types of operations.
For example:
the model Products.php
<?php
namespace App\Http\Models;
use Illuminate\Database\Eloquent\Model;
class Products extends Model
{
/**
* The table associated with the model.
*
* #var string
*/
protected $table = 'products';
protected $primaryKey = 'id';
protected $fillable = ['name', 'price', 'description'];
}
The controller Products.php
You can use the destroy method and pass one or more primary keys to it as arguments.
<?php
namespace App\Http\Controllers;
use App\Http\Models\Products;
class Products
{
public function destroy($id)
{
try {
$ids = explode(",", $id);
//$ids is a Array with the primary keys
Products::destroy($ids);
}
catch(...) {
}
}
}
You can also use this option to remove query results with a custom parameter
$deletedRows = Products::where('name', 'phones')->delete();
You can check the Laravel documentation https://laravel.com/docs/8.x/eloquent#soft-deleting
When you use the find method, it will find only a single ID. You should use a whereIn to match multiple ids
public function destroy($id)
{
try {
$ids = explode(",", $id);
$org->products()->whereIn('id', $ids)->get()->delete();
}
catch(...) {
}
}
This way you will find all the products with the given IDs and delete them all.
I also faced this problem. Let $orgs contains some records as a collection. Now you can easily delete these records using a loop like this-
foreach($orgs as $org)
{
$org->delete();
}
I have the followings tables:
tbl_project(id, description)
tbl_operation(id, project_id, name)
tbl_itemType(id, operation_id, name)
tbl_item(id, itemType_id, name, unit, price)
I wanna when i create a new project, it adds some operations in tbl_operation and then adds some itemTypes to tbl_itemType and then adds some items in tbl_item. How can i do it in afterSave() behavior of project's model?
I read the following link, but i don't know is it possible to do by this?
esaverelatedbehavior
just create a function in your ProjectModel
public function afterSave()
{
$operation_model = new Operation();
$operation_model->setAttributes($YOUR_DATA);
$operation_model->save(); // create your operation
// same goes for every other data you want to save
return parent::afterSave(); // keep the chain
}
You could make use of the relations. This approach will only work if the respective relation contains only the models to be saved. In your controller have
$project->operations = array(/*your operations*/);
In turn each operation model could also have the related itemTypes
$operation->itemTypes = array(/*itemTypes for this operation*/)
And lastly each itemType could have the related items.
And in your afterSave for operations have
public function afterSave() {
foreach ($this->operation as $op) {
$op->project_id = $model->id;
$op->save();
}
return parent::afterSave();
}
For the afterSave for the Operation and ItemType classes should in turn save the related ItemTypes and Items respectively.
Better use afterSave() function , i think it will work for you
whenever I get a User from the database, I want to say "if this field for this user is empty..."
This is for the current user and every user that is retrieved from the database. Where would I put this if statement? Would it go in the model or is there a generic function that I can use the in the user controller before rendering any user?
Many thanks
You can use afterFind in your model to perform such checks, and this will work for data returned for find operation, like:
//like for User model's some field
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
if (empty($val['User']['some_field'])) {
$results[$key]['User']['some_field'] = "Empty Field";
}
}
return $results;
}
Note:- You can define such functions in your Model Class or AppModel as well.