CI_session and Error Number: 1054 (Unknown column ...) - codeigniter

I have got problem with ci_session. When I am creating query with Active Record and I use session, it throws me Error Number: 1054.
It is caused by Session.php . I am using database session and when it executes sess_write(), then it throws error.
Can I create new instance of $this->db ?
Error is at line no. 289 in Session.php
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
In this place is set $this->CI->db->order_by(‘something’), from my code, before I call $this->session and this is my problem.
EDIT (13:12):
Unknown column 'contract.created' in 'order clause'
UPDATE `ci_sessions` SET `last_activity` = 1393330303, `user_data` = 'some_data' WHERE `session_id` = 'ea330d7194f902b6b38b88d509766560' ORDER BY `contract`.`created` DESC LIMIT 30
Filename: C:\Apache24\htdocs\Trokadero_v5\system\database\DB_driver.php
EDIT 2:
I am sure, it is happening by Active Record , because sequence of AR tasks is:
My_controller.php
$this->db->order_by('contract.created', 'DESC');
$this->session->set_flashdata('order_by_direction', 'DESC');
And then is executing in Session.php, with my own order_by clause
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));

I got it.
$this->session->userdata();
$this->session->set_userdata();
etc.
MUST BE used before/after execute your own CRUD ($this->db->...).

In your controller, you have to reset the db query.
$this->db->order_by('contract.created', 'DESC');
// add line of code to get data. After getting data, reset the db object.
$this->db->_reset_write();
$this->session->set_flashdata('order_by_direction', 'DESC');

Related

Laravel Illegal operator and value combination on where clause

There are more than 10 controllers in my project. They all have the same code, only the table names are different.
Exactly 2 of these controllers have a problem with the shared host that is 'Illegal operator and value combination'. Surprisingly other controllers works without making such a mistake.
Maybe I overlooked something.
That's where the 'Illegal operator and value combination' problem comes from.
$tarifs = DB::table('tarifs as tarif')
->join('companies as comp', 'tarif.company_id','=', 'comp.id')
->join('categories as cats', 'tarif.catid', '=', 'cats.id')
->where('tarif.company_id', '<', env('COMKEY'))
->get(['tarif.*', 'comp.name', 'comp.photo as cimage','cats.cat_uz']);
Showing this line as an error ->where('tarif.company_id', '<', env('COMKEY'))
This one works without errors.
$minutes = DB::table('minutes as minute')
->join('companies as comp', 'minute.company_id','=', 'comp.id')
->join('categories as cats', 'minute.catid', '=', 'cats.id')
->where('minute.company_id', '<', env('COMKEY'))
->get(['minute.*', 'comp.name', 'cats.cat_uz']);
The project is running normally and without errors on localhost.
Shared and local php versions are the same 7.4
When env('COMKEY') returns the null you will get this error:
InvalidArgumentException with message 'Illegal operator and value combination.'
Code:
$minutes = DB::table('tarifs as tarif')
->join('companies as comp', 'tarif.company_id','=', 'comp.id')
->join('categories as cats', 'tarif.catid', '=', 'cats.id')
->when($comKey,function($query) use ($comKey){
logger()->info('Comkey value is not null',[
'comKey'=> $comKey
]);
return $query->where('tarif.company_id', '<', env('COMKEY'));
},function($query) use ($comKey){
logger()->info('Comkey value is null');
return $query;
})
->dd();
Please use the above query to debug your query.
If the value is returned from env the log will be something like
Comkey value is not null {"comKey":"RandomKey"}
If the value is returned from env the log will be something like:
Comkey value is null

Laravel considering variable as column name in query builder

I am using laravel 5.5 and checking event checking if slot available between 2 times using this query:
$schedules->where('id', $id)
->wherebetween($date, ['start','end'])
->orwherebetween($endTime, ['start','end'])
->orwherebetween('start', [$date,$endTime])
->orderBy('start')->get();
getting this error
"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column
'2019-03-06 13:00:00' in 'where clause'
its because the 1st parameter should be the name of the column:
you should change your code to:
->wherebetween('column name', ['1st date','2nd date'])
->orwherebetween('column name', ['1st date','2nd date'])
I have fixed that by adding DB::raw() read more
$schedules->where('id', $id)
->wherebetween(DB::raw($date), ['start','end'])
->orwherebetween(DB::raw($endTime), ['start','end'])
->orwherebetween('start', [$date,$endTime])
->orderBy('start')->get();

Laravel postgresql join error. "Operator does not exist"

My laravel query code
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name', 'sample_point_record.animal_name_id', '=','animal_name.tid::VARCHAR)')
->leftJoin('geographic_unit', 'sample_point_record.geography_unit_id', '=','geographic_unit.tid::VARCHAR');
$query->get();
error message ==>
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer
So I try to add the character it needs
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name', 'sample_point_record.animal_name_id', '=','animal_name.tid::VARCHAR')
->leftJoin('geographic_unit', 'sample_point_record.geography_unit_id', '=','geographic_unit.tid::VARCHAR');
$query->get();
error message :
SQLSTATE[42703]: Undefined column: 7 ERROR: column animal_name.tid::VARCHAR does not exist
I don't think you can use raw Postgres SQL inside the a regular Laravel join function. But, you can (and probably have to) use raw SQL with DB::raw. Try this version:
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name a', function($join) {
$join->on(DB::raw("a.tid::VARCHAR"), DB::raw("="), DB::raw("sample_point_record.animal_name_i")) })
->leftJoin('geographic_unit g', function($join) {
$join->on(DB::raw("g.tid::VARCHAR"), DB::raw("="), DB::raw("sample_point_record.geography_unit_id")) })
$query->get();

Codeigniter showing problem with date function

I want to insert values into db. I have a table as registration_patients. My code is:
$data = array(
'patients_first_name' => $this->input->post('first_name'),
'patients_last_name' => $this->input->post('last_name'),
'patients_email' => $this->input->post('email'),
'patients_password'=> $this->input->post('password'),
'patients_birth_date'=> date('Y-m-d', strtotime($this->input->post('day') . "-" .$this->input->post('month') . "-" . $this->input->post('year'))),
'patients_sex'=> $this->input->post('sex'),
'patients_telephone'=> $this->input->post('telephone'),
'patients_codice_fiscale'=> $this->input->post('codice_fiscale'),
'account_active'=> 0
);
return $this->db->insert('registration_patients', $data);
Now it is inserted values into database bus showing some error and warning like that
A PHP Error was encountered
Severity: Warning
Message: strtotime(): It is not safe to rely on the system's timezone
settings. You are required to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those
methods and you are still getting this warning, you most likely
misspelled the timezone identifier. We selected 'Europe/London' for
'BST/1.0/DST' instead
Filename: models/doctors_model.php
Line Number: 22 and A PHP Error was encountered
Severity: Warning
Message: date(): It is not safe to rely on the system's timezone
settings. You are required to use the date.timezone setting or the
date_default_timezone_set() function. In case you used any of those
methods and you are still getting this warning, you most likely
misspelled the timezone identifier. We selected 'Europe/London' for
'BST/1.0/DST' instead
Filename: models/doctors_model.php
Line Number: 22
In your root_folder/index.php write date_default_timezone_set('America/New_York'); and see if the problem is solved or not. Here is the link of available timezones.

Programmatically remove attribute from attribute-set from models

I’ve tried removing an eav_entity_attribute using this code:
$entAttr = Mage::getModel('eav/entity_attribute')->getCollection()
->addFieldToFilter('attribute_set_id',$set->getId())
->addFieldToFilter('attribute_id',$attId)->getFirstItem();
$entAttr->delete();
but it doesn’t work. I receive this error: Column not found: 1054 Unknown column ‘attribute_set_id’ in ‘where clause’’
How can I delete an eav_entity_attribute this way?
Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product','attribute_name')->delete();
or to delete it directly from the attribute set:
$setup = Mage::getModel('eav/entity_setup);
$setup->startSetup();
$setup->deleteTableRow('eav/entity_attribute',
'attribute_id',$setup->getAttributeId('catalog_product','attribute_code_here'),'attribute_set_id',$setup->getAttributeSetId('catalog_product', 'Default')
);
$setup->endSetup();

Resources