$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
$category->save();
$category->category_id = $category->id;
$category->save();
I have this table, only the id is set to not null. I need to insert the column WITHOUT using auto increment, what do I add to my function?
I was thinking to count the id and just to add +1 so that the numbers are in squence:
$category->id = id()->count()+1;
This line is not counting though
fetch last id from db
$categoryId=HmsBbrCategory::orderByDesc('id')->first();
$autoIncId=$categoryId->id+1;
$category = new HmsBbrCategory;
$category->category_name = $request->input('category_name');
$category->category_description = $request->input('category_description');
$category->id=$autoIncId;
$category->category_id =$autoIncId;
$category->save();
Related
This question already has answers here:
How to get last inserted id through save() method in laravel
(6 answers)
Closed 2 years ago.
I have a table and it is like auto increment, I tried to do something like this: in an order table you insert for example your id, product name, value, and delivery address, and I needed it in another table called the order id and insert it in the order details.
using this method
$order = new order;
$order->id_user = 1; //$iduser;
$order->name_destain = $request->name;
$order->state = 1;
$order->total = $request->total;
$order->created_at = Carbon::now();;
$order->updated_at = Carbon::now();;
$order->save();
You can do it like, I assume that the name of other table is order_details and its model is OrderDetail.php
$order = new order;
$orderDetailObj = new OrderDetail; // or you can insert it in existing one OrderDetail::find($id); here $id is the particular (primary key)id of OrderDetail
$order->id_user = 1; //$iduser;
$order->name_destain = $request->name;
$order->state = 1;
$order->total = $request->total;
$order->created_at = Carbon::now();;
$order->updated_at = Carbon::now();;
if($order->save()){
$orderDetailObj->order_id = $order->id_user
$orderDetailObj->update();
return true;
}
How to get a list of skus whiout using the looping in magento.
Example: I am using below code with my conditons.
$productsCollection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku');
Now I want to result as array('A001','A002'....) etc.
I don't want to iterate (loop) the product collection.
Please suggest.
If you want to retrieve the collection in that way, you will have to loop through the collection and retrieve the sku.
$skus = array();
foreach ($productsCollection as $product) {
$skus[] = $product->getSku();
}
If you don't want that, you can just use a simple query because the SKU is kept in the catalog_product_entity table.
$conn = Mage::getSingleton('core/resource')->getConnection('core_write');
$table = 'catalog_product_entity';
$q = "SELECT sku FROM {$table}";
$list = $conn->fetchOneFieldAll($q, 'sku');
Retrieve the read connection you should use ->getConnection('core_read') not ->getConnection('core_write'). Whole codes is below which runs faster.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$skus = $readConnection->fetchCol('SELECT sku FROM `catalog_product_entity`');
foreach ($skus as $sku) {
echo $sku;
}
I have one to many relationship model on my database. For example, one property has more than one price:
property 1 => $200, $300
property 2 => $200, $350
etc.
This how to create property :
$prices = array();
$property = new Property();
$property->setName('property 3');
$data_prices = array(200,300,400);
foreach ($data_prices as $price {
$prc = new Price();
$prc->setPrice($price);
$prices[] = $prc; #object collection
}
$property->setPrices($prices);
$property->save();
#codes above create new records on table property and prices (related) and property id = 1
But when I try to update :
$prices = array();
$property = PropertyQuery::create()->findOneByPropertyId(1);
#create new prices
$data_prices = array(10,20,30);
foreach ($data_prices as $price) {
$prc = new Price();
$prc->setPrice($price);
$prices[] = $prc; #object collection
}
$property->setPrices($prices);
$property->save();
instead of deleting old price records or updating them, Propel creates new records. I want to delete old price records because I don't need them, or, if I can update the old with the new price it would be great.
How to do this in Propel?
Declaring the Price objects as new does just that—creates new Price objects. You need to either call the Price objects from the Property object or load the Price objects inside your foreach loop. Since I don't know the structure of your Price table, I can only give you the former solution:
$prices = array();
$property = PropertyQuery::create()->findOneByPropertyId(1);
$prices = $property->getPrices();
$data_prices = array(10,20,30);
for ($i = 0; $i < count($data_prices); $i++) {
if (!isset($prices[$i])) {
$prc = new Price();
$prc->setPrice($price);
$prc->save();
$prices[] = $prc;
} else {
$prc = $prices[$i];
$prc->setPrice($data_prices[$i]);
$prc->save();
}
}
$property->save();
How can i copy a record and save it with a different value for 1 or more field?
for example:
get----> first_name, last_name, civil_status, work
i want to copy the first name, and last name then insert it with a new civil status and work.
You could use the replicate method of model like this:
// Retrieve the first task
$task = Task::first();
$newTask = $task->replicate();
$newTask->project_id = 16; // the new project_id
$newTask->save();
You could use replicate method. This will create a new object with the same values except the primary key, and the timestamps. After that you can save your model:
$task = Task::find(1);
$new = $task->replicate();
If you want you could change a property
$new->project = $otherProject;
and then
$new->save();
if you want the new ID, easy:
$newID = $new->id;
You can use replicate() and then update that row:
Model::find(1)->replicate()->save();
$model = Model::find(1);
$model->project_id = $new_project_id;
$model->save();
also you can do like this.
$selected = Model::find(1);
$copy = $selected->replicate()->fill(
[
'project_id' => $new_project_id,
]
);
$copy->save();
What is the correct way to retrieve the last row, sorted desc by id, in CodeIgniter?
I've tried select_max but I want the whole row.
function getLastPagination(){
$data = array();
$this->db->query("SELECT id,sort FROM pagination GROUP BY id DESC LIMIT 1");
$query = $this->db->get('pagination');
$data = $query->row_array();
return $data;
}
$data['allpag'] = $this->Model_cats->getLastPagination();
$data['pagin'] = $data['allpag']['sort'];
$per_page = $data['pagin'];
Here I am getting the first value, but I want the last row.
$this->db->select('id')->order_by('id','desc')->limit(1)->get('table_name')->row('id');
Try this.............
function getLastPagination(){
$query ="select * from pagination order by id DESC limit 1";
$res = $this->db->query($query);
if($res->num_rows() > 0) {
return $res->result("array");
}
return array();
}
}
In controller function you need to do following things.......
$allpag = $this->Model_cats->getLastPagination();
$per_page = $allpag[0]['sort'];
$query = $this->db->query("SELECT * FROM pagination ORDER BY id DESC LIMIT 1")->row_array();
Should work?