Laravel considering variable as column name in query builder - laravel-5

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();

Related

Getting error when Import products csv in Magmi to add products in magento 1.9

Here is the error I am getting when I choose the "Create new item and Update existing ones" and run import
SKU RDDBDZE152 - 1 SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x92ll we...' for column 'value' at row 1 -
SKU RDDBDZE152 - 2 SQLSTATE[HY000]: General error: 1366 Incorrect string value: '\x92ll we...' for column 'value' at row 1 - ERROR ON RECORD #1
What this error mean ?
Well, found the reason behind this error. There was a special character in product description. Removed the special character and problem solved.

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();

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

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');

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