Symfony2 + DBAL. How to use bindValue for multiple insert? - symfony

I'm using DBAL and I want to execute multiple insert query. But I have the problem: bindValue() method not working in loop. This is my code:
$insertQuery = "INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES %s
ON DUPLICATE KEY UPDATE company=VALUES(company), user=VALUES(user)";
for ($i = 0; $i < count($data); $i++) {
$inserted[] = "(':number', ':company', ':user')";
}
$insertQuery = sprintf($insertQuery, implode(",", $inserted));
$result = $db->getConnection()->prepare($insertQuery);
for ($i = 0; $i < count($data); $i++) {
$result->bindValue($data[$i]["number"]);
$result->bindValue($data[$i]["company"]);
$result->bindValue($data[$i]["user"]);
}
$result->execute();
As result I received one-line table with fields: :number, :company, :user.
What am I doing wrong?
Thanks a lot for any help!

The problem you're having is that your binding has no way to determine to which placeholder it should be doing the binding with. To visualize it better, think on the final DBAL query you're generating:
INSERT INTO `phonebook`(`number`, `company`, `user`) VALUES
(':number', ':company', ':user'),
(':number', ':company', ':user'),
(':number', ':company', ':user');
When you do the binding, you're replacing all the parameters at the same time, ending up with a single row inserted.
One possible solution would be to give different parameter names to each row and then replace each one accordingly.
It would look like something similar to this:
public function randomParameterName()
{
return uniqid('param_');
}
...
$parameters = [];
for ($i = 0; $i < count($data); $i++) {
$parameterNames = [
'number' => $this->randomParameterName(),
'company' => $this->randomParameterName(),
'user' => $this->randomParameterName(),
];
$parameters[$i] = $parameterNames;
$inserted[] = sprintf("(':%s', ':%s', ':%s')",
$parameterNames['number'],
$parameterNames['company'],
$parameterNames['user']
);
}
$insertQuery = sprintf($insertQuery, implode(",", $inserted));
$result = $db->getConnection()->prepare($insertQuery);
foreach ($parameters as $i => $parameter) {
$result->bindValue($parameter['number'], $data[$i]["number"]);
$result->bindValue($parameter['company'], $data[$i]["company"]);
$result->bindValue($parameter['user'], $data[$i]["user"]);
}
You could probably extend your $data variable and incorporate the new parameter names into it. This would remove the need of yet another array $parameters to hold reference to the newly created parameter names.
Hope this helps

There is another alternative:
$queryStart = "INSERT INTO {$tableName} (" . implode(', ', array_keys($buffer[0])) . ") VALUES ";
$queryRows = $params = $types = [];
foreach ($rowBuffer as $row) {
$rowQuery = '(' . implode(', ', array_fill(0, count($row), '?')) . ')';
$rowParams = array_values($row);
list($rowQuery, $rowParams, $types) = SQLParserUtils::expandListParameters($rowQuery, $rowParams, $types);
$queryRows[] = $rowQuery;
$params = array_merge($params, $rowParams);
}
$query = $queryStart . implode(', ', $queryRows);
$connection->executeQuery($query, $params, $types);

Related

How to get value from a field key (Ninja Forms)

Having a try with Ninja Forms, I’m actually able to get value from a field ID using $form_data array variable.
function my_ninja_function( $form_data ) {
$my_field_id = 1;
$my_value_from_field_id = $form_data['fields'][$my_field_id]['value'];
echo $my_value_from_field_id;
// output value is possible
}
And now trying to get value from a field key, without success...
$my_field_key = 'my_key';
$my_value_from_field_key = $form_data['fields'][$my_field_key]['value'];
echo $my_value_from_field_key;
// output value is not possible
with a little more effort...
$form_fields = $form_data['fields'];
foreach( $form_fields as $field ){
$field_value = $field['value'];
$field_key = $field['key'];
$data[$field_key] = $field_value;
};
$my_value_from_key = $data['my_key'];
echo $my_value_from_key;
// output is possible
It works!
By value I'm assuming you mean the field's label. You can get a field's label from the field's settings like this:
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$label = $model['label'];
}
if you really do mean value, then perhaps you are referring to a form submission's field value. You can get those like this:
$sub_id = 1; // Need to know the submission's ID
$sub = Ninja_Forms()->form()->sub($sub_id)->get();
$form_id = 1;
$form_fields = Ninja_Forms()->form($form_id)->get_fields();
foreach( $form_fields as $field ) {
$model = $field->get_settings();
$value = $sub->get_field_value($model['key']); // User submitted value
}
Note that NinjaForms has added field keys in version 3 after I made the suggestion, as previous versions had no unique field identifier which made exporting/importing fields and forms very problematic.

Wordpress get_results parsing the symbol "&"

I have a strange Thing, where I do not know where the failure is.
$name = "Fast & Furious 8";
$res1 = $wpdb->prepare(
"
SELECT
*
FROM
wp_dbtable
WHERE
filmname = '%s'
LIMIT 1
",
$name
);
$res = $wpdb->get_results( $res1 );
foreach($res as $reseachG) {
}
I have a Problem with the Symbol &. For some reasons it does not pull anything out of my table, even if it should do this.
If I use instead of the variable the text it self, like this:
$res1 = $wpdb->prepare(
"
SELECT
*
FROM
wp_dbtable
WHERE
filmname = '%s'
LIMIT 1
",
"Fast & Furious 8"
);
it works. Also, other text inside the variable works well.
So it seems that prepare, or get_results does not accept this Symbol, or changes it. How can I solve it? I couldn't find any hint on the Internet.
It might not be a PHP-Problem, or a My-SQL-Problem, it might be a Problem of WordPress classes.
Thanks a lot.
The code above is correct. The Problem was, that I pulled the text for the filmname out of the data base. He transformed the & into html-special-chars. So I had to run: htmlspecialchars_decode($filmname);. It was not easy to find, becase every print method printed the correct text.
I found an function on the Internet, which shows me hidden chars and every single real entiti. Maybe it will help some of you also:
function hexdump ($data, $htmloutput = true, $uppercase = false, $return = false){
// Init
$hexi = '';
$ascii = '';
$dump = ($htmloutput === true) ? '<pre>' : '';
$offset = 0;
$len = strlen($data);
// Upper or lower case hexadecimal
$x = ($uppercase === false) ? 'x' : 'X';
// Iterate string
for ($i = $j = 0; $i < $len; $i++)
{
// Convert to hexidecimal
$hexi .= sprintf("%02$x ", ord($data[$i]));
// Replace non-viewable bytes with '.'
if (ord($data[$i]) >= 32) {
$ascii .= ($htmloutput === true) ?
htmlentities($data[$i]) :
$data[$i];
} else {
$ascii .= '.';
}
// Add extra column spacing
if ($j === 7) {
$hexi .= ' ';
$ascii .= ' ';
}
// Add row
if (++$j === 16 || $i === $len - 1) {
// Join the hexi / ascii output
$dump .= sprintf("%04$x %-49s %s", $offset, $hexi, $ascii);
// Reset vars
$hexi = $ascii = '';
$offset += 16;
$j = 0;
// Add newline
if ($i !== $len - 1) {
$dump .= "\n";
}
}
}
// Finish dump
$dump .= $htmloutput === true ?
'</pre>' :
'';
$dump .= "\n";
// Output method
if ($return === false) {
echo $dump;
} else {
return $dump;
}
}
Which shows you the hexa-codes for each char and also every single space, line, special trasformed symbol, or line-braker. It helps to see the real data.
thx to all.

Alter search query condition for dataset in Drupal 7

I am very new to Drupal and attempting to build a module that will allow admins to tag nodes with keywords to boost nodes to the top of the search results.
I have a separate DB table for the keywords and respective node IDs. This table is UNIONed with the search_index table via hook_query_alter...
function mos_search_result_forcer_query_alter(QueryAlterableInterface &$query) {
if (get_class($query) !== 'PagerDefault') { //<< check this because this function will mod all queries elsewise
return;
}
// create unioned search index result set...
$index = db_select('search_index', 's');
$index->addField('s', 'sid');
$index->addField('s', 'word');
$index->addField('s', 'score');
$index->addField('s', 'type');
$msrfi = db_select('mos_search_result_forcer', 'm');
$msrfi->addField('m', 'nid', 'sid');
$msrfi->addField('m', 'keyword', 'word');
$msrfi->addExpression('(SELECT MAX(score) + m.id / (SELECT MAX(id) FROM {mos_search_result_forcer}) FROM {search_index})', 'score');
$msrfi->addExpression(':type', 'type', array(':type' => 'node'));
$index->union($msrfi);
$tables =& $query->getTables();
$tables['i']['table'] = $index;
return $query;
}
Drupal then generates the almost correct query...
SELECT
i.type AS type, i.sid AS sid, SUM(CAST('10' AS DECIMAL) * COALESCE(( (12.048628015788 * i.score * t.count)), 0) / CAST('10' AS DECIMAL)) AS calculated_score
FROM (
SELECT
s.sid AS sid, s.word AS word, s.score AS score, s.type AS type
FROM
search_index s
UNION SELECT
m.nid AS sid, m.keyword AS word, (
SELECT
MAX(score) + m.id / (SELECT MAX(id) FROM mos_search_result_forcer)
FROM
search_index
) AS score, 'node' AS type
FROM
mos_search_result_forcer m
) i
INNER JOIN node n ON n.nid = i.sid
INNER JOIN search_total t ON i.word = t.word
INNER JOIN search_dataset d ON i.sid = d.sid AND i.type = d.type
WHERE (n.status = '1')
AND( (i.word = 'turtles') )
AND (i.type = 'node')
/* this is the problem line... */
AND( (d.data LIKE '% turtles %' ESCAPE '\\') )
/* ...end problem line */
GROUP BY i.type, i.sid
HAVING (COUNT(*) >= '1')
ORDER BY calculated_score DESC
LIMIT 10 OFFSET 0
...I need that "problem line" to read...
AND( (d.data LIKE '% turtles %' ESCAPE '\\') OR (d.sid IN (SELECT nid FROM mos_search_result_forcer)) )
...what hook can I use to add that OR condition?
I don't want to hack Drupal's core.
I don't want to change the union/subqueries (not my decision).
I will optimize queries later - functionality is more important.
Thanks, smart people!
The basic principle is to grab the conditions array, loop through and find the index for the problem condition, remove it, then re-add an identical condition along with the new one as part of a db_or()
This is un-tested and most likely won't work verbatim, but it should give you a starting point:
$conditions =& $query->conditions();
$index = FALSE;
$removed_condition = NULL;
for ($i = 0, $l < count($conditions); $i < $l; $i++) {
if ($conditions[$i]['field'] == 'd.data' && $conditions[$i]['operator'] == 'LIKE') {
$index = $i;
$removed_condition = $condition;
break;
}
}
if ($index !== FALSE) {
unset($conditions[$index]);
$sub_query = db_select('mos_search_result_forcer')->fields('mos_search_result_forcer', array('nid'));
$new_condition = db_or()
->condition('d.data', $removed_condition['value'], 'LIKE')
->condition('d.sid', $sub_query, 'IN');
$query->condition($new_condition);
}
Thanks to some helpful suggestions from #Clive with hook_module_implements_alter, and a lot of trial and error, I have finally solved this issue.
Here is the final code...
function mos_search_result_forcer_module_implements_alter(&$imps, $hook) {
if ($hook !== 'query_alter' || !array_key_exists('mos_search_result_forcer', $imps)) {
return;
}
$imp = $imps['mos_search_result_forcer'];
unset($imps['mos_search_result_forcer']);
$imps['mos_search_result_forcer'] = $imp;
}
function mos_search_result_forcer_query_alter(QueryAlterableInterface &$query) {
if (get_class($query) !== 'PagerDefault') { //<< check this because this function will mod all queries elsewise
return;
}
// create unioned search index result set...
$index = db_select('search_index', 's');
$index->addField('s', 'sid');
$index->addField('s', 'word');
$index->addField('s', 'score');
$index->addField('s', 'type');
$msrfi = db_select('mos_search_result_forcer', 'm');
$msrfi->addField('m', 'nid', 'sid');
$msrfi->addField('m', 'keyword', 'word');
$msrfi->addExpression('(SELECT MAX(score) + m.id / (SELECT MAX(id) FROM {mos_search_result_forcer}) FROM {search_index})', 'score');
$msrfi->addExpression(':type', 'type', array(':type' => 'node'));
$index->union($msrfi);
$tables =& $query->getTables();
$tables['i']['table'] = $index;
// needs special "or" condition to keep from filtering out forced resutls...
class MSRFPagerDefaultHelper extends PagerDefault { //<< override to gain access to protected props
static function msrfHelp(PagerDefault &$pagerDefault) {
$searchQuery =& $pagerDefault->query;
MSRFSearchQueryHelper::msrfHelp($searchQuery);
}
}
class MSRFSearchQueryHelper extends SearchQuery { //<< override to gain access to protected props
static function msrfHelp(SearchQuery &$searchQuery) {
$conditions =& $searchQuery->conditions;
$condition = db_or()->condition($conditions)->condition('d.sid', db_select('mos_search_result_forcer')->fields('mos_search_result_forcer', array('nid')), 'IN');
$searchQuery->conditions = $condition;
}
}
MSRFPagerDefaultHelper::msrfHelp($query);
return $query; //<< i don't think this is needed as var is reffed - just for good measure, i guess
}

Wrap the code to translate with wordpress/poedit?

Current I have WP code like this. I need to make it translateable by poedit. How do I wrap the code to make it work? Im not sure which method is use for this case. Some thing like:
<?php my_e( 'Total sales' ); ?> or __('Total sales', 'my')
This is the code. I need to translate ["Sales amount"], ["Number of sales"]
foreach ($results as $result) {
$date = $result->formatted_post_date;
$statistics[$date]["Sales amount"] += $wp_list_table->column_total_sales($result->ID);
$statistics[$date]["Number of sales"]++;
$statistics[$date]["date"] = $date;
$max_number_of_sales = max(array($max_number_of_sales,$statistics[$date]["Number of sales"] )); }
Thank you for help
You have to use __('string','textdomain') to assign a translated string to some variable. And _e('string','textdomain') to echo a translated string. See I18n_for_WordPress_Developers.
Two observations:
you'll not be able to translate array keys, see php.net/manual/en/language.types.array.php
what you're doing seems wrong. I'd do it like:
$sales_amount = 0;
$sales_number = 0;
foreach ($results as $result) {
$sales_amount += $wp_list_table->column_total_sales($result->ID);
$sales_number++;
$date = $result->formatted_post_date;
$statistics[$date]["sales_amount"] = $sales_amount;
$statistics[$date]["sales_number"] = $sales_number;
}
echo __( 'Sales Amount', 'my' ) . $sales_amount;

How to execute a php loop in an andWhere clause using QueryBuilder?

I would like to execute this type of query using QueryBuilder in my FakeRepository.php (it's for a search form where the user can check some boxes).
if (sizeof($p['types']) > 0) {
$qb->andWhere(
foreach ($p['types'] as $type_id)
{'type.id=' .$type_id.' OR ' }
'1=0');
}
But I have an error with my syntax but I don't know how to fix it :
Parse error: syntax error, unexpected T_FOREACH, expecting ')' in /MyBundle/Entity/FakeRepository.php
Thanks a lot for your help
You need to first construct your OR condition and then pass it to the query builder
$first = true;
$orQuery = '';
foreach ($p['types'] as $type_id)
if ($first) {
$first = false;
} else {
$orQuery .= ' OR ';
}
$orQuery .= 'type.id=' .$type_id;
}
$qb->andWhere($orQuery);
You can also resove this problem:
$arr = array();
foreach ($p['types'] as $type_id){
$arr[] = $qb->expr()->orX("type.id = $type_id");
}
$qb->andWhere(join(' OR ', $arr));
Another solution to keep QueryBuilder functionality
$orX = $qb->expr()->orX();
foreach ($types as $key => $type) {
$orX->add($qb->expr()->eq('types', ':types'.$key));
$qb->setParameter('types'.$key, $type->getId());
}
$qb->andWhere($orX);

Resources