I m trying to delete my db table row using below code from observer. but can't delete it gives error in system log
Some transactions have not been committed or rolled back
$customFieldValue = $this->_getRequest()->getPost();
//$groupCodeArray = $customFieldValue['product']['customer_group_ids'];
$DataCollectionDel = Mage::getModel('customerspecificproduct/customerspecificproduct')->getCollection()->addFieldToFilter(
'product_ids',
array(
'eq'=>$product->getId()
)
)->addFieldToFilter(
'group_id',
array(
'notnull'=>true,
)
);
if($DataCollectionDel){
foreach($DataCollectionDel as $data){
$deleteRow = Mage::getModel('customerspecificproduct/customerspecificproduct')->load($data->getId())->delete();
}
}exit;
I found solution myself
$DataCollectionDel = Mage::getModel('customerspecificproduct/customerspecificproduct')->getCollection()->addFieldToFilter(
'product_ids',
array(
'eq'=>$product->getId()
)
)->addFieldToFilter(
'group_id',
array(
'notnull'=>true,
)
);
try{
if($DataCollectionDel){
foreach($DataCollectionDel as $data){
$data->delete();
}
}
I found solution for it :3 without getCollection().
Model
public function deleteByCondition($itemId,$vendor)
{
return $this->getResource()->deleteByCondition($itemId,$vendor);
}
Resource Model
public function deleteByCondition($itemId,$vendor)
{
$table = $this->getMainTable();
$where = array();
$where[] = $this->_getWriteAdapter()->quoteInto('item_id = ?',$itemId);
$where[] = $this->_getWriteAdapter()->quoteInto("vendor = ? ", $vendor);
$result = $this->_getWriteAdapter()->delete($table,$where);
return $result;
}
Related
I am new to codeiginter,how to insert the data into table using codeiginter
my controller code;
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
It is simple form use internet to search it
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
$this->db->insert('tablename',$data);
I would like to share reusable code approach by following the MVC coding style
My Controller
$table = 'table_name';
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
$record_id = $this->Common_Model->insert_into_table($table, $data);
My Common_Model
function insert_into_table($table, $data) {
// echo "<pre>";print_r($data);exit;
$insert = $this->db->insert($table, $data);
$insert_id = $this->db->insert_id();
if ($insert) {
return $insert_id;
} else {
return false;
}
}
You can use this model function as much time as you want.
I have problem with edit and update with 2 table using query builder.
--->After i press button submit--->the data insert new row---> not update current data.
This is my function edit(only for view old data)
public function edit(Request $request, $id){
$tax_rate = TaxRate::find($id);
$tax_rate_details = TaxRateDetail::where('tax_rate_id', $id)->get();
$country = Country::all();
$geo_zones = GeoZone::all();
//dd($tax_rate);
//dd($tax_rate_details);
//dd($geo_zones);
if(!$tax_rate) {
return redirect('/');
}
return view('tax_management.edit',['country' => $country , 'tax_rate'=>$tax_rate , 'geo_zones'=>$geo_zones, 'tax_rate_details'=>$tax_rate_details]);
}
This is my update(i do validation and the saveTax is the saving part)
public function update(Request $request, $id){
$this->validate($request,[
'country_id'=> 'required',
'tax_type' => 'required',
'name' => 'required|max:100',
'code' => 'required|max:50'
]);
//dd($request->input());
DB::beginTransaction();
try{
$tax_rate = TaxRate::find($id);
$tax_rate_details = TaxRateDetail::where('tax_rate_id', $id)->get();
$this->saveTax($request, $tax_rate);
DB::commit();
return redirect()->route('tax_management.index');
} catch (\Exception $ex){
//dd($ex);
DB::rollback();
return back()->withInput()->withErrors('Fail to save');
}
}
This is my function save()
private function saveTax(Request $request, $tax_rate){
$tax_rate->country_id = $request->input('country_id');
$tax_rate->geo_zone_id = $request->input('geo_zone_id');
$tax_rate->tax_type = $request->input('tax_type');
$tax_rate->name = $request->input('name');
$tax_rate->code = $request->input('code');
$tax_rate->description = $request->input('description');
if(!empty($request->input('active'))){
$tax_rate->active =1;
} else {
$tax_rate->active =0;
}
$tax_rate->save();
if($tax_rate->tax_rate_id) {
TaxRateDetail::where('tax_rate_id', $tax_rate->tax_rate_id)->delete();
}
if($request->input('tax_rate_details')){
foreach ($request->input('tax_rate_details') as $key => $value) {
$tax_rate_detail = new TaxRateDetail();
$tax_rate_detail->tax_rate_id = $tax_rate->tax_rate_id;
$tax_rate_detail->priority = $value['priority'];
$tax_rate_detail->date_from = $value['date_from'];
$tax_rate_detail->date_to = $value['date_to'];
$tax_rate_detail->rate = $value['rate'];
$tax_rate_detail->type = $value['type'];
$tax_rate_detail->active = $value['active'];
//dd($tax_rate_detail);
$tax_rate_detail->save();
}
}
}
I want to save edit update with old(id). not create new. Please help thank you. I don't know where the code gone wrong.
I have to lots of paginations one lot is for my user's results and the other is for my question results.
When I am on link 3 on my question results it also switches the results on users list pagination.
Question if I click on the questions pagination links how can I make sure it does not affect the results of the user's list.
I have tried this Best way to do multiple pagination on one page in codeigniter does not work
As you can see in image below because I am on questions list page 3 it has effected the users list
<?php
class Dashboard extends MY_Controller {
public $data = array();
public function __construct() {
parent::__construct();
$this->load->model('user/user_model');
$this->load->model('forum/question_model');
$this->load->library('pagination');
}
public function index() {
$this->data['title'] = 'Dashboard';
$this->data['is_logged'] = $this->session->userdata('is_logged');
$config1['base_url'] = base_url('dashboard/');
$config1['total_rows'] = $this->user_model->total_users();
$config1['per_page'] = 2;
$config1['uri_segment'] = 2;
$config1['num_links'] = 200;
$config1['use_page_numbers'] = FALSE;
$config1['prefix'] = 'u_';
$pagination1 = new CI_Pagination();
$pagination1->initialize($config1);
$this->data['pagination1'] = $pagination1->create_links();
$page_segment1 = explode('_', $this->uri->segment(2));
$page1 = ($this->uri->segment(2)) ? $page_segment1[1] : 0;
$this->data['users'] = array();
$users = $this->user_model->get_users($config1['per_page'], $page1);
foreach ($users as $user) {
$this->data['users'][] = array(
'user_id' => $user['user_id'],
'username' => $user['username'],
'status' => ($user['status']) ? 'Enabled' : 'Disabled',
'warning' => '0' . '%',
'date' => date('d-m-Y H:i:s A', $user['date_created_on']),
'href' => site_url('user/profile/') . $user['user_id']
);
}
// Questions Pagination & Results
$config2['base_url'] = base_url('dashboard/');
$config2['total_rows'] = $this->question_model->total_questions();
$config2['per_page'] = 2;
$config2['uri_segment'] = 2;
$config2['num_links'] = 200;
$config2['use_page_numbers'] = FALSE;
$config2['prefix'] = 'q_';
$pagination2 = new CI_Pagination();
$pagination2->initialize($config2);
$this->data['pagination2'] = $pagination2->create_links();
$page_segment2 = explode('_', $this->uri->segment(2));
$page2 = ($this->uri->segment(2)) ? $page_segment2[1] : 0;
$this->data['questions'] = array();
$questions = $this->question_model->get_questions($config2['per_page'], $page2);
foreach ($questions as $question) {
$this->data['questions'][] = array(
'user_id' => $question['user_id'],
'title' => $question['title']
);
}
$this->data['navbar'] = $this->load->view('common/navbar', $this->data, TRUE);
$this->data['header'] = $this->load->view('common/header', $this->data, TRUE);
$this->data['footer'] = $this->load->view('common/footer', '', TRUE);
$this->load->view('common/dashboard', $this->data);
}
}
Question Model
<?php
class Question_model extends CI_Model {
public function get_questions($limit, $start) {
$this->db->select('*');
$this->db->from('questions q');
$this->db->join('users u', 'u.user_id = q.user_id', 'left');
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result_array();
}
public function total_questions() {
return $this->db->count_all("questions");
}
}
Users Model
<?php
class User_model extends CI_Model {
public function get_users($limit, $start) {
$this->db->select('u.status, u.date_created_on, ud.*');
$this->db->from('users u', 'left');
$this->db->join('users_userdata ud', 'ud.user_id = u.user_id', 'left');
$this->db->limit($limit, $start);
$query = $this->db->get();
return $query->result_array();
}
public function total_users() {
return $this->db->count_all("users");
}
}
I'm trying to update a pivot table like this:
public function updatePermission($id, $permissionId)
{
$permissionValue = Input::get('value');
$user = User::find($id);
$perms = ['value' => $permissionValue];
$user->permissions()->updateExistingPivot($permissionId, $perms);
}
This pivot has been previously created with:
public function attachPermission($id)
{
$permissionId = Input::get('id');
$permissionValue = Input::get('value');
$user = User::find($id);
if (!$user->permissions->contains($permissionId)) {
$user->attachPermissionById($permissionId);
$perms = ['value' => $permissionValue];
$user->permissions()->updateExistingPivot($permissionId, $perms);
} else {
return Response::json(array('error' => 'Permission ' . $permissionId . ' is alreay set for user ' . $user->id));
}
return Response::json(array('role' => User::with(['roles.permissions', 'permissions', 'students'])->find($user->id)));
}
When the updatePermission() method is hit, it passes fine, but it doesn't update the pivot table with the new value. What am I doing wrong here?
I won't tell you why it doesn't work, but I suggest you do this:
public function attachPermission($id)
{
$permissionId = Input::get('id');
$value = Input::get('value');
$user = User::find($id);
$sync = $user->permissions()->sync([$permissionId => compact('value')], false);
return (in_array($permissionId, $sync['updated']))
? Response::json(...) // permission updated
: Response::json(...); // permission added
}
It will add or update new permission for you.
I have inserted a row in db using laravel eloquent method. See the code below,
public function store() {
$language = new languages;
$language -> languages = Input::get('languages');
$language -> created_by = Auth::user()->id;
$language -> updated_by = Auth::user()->id;
if( $language -> save() ) {
$returnData = languages::where("id","=",$language -> id) -> get();
$data = array ("message" => 'Language added successfully',"data" => $returnData );
$response = Response::json($data,200);
return $response;
}
}
I want the last inserted row from the table, but my response contains empty data. Please guide the right method of getting the data?
Almost there: you would need to call first() to get just one row. But, since you're using eloquent, you can call the find() method:
public function store() {
$language = new languages;
$language->languages = Input::get('languages');
$language->created_by = Auth::user()->id;
$language->updated_by = Auth::user()->id;
if($language->save()) {
$returnData = $language->find($language->id);
$data = array ("message" => 'Language added successfully',"data" => $returnData );
$response = Response::json($data,200);
return $response;
}
}