Drupal question - using drupal_execute for multivalue fields? - drupal

I'm using drupal_execute to save a node programmatically, and for the most part, it works fine, except when it comes to a multi-value field.
What gets posted is this (I'm just including the portion that isn't working):
[alt] => Array
(
[0] => Array
(
[name] => Sam I. Am
[phone] => (650) 5553131
)
[1] => Array
(
[name] => The Lorax
[phone] => 6505553344
)
[2] => Array
(
[name] =>
[phone] =>
)
)
When I'm setting the $form_state['values'], I'm using:
for($a = 0; $a < count($_REQUEST['alt']); $a++) {
$form_state['values']['field_alternativename'][$a]['value'] = check_plain($_REQUEST['alt'][$a]['name']);
$form_state['values']['field_alternativephone'][$a]['value'] = format_phone($_REQUEST['alt'][$a]['phone']);
}
And to save the node:
drupal_execute('info_node_form', $form_state, $node);
As a test, to make sure that I'm referencing the appropriate fields, I edited an existing node using node/X/edit and printed out the $form_state['values'] upon submission. This is what it printed out:
//output of print '<pre>'; print_r($form_state['values']); print '</pre>';
[field_alternativename] => Array
(
[0] => Array
(
[value] => Sam I. Am
[_error_element] => group_alternative_contacts][0][field_alternativename][value
[_weight] => 0
[_remove] => 0
)
[1] => Array
(
[value] => The Lorax
[_error_element] => group_alternative_contacts][1][field_alternativename][value
[_weight] => 1
[_remove] => 0
)
)
[field_alternativephone] => Array
(
[0] => Array
(
[value] => (650) 5553131
[_error_element] => group_alternative_contacts][0][field_alternativephone][value
[_weight] => 0
[_remove] => 0
)
[1] => Array
(
[value] => (650) 5553344
[_error_element] => group_alternative_contacts][1][field_alternativephone][value
[_weight] => 1
[_remove] => 0
)
)
So, I'm not understanding why it isn't being saved... I'm not setting the delta, but I didn't think I'd have to? In mysql, the data is stored as:
mysql> select * from content_field_alternativename ;
+-------+-------+-------+-----------------------------+
| vid | nid | delta | field_alternativename_value |
+-------+-------+-------+-----------------------------+
| 22433 | 22433 | 0 | Sam I. Am |
+-------+-------+-------+-----------------------------+
mysql> select * from content_field_alternativephone;
+-------+-------+-------+------------------------------+
| vid | nid | delta | field_alternativephone_value |
+-------+-------+-------+------------------------------+
| 22433 | 22433 | 0 | (650) 5553131 |
+-------+-------+-------+------------------------------+

The delta is how cck stores multiple values. nid x, vid x, delta 0 is the first in the multivalue field nid x, vid x, delta 1 is the second etc. It's required so if you add deltas to your multi-values it should work.

Related

wp_query and meta_query for repeater field values

I have a word press site with custom fields.
I need to query a repeater field as a table. It looks like that:
| param_1 | param_2 | param_2 | param_4
| 20 | 20 | 20 | 20
| 555 | 680 | 56 | 0
| 5555 | 45 | 56 | 1
| 69 | 0 | 45 | 0
I need to query this repeater to get a result only if it match the query in the same line
My meta query looks like that:
[post_type] => product
[posts_per_page] => -1
[orderby] => title
[order] => ASC
[meta_query] => Array (
[relation] => AND
[0] => Array (
[key] => BBBproduct_params_values_AAA_param_value_0
[value] => 25
[compare] => <=
[type] => NUMERIC
)
[1] => Array (
[key] => BBBproduct_params_values_AAA_param_value_1
[value] => 56
[compare] => >=
[type] => NUMERIC
)
[2] => Array (
[key] => BBBproduct_params_values_AAA_param_value_2
[value] => 56
[compare] => >=
[type] => NUMERIC
)
[3] => Array (
[key] => BBBproduct_params_values_AAA_param_value_3
[value] => 0
[compare] => =
[type] => NUMERIC
)
)
She is created trough this code:
if( $product_search_by_param_active ){
function my_posts_where( $where ){
$where = str_replace("AAA_param_value_", "%_param_value_", $where);
$where = str_replace("meta_key = 'BBBproduct_params_values_", "meta_key LIKE 'product_params_values_", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
$range_angine_settings = get_field('param_search_range_settings');
$search_args['meta_query'] = array();
$search_args['meta_query'] ['relation'] = 'AND';
foreach( $range_angine_settings as $rangekey => $search_range ){
${'range-'.$rangekey} = $_GET['range-'.$rangekey]? $_GET['range-'.$rangekey] : '0';
if ( $_GET['range-'.$rangekey] ) $has_range_query = true;
//$pre_prama_value = 'product_params_values_'.strval($rangekey).'_param_value_AAA';
if( isset($_GET['range-'.$rangekey])){
$search_args['meta_query'][] = array(
"key" => 'BBBproduct_params_values_AAA_param_value_'.strval($rangekey),
"value" => $search_range['search_metric_ranges'][${'range-'.$rangekey}]['range_value'],
"compare" => $search_range['product_search_logic'],
'type' => 'NUMERIC'
);
}
}
}
The problem is that this because of the '%' I get result even if the query match not on the same line.
In the example above it is intended not to get any results, but as every condition is met on a different line I do get a result.
Is there a way to create this king of query to give results by line number?
For me I prefer using CMB2 for custom fields
https://github.com/CMB2/CMB2

How to get value from multidimensional array

Array ( [0] => Array ( [data] => Array ( [0] => Array ( [like_info] => Array ( [like_count] => 30 ) [comment_info] => Array ( [comment_count] => 6 ) [share_count] => 17 [attachment] => Array ( [description] => Mitkkk kkssk [media] => Array ( [0] => Array ( [src] => com.jpg&cfs=1 ) ) [name] => euch ) [permalink] => example.com/47457343655 [created_time] => 1925 ) ) ) )
how to get value from all the column in variable listed here
like_count
share_count
comment_count
description
src
permalink
created_time
You can make a vector of variables, i mean a single array:
$var = array();
Then you can do a double loop to get all the values of the multi dimensional array and store them on the vector, like:
for($i = 0; i< count($yourarray); i++){
if(is_array($yourarray[i])){
for($j = 0; j< count($yourarray[i]); j++) $var[] = $yourarray[i][j];
}else $var[] = $yourarray[i];
}

How to retrieve child records from database using Symfony2?

I have two tables, a map_families table and a map_contacts table. There is a one to many relationship between these two tables and when a Family record is displayed I'd like to display any child records it has from the map_contacts table.
At the moment though, I have this code:
$familylist = $dm->createQuery('
SELECT mf.id, mf.surname, mf.firstName, mf.email, mf.phone, mc.id AS memID, mc.firstName AS memFirstName, mc.surname AS memSurname
FROM MyBundle:MapFamilies mf
LEFT JOIN MyBundle:MapContacts mc WHERE mf.id = mc.family
WHERE mf.centre = :centre
ORDER BY mf.surname ASC, mf.firstName ASC'
)->setParameter('centre', $centre);
$famlist = $familylist->getResult();
And this gives the following result:
Array
(
[0] => Array
(
[id] => 4
[surname] => Kentucky
[firstName] => John
[email] => email#somewhere.com
[phone] => 0123 456 7890
[memID] => 2
[memFirstName] => Jason
[memSurname] => Kentucky
)
[1] => Array
(
[id] => 4
[surname] => Kentucky
[firstName] => John
[email] => email#somewhere.com
[phone] => 0123 456 7890
[memID] => 3
[memFirstName] => Michael
[memSurname] => Kentucky
)
)
But what I'm trying to get is something like this:
Array
(
[0] => Array
(
[id] => 4
[surname] => Kentucky
[firstName] => John
[email] => email#somewhere.com
[phone] => 0123 456 7890
[members] => Array
(
[0] => Array
(
[memID] => 2
[memFirstName] => Mick
[memSurname] => Burke
),
[1] => Array
(
[memID] => 3
[memFirstName] => John
[memSurname] => Burke
)
)
)
)
How can I do this through Symfony2?
To do what you're after in querybuilder.
$qb = $this->getEntityManager()->createQueryBuilder();
$result = $qb->select('mf, mc')
->from('MyBundle:MapFamilies', 'mf')
->join('mf.mapContacts', 'mc')
->where('mf.centre = :centre')
->setParameter('centre', $centre)
->getQuery()->getArrayResult();
This will give you an array of MapFamily, with their relevant MapContact entities.
I took a guess at your property name for MapFamily -> MapContact so you might need to change that.

how extract values from multidimensional array

I have this multidimensional array:
Array
(
[car] => Array
(
[responsecode] => 200
[ford] => Array
(
[start] => 0
[count] => 20
[model] => 972000
[results] => Array
(
[0] => Array
(
[date] =>
[clickurl] => xx
[url] => xx
[dispurl] => xx
[title] => xx
[abstract] => xx
)
[1] => Array
(
[date] =>
[clickurl] => xx
[url] => xx
[dispurl] => xx
[title] => Txx
[abstract] => xx
)
I need retrieve value from [model] (972000)
Its really hard for me. Thanks in advance!
if you have your array assigned to a variable say $arry then it would be
$arry['car']['ford']['model']
there are other techniques to get the 'model' from every 'car' in an array, is that what you are looking for?
<?php
$array = array(
"foo" => "bar",
42 => 24,
"multi" => array(
"dimensional" => array(
"array" => "foo"
)
)
);
var_dump($array["foo"]);
var_dump($array[42]);
var_dump($array["multi"]["dimensional"]["array"]);
?>
http://php.net/manual/en/language.types.array.php

cakephp 2.0 hasmany relation retrieve data

I have two model name image and comment. here the relation is like image has many comments . Now in my listing page I want to display all imae detail and only the no of comment on that image . can you tell me how should I get that ?
after I write the query my return data is
Array
(
[0] => Array
(
[Image] => Array
(
[image_id] => 57
[user_id] => 1
[category_id] => 22
[image_title] => scroul
[description] => beutifull natural image for the animal
[keyword] => scrual
[image_price] =>
[image_name] => 7bf4a72509da5906903c84e88228b9dd.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 12
[size] => 3244
[resolution] => 2162 x 1644
[i_date] => 1348573022
[i_by] => 1
[u_date] => 1348573022
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 5
[image_id] => 57
[user_id] => 2
[comment] => socute
[comment_date] => 1348739230
[is_active] => N
[is_deleted] => N
)
)
)
[1] => Array
(
[Image] => Array
(
[image_id] => 56
[user_id] => 1
[category_id] => 22
[image_title] => cute dog
[description] => cute dog looking
[keyword] =>
[image_price] =>
[image_name] => d4af899b0d52cccbec94952a3abd0077.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 8
[size] => 620
[resolution] => 2592 x 1944
[i_date] => 1348572897
[i_by] => 1
[u_date] => 1348572897
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 3
[image_id] => 56
[user_id] => 2
[comment] => ohhhhhhh
[comment_date] => 1348737968
[is_active] => N
[is_deleted] => N
)
)
)
[3] => Array
(
[Image] => Array
(
[image_id] => 55
[user_id] => 1
[category_id] => 22
[image_title] => ships
[description] => ships with beutiful green background
[keyword] => ship,green,animal,nature,background,eating,white ship
[image_price] =>
[image_name] => c0dfc2432ae047e9160f3ef99880fe87.jpg
[image_path] => img/uploads/images/original/
[image_available_size] =>
[like] => 1
[size] => 1831
[resolution] => 2520 x 1944
[i_date] => 1348572846
[i_by] => 1
[u_date] => 1348661976
[u_by] => 1
[is_active] => Y
[is_deleted] => N
)
[Comment] => Array
(
[0] => Array
(
[comment_id] => 2
[image_id] => 55
[user_id] => 2
[comment] => i like it
[comment_date] => 1348737942
[is_active] => Y
[is_deleted] => N
)
[1] => Array
(
[comment_id] => 4
[image_id] => 55
[user_id] => 2
[comment] => good scene
[comment_date] => 1348738004
[is_active] => N
[is_deleted] => N
)
)
)
)
in the above array there is a all comment of that image. I don't want here comment list I just want no of comments .
Could you post the find statement you're using. You can set recursive to a certain level, from the documentation:
-1 Cake fetches Group data only, no joins.
0 Cake fetches Group data and its domain
1 Cake fetches a Group, its domain and its associated Users
2 Cake fetches a Group, its domain, its associated Users, and the Users’ associated Articles
So you can call the following if you only want the data from the current model only:
$this->Model->find('all', array('recursive' => -1));
There is also the Containable behavior, which allows you to specify which Model data you want to retrieve when calling find.
Say you have a Post model which hasMany Image. The call, with the Containable behaviour in the Post Model properly included, would be:
$this->Post->find('all', array(
'conditions' => array('Post.id' => 1),
'contain' => array('Image')
));
EDIT:
Because of the spelling and formatting I misread your initial question. I thought you wanted "no comments" to appear in the data array, instead you want only the "number of comments" to appear.
If you want the comment count, use counterCache as Kishor Kundan proposes.
Cake offers another amazing magic, "counterCache".
You can define the counterCache in your Comments model
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true
);
Then you add a field, 'comment_count' in your images table (or the table which is being used by the model Image) and the cake will do the rest for you.
This does add an overhead every time a comment is added/deleted but it is far better alternative than to issue an 'count' every time you fetch image data.
For more info you can check the cookbook. Look out for "counterCache" there.
UPDATE:
To limit the scope for counter cache, use additional attribute 'counterScope' as
public $belongsTo = array(
'className' => 'Image',
'foreignKey' => <your_foreign_key>,
...
...
'counterCache' => true,
'counterScope' => array('Image.active' => 1)
);

Resources