How can I do update in doctrine, with array, so I don't do for loop for each time (I just want to do 1 call to database)
$myarray = [1, 2, 3];
$sql = "UPDATE `mytable` SET is_processing = :is_processing, end_time=NOW() WHERE id = :id";
$result = $this->connection->executeUpdate(
$sql,
array(
'is_processing' => false,
'id' => $myarray // This is unknown number amount of array
)
);
What I'm trying to achieve is:
it should update the table with field is_processing = false, endTime become current time, where id = whatever the array point to
Use IN clause in your query.
UPDATE `mytable` SET is_processing = :is_processing, end_time=NOW() WHERE id IN(:ids)
then
$result = $this->connection->executeUpdate(
$sql,
array(
'is_processing' => false,
'ids' => [3, 25]
),
array(
'ids' => \Doctrine\DBAL\Connection::PARAM_INT_ARRAY
)
);
Related
I am trying to merge two results of two queries in MYSQL using PHP, but I am puzzled how to do it! I am using PDO. I am programming for a hobby and am trying to make a to do list app just like a Trello board. However, I just can't figure out how to merge two results from different tables in a database.
The idea is as follows:
I have a table called 'task_lists' with the content:
'list_id => 1, list_name = 'listOne'
'list_id => 2, list_name = 'listTwo'
And a table called 'tasks':
task_id => 1, list_id => 1, task_name => 'taskOfListOne', duration => 5, is_done => 1
task_id => 2, list_id => 1, task_name => 'anotherTaskOfListOne', duration => 5, is_done => 1
task_id => 3, list_id => 2, task_name => 'taskOfListTwo', duration => 10, is_done => 0
And I am trying to create an array that is merged between the two results as something like:
(I know this is a rough picture of how the array is supposed to look like)
$result = [array]
[list_id] = 1, [list_name] = 'listOne' =>
[array][list_id] = 1, ['task_name] = taskOfListOne,[duration] = 5, ['is_done'] => 1
[array][list_id] = 1, ['task_name] = anotherTaskOfListOne,[duration] = 5, ['is_done'] => 1
[list_id] = 2, [list_name] = 'listTwo' =>
[array][list_id] = 2, ['task_name] = taskOfListTwo,[duration] = 5, ['is_done'] => 1
Is this even possible? I have tried a Union sql query and methods like nested foreach statements, but none of them worked for me. Am I missing something here?
PS: Sorry for my bad english.
Have you tried a left join?
SELECT TL.`list_id`, TL.`list_name`, T.`task_name`, T.`duration`
FROM task_lists AS TL
LEFT JOIN tasks as T ON TL.`list_id` = T.`list_id`
And then in PHP you build the array in the format you want.
Later edit:
Simple PHP example to parse SQL data as you asked (to remove duplicated info):
<?php
// $mysql_rows -> here is your query result, fetched as associative array
$filtered_array = array();
foreach ($mysql_rows as $row){
// Initiate record if is not already initiated
if (!isset($filtered_array[ $row['list_id'] ])){
$filtered_array[ $row['list_id'] ] = array(
'list_id' => $row['list_id'],
'list_name' => $row['list_name'],
'tasks' => array()
);
}
// Add tasks
$filtered_array[ $row['list_id'] ]['tasks'][] = array(
'task_name' => $row['task_name'],
'duration' => $row['duration'],
'is_done ' => $row['is_done ']
);
}
// Optional: if you want to remove list_id from $filtered_array key names, uncomment the next line
// $filtered_array = array_values($filtered_array);
?>
When I insert a new record into a database table, I need to take an existing previous value of a column called el_order, add +1, and use that new el_order+1 to insert the new record with that value in the column.
I can't use autoincrement because I need to do some things with that column (reorder, move, etc) and have to use it as an integer.
Table
ID name el_order
1 1 1
21 bla 2
2 2 3
--NEW-- --NEW-- 3+1 (NEW)
I add a new record, and need to insert it with 3+1 in it's el_order column...
I have tried this, but no luck:
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio');
$eldi_key = url_title($this->input->post('id'), 'underscore', TRUE);
$el_order = $res+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
$this->db->insert('elem_diccio', $datos);
Just like this
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio')->row()->el_order;
$eldi_key = url_title($this->input->post('id'), 'underscore', TRUE);
$el_order = $res+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
$this->db->insert('elem_diccio', $datos);
$res is a CI_DB_mysqli_result Object. To get the column, you need
$this->db->select_max('el_order');
$res = $this->db->get('elem_diccio')->row();
$el_order = $res->el_order+1;
$datos = array(
'ID' => $id,
'el_order' => $el_order,
'name' => $this->input->post('name'),
);
Can't figure out why this code isn't working:
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([$SQL_values]);
And these are dumps of the two strings being inserted into those statements:
$SQL_update = UPDATE laptops SET asset_tag = :asset_tag WHERE id = :id
$SQL_values = 'asset_tag' => 5544, 'id' => 23
You missed : in your code:-
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute([':asset_tag' => 5544, ':id' => 23]);
So actually what you have to do is:-
$SQL_values =[':asset_tag' => 5544, ':id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Or
$SQL_values =['asset_tag' => 5544, 'id' => 23]; // create array like this
$update_SQL = $db->prepare($SQL_update);
$update_SQL->execute($SQL_values); // pass that array
Note:- execute won't accept a string, it must be an array.
$phql = 'SELECT id,username,email FROM Users WHERE active = :active: LIMIT :offset:, :limit:';
$users = $this->modelsManager->executeQuery($phql, array('active' => 'Y', 'offset' => 100, 'limit' => 10));
//But Generates the error SQL statement
//SELECT `users`.`id` AS `id`, `users`.`username` AS `username`, `users`.`email` AS `email` FROM `users` WHERE `users`.`active` = 'Y' LIMIT '10' OFFSET '100'
ANY one help me ? just using $this->modelsManager...,whith $phql to bind numeric parameters.
I haven't had the chance to test it out, but from the PHQL, modelsManager, and Query documentation, it seems that you can enforce it by creating a query rather than executing it straight away, and then executing it with the specific types you want:
// Create the query
$phql = 'SELECT id,username,email FROM Users WHERE active = :active: LIMIT :offset:, :limit:';
$query = $this->modelsManager->createQuery($phql);
// Setup the values to bind
$values = array(
'active' => 'Y',
'offset' => 100,
'limit' => 10
);
// Setup the type for each value
$types = array(
'active' => Phalcon\Db\Column::BIND_PARAM_STR,
'offset' => Phalcon\Db\Column::BIND_PARAM_INT,
'limit' => Phalcon\Db\Column::BIND_PARAM_INT
);
// Execute the query
$users = $query->execute($values, $types);
This is the query I use:
$idList="32,33,21,11";
$query = "SELECT post_id, meta_value FROM wp_posts
WHERE meta_key = 'grade' AND post_id IN ($idList)";
As a result, the where clause become keys:
$result = array(
0 => array('post_id'=> 32, 'meta_value'=>5),
1 => array('post_id'=> 33, 'meta_value'=>2),
2 => array('post_id'=> 21, 'meta_value'=>8),
)
I desire to have a result like this:
$result = array(
0 => array( 32 => 5 ),
1 => array( 33 => 2 ),
2 => array( 21 => 8 ),
)
How to achieve this? Thanks!
Loop through the result set like this:
$_final = array(); // new result set
foreach ($result as $value)
$_final[] = array($value['post_id'] => $value['meta_value']);
unset($result); // free memory from old result set
UPDATE: If there are too many element and you afraid of memory consumption, you can do it the following way:
$_final = array(); // new result set
foreach ($result as $key => $value) {
$_final[] = array($value['post_id'] => $value['meta_value']); // composing new data
unset($result[$key]); // unsetting already parsed value
}
unset($result); // freeing memory from old result set