cakephp 2.0 hasmany relation retrieve data - count

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

Related

How to get a nested Gutenberg block to parse

I'm looking for way to get a specific Gutenbergblock from my post content to use in the archive template. Somewhere in my single post there's an iconlist (nested in a column and the column nested in a row) that I would like to use as a summary of the page in the entry on the archive page.
I wrote this code to search for the block called 'kadence/iconlist' and render it.
<?php $post_id = get_the_ID();
$post = get_post($post_id);
$blocks = parse_blocks($post->post_content);
foreach ($blocks as $block) {
if ($block['blockName'] == 'kadence/iconlist') {
echo render_block($block);
}
} ?>
This renders nothing because the 'kadence/iconlist' is nested. A var_dump of the $block shows this structure:
Array
(
[blockName] => kadence/rowlayout
[attrs] => Array
(
[uniqueID] => _94b9e4-63
[columnGutter] => wider
[colLayout] => equal
[maxWidth] => 1220
[topPadding] => 55
[bottomPadding] => 55
[overlayFirstOpacity] => 1
[overlaySecondOpacity] => 1
[overlayOpacity] => 90
[align] => full
[firstColumnWidth] => 50
[secondColumnWidth] => 50
)
[innerBlocks] => Array
(
[0] => Array
(
[blockName] => kadence/column
[attrs] => Array
(
[topPadding] => 25
[bottomPadding] => 25
[leftPadding] => 25
[rightPadding] => 25
[uniqueID] => _91ffee-25
)
[innerBlocks] => Array
(
[0] => Array
(
[blockName] => kadence/advancedgallery
[attrs] => Array
(
[uniqueID] => _ccc1ba-38
[columns] => Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 1
[5] => 1
)
[thumbSize] => full
[ids] => Array
(
[0] => 355
[1] => 354
[2] => 357
[3] => 353
[4] => 351
[5] => 352
[6] => 349
[7] => 348
[8] => 350
)
[type] => thumbslider
[imageRatio] => square
[linkTo] => media
[gutter] => Array
(
[0] => 40
[1] =>
[2] =>
)
[imageRadius] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[autoPlay] => 1
[autoSpeed] => 4000
[lightbox] => magnific
[margin] => Array
(
[0] => Array
(
[desk] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
)
[tablet] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
[mobile] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
)
)
)
[thumbnailRatio] => square
[thumbnailColumns] => Array
(
[0] => 4
[1] => 4
[2] => 4
[3] => 4
[4] => 4
[5] => 4
)
)
[innerBlocks] => Array
(
)
[innerHTML] =>
Previous
Next
Previous
Next
[innerContent] => Array
(
[0] =>
Previous
Next
)
)
)
[innerHTML] =>
[innerContent] => Array
(
[0] =>
[1] =>
[2] =>
)
)
[1] => Array
(
[blockName] => kadence/column
[attrs] => Array
(
[id] => 2
[uniqueID] => _b56b72-c0
)
[innerBlocks] => Array
(
[0] => Array
(
[blockName] => kadence/advancedheading
[attrs] => Array
(
[level] => 1
[uniqueID] => _1f94db-5b
)
[innerBlocks] => Array
(
)
[innerHTML] =>
RVS-200HD+
[innerContent] => Array
(
[0] =>
RVS-200HD+
)
)
[1] => Array
(
[blockName] => kadence/iconlist
[attrs] => Array
(
[items] => Array
(
[0] => Array
(
[icon] => fe_check
[link] =>
[target] => _self
[size] => 20
[text] => Voordeel nummer 1 iets met emmers
[width] => 2
[color] =>
[background] =>
[border] =>
[borderRadius] => 0
[borderWidth] => 1
[padding] => 5
[style] => default
)
[1] => Array
(
[icon] => fe_check
[link] =>
[target] => _self
[size] => 20
[text] => Puntje 2
[width] => 2
[color] =>
[background] =>
[border] =>
[borderRadius] => 0
[borderWidth] => 1
[padding] => 5
[style] => default
)
[2] => Array
(
[icon] => fe_check
[link] =>
[target] => _self
[size] => 20
[text] => Puntje 3, deze zijn per bericht gewoon aan te passen
[width] => 2
[color] =>
[background] =>
[border] =>
[borderRadius] => 0
[borderWidth] => 1
[padding] => 5
[style] => default
)
[3] => Array
(
[icon] => fe_check
[link] =>
[target] => _self
[size] => 20
[width] => 2
[color] =>
[background] =>
[border] =>
[borderRadius] => 0
[borderWidth] => 1
[padding] => 5
[style] => default
[text] => En toe te voegen
)
)
[listCount] => 4
[uniqueID] => _342687-70
)
[innerBlocks] => Array
(
)
[innerHTML] =>
Voordeel nummer 1 iets met emmers
Puntje 2
Puntje 3, deze zijn per bericht gewoon aan te passen
En toe te voegen
[innerContent] => Array
(
[0] =>
Voordeel nummer 1 iets met emmers
Puntje 2
Puntje 3, deze zijn per bericht gewoon aan te passen
En toe te voegen
)
)
[2] => Array
(
[blockName] => core/block
[attrs] => Array
(
[ref] => 918
)
[innerBlocks] => Array
(
)
[innerHTML] =>
[innerContent] => Array
(
)
)
)
[innerHTML] =>
[innerContent] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
[5] =>
[6] =>
)
)
)
[innerHTML] =>
[innerContent] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
)
Array
(
[blockName] =>
[attrs] => Array
(
)
[innerBlocks] => Array
(
)
[innerHTML] =>
[innerContent] => Array
(
[0] =>
)
)
How do I have to change the if($block['blockName'] == 'kadence/iconlist' to actually get to the kadence/iconlist block?

To get the category from the post list page

I do this for get the category name and i got the array
Array ( [0] => stdClass Object ( [term_id] => 4 [name] => cinematography [slug] => cinematography [term_group] => 0 [avhec_term_order] => 1 [term_taxonomy_id] => 4 [taxonomy] => category [description] => [parent] => 0 [count] => 11 [object_id] => 2526 [cat_ID] => 4 [category_count] => 11 [category_description] => [cat_name] => cinematography [category_nicename] => cinematography [category_parent] => 0 ) )
For this i wrote
$category = get_the_category();
print_r($category);
Now how can i store the "[name] => cinematography" in a variable.
I assume $category is the array you got there, then
$cinematography = $category[0]->cinematography;
https://drupal.org/node/1245956

printing all the “maps” related to the content type

my content type, plan to have one or more 'maps'.
With the following php code, I can take all their link:
<?php print render($content['field_mappe_di_riferimento']); ?>
and I get the following result:
I would like to put before the link the image that is within the content type "Mappa", entered in the field "field_immagine_min."
this is the structure of my content type:
this is the content type "Mappa":
with: "print_r($field_mappe_di_riferimento);" I get it:
Array ( [0] => Array ( [nid] => 30 [access] => 1 [node] => stdClass Object ( [vid] => 30 [uid] => 1 [title] => Carta di Marignano, primo giorno, 13 Settembre 1515 [log] => [status] => 1 [comment] => 1 [promote] => 1 [sticky] => 0 [nid] => 30 [type] => mappa [language] => en [created] => 1352066743 [changed] => 1352127342 [tnid] => 0 [translate] => 0 [revision_timestamp] => 1352127342 [revision_uid] => 1 [field_battaglia_di_riferimento] => Array ( [und] => Array ( [0] => Array ( [nid] => 2 ) ) ) [field_immagine_std] => Array ( [und] => Array ( [0] => Array ( [fid] => 23 [alt] => [title] => [width] => 672 [height] => 554 [uid] => 1 [filename] => polesella.jpg [uri] => public://polesella_0.jpg [filemime] => image/jpeg [filesize] => 337755 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_immagine_min] => Array ( [und] => Array ( [0] => Array ( [fid] => 24 [alt] => [title] => [width] => 150 [height] => 110 [uid] => 1 [filename] => polesella_small.jpg [uri] => public://polesella_small.jpg [filemime] => image/jpeg [filesize] => 10672 [status] => 1 [timestamp] => 1352066743 [rdf_mapping] => Array ( ) ) ) ) [field_testo_opzionale] => Array ( [und] => Array ( [0] => Array ( [value] => pisello [format] => [safe_value] => pisello ) ) ) [rdf_mapping] => Array ( [rdftype] => Array ( [0] => sioc:Item 1 => foaf:Document ) [title] => Array ( [predicates] => Array ( [0] => dc:title ) ) [created] => Array ( [predicates] => Array ( [0] => dc:date 1 => dc:created ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [changed] => Array ( [predicates] => Array ( [0] => dc:modified ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) [body] => Array ( [predicates] => Array ( [0] => content:encoded ) ) [uid] => Array ( [predicates] => Array ( [0] => sioc:has_creator ) [type] => rel ) [name] => Array ( [predicates] => Array ( [0] => foaf:name ) ) [comment_count] => Array ( [predicates] => Array ( [0] => sioc:num_replies ) [datatype] => xsd:integer ) [last_activity] => Array ( [predicates] => Array ( [0] => sioc:last_activity_date ) [datatype] => xsd:dateTime [callback] => date_iso8601 ) ) [cid] => 0 [last_comment_timestamp] => 1352066743 [last_comment_name] => [last_comment_uid] => 1 [comment_count] => 0 [name] => ant [picture] => 0 [data] => b:0; ) ) )
so I tried to take the nid of the node that I want to take the picture, with success:
print ($content['field_mappe_di_riferimento']['#items'][0][nid]);
I get 30...
I found the solution:
$array = $content['field_mappe_di_riferimento']['#items'];
$k = 0;
foreach($array as $k => $v){
print($k);
//carico il nodo
$node = node_view(node_load($content['field_mappe_di_riferimento']['#items'][$k]['nid']));
//stampa l'img del nodo
print render($node['field_immagine_min']);
}

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

how to get taxonomy children id in drupal

I have a taxonomy parent tid '8'
I need to get all child tid for this parent id
$tid = 8;
$children = taxonomy_get_children($tid);
print_r($children);
am getting this result
8
Array
(
[9] => stdClass Object
(
[tid] => 9
[vid] => 3
[name] => Domestic
[description] => Domestic
[weight] => 0
)
[12] => stdClass Object
(
[tid] => 12
[vid] => 3
[name] => Economic
[description] => Economic
[weight] => 1
)
[11] => stdClass Object
(
[tid] => 11
[vid] => 3
[name] => International
[description] =>
[weight] => 2
)
[10] => stdClass Object
(
[tid] => 10
[vid] => 3
[name] => Social
[description] =>
[weight] => 3
)
)
how can I display only tid from this array.
$children = array_keys(taxonomy_get_children($tid));
You will notice it's an associative array where the tid in the object is the same as the array key - just grab the keys, it's equivalent to grabbing the tids from the objects themselves.

Resources