Custom WHERE in storing JTable row in Joomla 1.5 - joomla

I'm using JTable to store a record in a table. My table has 3 primary keys(pid,eid,sid). I want to store (Insert,update) a record
my code:
$row =& JTable::getInstance('mytable', 'Table');
$row->load(
array(
'pid' =>$pid,
'eid' =>$eid,
'sid' =>$sid
)
);
$row->data = $data;
if (!$row->store()) {
JError::raiseError(500, $row->getError() );
}
The load function runs with warning:
Warning: mysql_real_escape_string() expects parameter 1 to be string, object given in ...\joomla\database\database\mysql.php on line 193
and the store function raise an error:
, but the store raise an error with the SQL statement. The SQL statement contains the field names and new values and 'WHERE' keyword but without a condition.
any help?

The load function takes an integer as an input (see here http://docs.joomla.org/JTable/load) so you cannot pass it an array. The integer you pass it should be the primary key of your table. Here you can use any one of your 3 primary keys, because being primary it will be unique.

Related

Update Laravel column based on another column from the same query

I want to update all row that has no 'timemodified' based on another column from the same query.
DB::table('sco_tracks')->where('timemodified', '')->update([
'timemodified' => Carbon::createFromFormat('Y-m-d H:i:s' , DB::raw('updated_at'))->timestamp
]);
With this code I get: Data missing {"exception":"[object] (InvalidArgumentException(code: 0): A four digit year could not be found
the raw query would be
UPDATE sco_tracks t SET timemodified=UNIX_TIMESTAMP(created_at) WHERE timemodified IS NULL;
the code for laravel:
DB::table("sco_tracks")->where('timemodified','')->update(['timemodified'=>DB::raw('UNIX_TIMESTAMP(updated_at)')]);
and if the value of timemodified field is null you can use this code:
DB::table("sco_tracks")->whereNull('timemodified')->update(['timemodified'=>DB::raw('UNIX_TIMESTAMP(updated_at)')]);

Error : Call to a member function where() on int

I want to update data only in pivot table in Laravel 8. All columns are foreign keys. While run the following query, the data in pivot table has been updated but Laravel form give the following error.
Error:
Call to a member function where() on int
Controller:
DB::table("student_topic_examiner")->update([
"internal_id" => $internal->id,
"external_id" => $external->id])
->where("roll_number", "=", $request->input('roll_number'));
Database pivot table is as pivot table.
Please guide.
The problem is that you are calling where() after update().
update() returns a integer (1 if something was updated, 0 otherwise).
It means that you code translates to (1)->where("roll_number", "=", $request->input('roll_number')).
If you only want to update the rows where this condition is true:
->where("roll_number", "=", $request->input('roll_number'))
Then, call it before update():
DB::table("student_topic_examiner")
->where("roll_number", "=", $request->input('roll_number'))
->update([
"internal_id" => $internal->id,
"external_id" => $external->id]
);

Laravel Failed to attach many-to-many table

Why I have got 3 values when I attach "dresscodes"?
$event->dresscode()->attach($request->get('dressCodesValue'));
this is error message
SQLSTATE[01000]: Warning: 1265 Data truncated for column 'dress_codes_id' at row 1 (SQL: insert into `dress_codes_events` (`dress_codes_id`, `events_id`) values (1,2, 28))
Here is a relation of Events table
public function dresscode()
{
return $this->belongsToMany(DressCodes::class);
}
Here are dresscode value when print out from controller $request->get('dressCodesValue') // 1,2
I fixed it.
Here is my problem
When I pass my data into FormData() and post it via axios.post It's converts my array value into string
Solution
just explode string back to array and pass that value into attach() method

Data truncated error on store method with ManyToMany relationships

I'm new to Laravel and having problems with ManyToMany relationships.
I have two tables:
members
groups
A member can belong to many groups, a group can have many members.
I've created the relationships in the models and the pivot table.
When I create a member, I have checkboxes for the groups.
In my store method I do this:
....
$member->save();
if(isset($request->groups)) {
$groups = implode(',', $request->groups);
$member->groups()->sync([$groups]);
}
dd($groups) gives: "2,7"
I get the error:
QueryException in Connection.php line 761: SQLSTATE[01000]: Warning: 1265 Data truncated for column 'group_id' at row 1 (SQL: insert into group_member (group_id, member_id) values (2,7, 5))
Where is this 5 coming from and why do I get this error?
Thank you
This error is because of the data type you entered.
Using
implode()
You turn the array into a string while sync() method wants an array as input. Let's try just:
$member->save();
if(isset($request->groups)) {
$member->groups()->sync($request->groups);
}
To avoid the inconsistency that might arise due to the comma separated string, I'd convert
$request->groups, which is a string, to a standard array before adding it to the sync function.
if(isset($request->groups)) {
$groupIds = array($request->input('groups'));
$member->groups()->sync($groupdIds);
}

Error when retrieving and updating data from database in Codeigniter

In my application controller, I have a method transaction_reimburse() that receives ids as POST string values. The method's function is to get all transactions that match the ids from the database, and update them accordingly.
Controller method:
public function transaction_reimburse() {
$reimburse = array('reimburse' => 1);
$transactions = $this->Transaction_model->get_these_ids($this->input->post('ids'));
$this->Transaction_model->reimburse_these($transactions, $reimburse);
}
Model method:
function get_these_ids($ids) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $ids .")");
return $this->db->get();
}
Model method:
function reimburse_these($transactions, $reimburse) {
$this->db->select('tbl_user.name as uname, tbl_transactions.participant_id as pid, tbl_transactions.card_number as card_no, tbl_transactions.group as ugroup, tbl_toilet.name as utoilet, tbl_transactions.amount_paid as u_amount, tbl_transactions.toilet_price as t_price, tbl_transactions.reimburse_amount as r_amount, tbl_transactions.details as u_details');
$this->db->from('tbl_transactions');
$this->db->join('tbl_user', 'tbl_user.participant_id = tbl_transactions.participant_id', 'left');
$this->db->join('tbl_toilet', 'tbl_toilet.toilet_code = tbl_transactions.toilet_code', 'left');
$this->db->where("tbl_transactions.id IN (". $transactions .")");
$this->db->update($this->tbl_transaction, $reimburse);
}
However, I am getting the following error:
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: models/transaction_model.php
Line Number: 450
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
UPDATE `tbl_transactions` SET `reimburse` = 1 WHERE `tbl_transactions`.`id` IN ()
Filename: C:\xampp\htdocs\ipa\system\database\DB_driver.php
Line Number: 330
What I need to know is why do I get these errors?
The errors mean that your $ids variable is not in a string format (meaning: 1,2,3,4. Its probably an array). To fix it, first find out what format it is on by using print_r($ids) or var_dump($ids). Once you know the format, use PHP functions(such as implode()) to transform it into strings.
IN is looking for a set in the format e.g. (1,2,3,4).
Either make your post values adopt the format 1,2,3,4 so that when the value is included into the query it's valid or if you're sending an array use a method in PHP such as implode(',',$array) to create the desired format for binding into the query..
I have reviewed your both function and found that in both function you put tbl_transactions.id in where clause and ids is passed from post and hence there is no need to call get_these_ids to get ids, you already have ids in post array and hence instead of transactions variable, just use $this->input->post('ids') for reimburse_these functions.
if you surely want to get tbl_transactions from get_these_ids function then you need to use group_concat with group by in query of get_these_ids function and just return that concated id in get_these_ids function then it will work.

Resources