CakePHP contain key issue - cakephp

Hi i wrote contain on my model it effects only on first record and the rest of them
contains all the unnecessary records.
My Controller logic :
$findFiles['conditions']['Content.created_by'] = $curUser;
$findFiles['conditions']['KmpContent.is_approved'] = false;
$findFiles['conditions']['KmpContent.is_file'] = true;
$findFiles['contain'] = array('Parent' => array('SectionApprover' => array('Approver')));
$findFiles['recursive'] = 3;
$findFiles['fields'] = array('id', 'file_path', 'url_path', 'is_approved', 'name', 'created', 'approved_by','parent_id','display_text','is_active');
$fileData = $this->KmpContent->find('all', $findFiles);
The array Details are as follows
0 =>
array (
'KmpContent' =>
array (
'id' => '881',
'file_path' => '/uploads/test_file_for_multiple_approvers_backend_validations.docx',
'url_path' => '/uploads/test_file_for_multiple_approvers_backend_validations.docx',
'is_approved' => '0',
'name' => 'file_test_file_for_multiple_approvers_backend_validations.docx',
'created' => '2013-08-20 11:49:58',
'approved_by' => NULL,
'parent_id' => '143',
'display_text' => 'test file for multiple approvers',
'is_active' => '1',
),
'Parent' =>
array (
'id' => '143',
'name' => 'sct_Services.html',
'display_text' => 'Services',
'Parent' =>
array (
),
'Uploader' =>
array (
),
'SectionApprover' =>
array (
0 =>
array (
'id' => '104',
'kmp_content_id' => '143',
'approver_id' => '882',
'Approver' =>
array (
'id' => '882',
'first_name' => 'Prakasha',
'last_name' => 'Matte',
),
),
1 =>
array (
'id' => '256',
'kmp_content_id' => '143',
'approver_id' => '1168',
'Approver' =>
array (
'id' => '1168',
'first_name' => 'Rima Roy',
'last_name' => 'Chowdhury',
),
),
2 =>
array (
'id' => '257',
'kmp_content_id' => '143',
'approver_id' => '1171',
'Approver' =>
array (
'id' => '1171',
'first_name' => 'Durga Venkatesh',
'last_name' => 'Sambhani',
),
),
3 =>
array (
'id' => '258',
'kmp_content_id' => '143',
'approver_id' => '1172',
'Approver' =>
array (
'id' => '1172',
'first_name' => 'Gowtham Babu',
'last_name' => 'Tummala',
),
),
4 =>
array (
'id' => '259',
'kmp_content_id' => '143',
'approver_id' => '889',
'Approver' =>
array (
'id' => '889',
'first_name' => 'Sandeep',
'last_name' => 'Pathuri',
),
),
),
),
'SectionApprover' =>
array (
),
),
1 =>
array (
'KmpContent' =>
array (
'id' => '882',
'file_path' => '/uploads/test_file_for_ma_contentcontroller_save.txt',
'url_path' => '/uploads/test_file_for_ma_contentcontroller_save.txt',
'is_approved' => '0',
'name' => 'file_test_file_for_ma_contentcontroller_save.txt',
'created' => '2013-08-20 11:54:15',
'approved_by' => NULL,
'parent_id' => '28',
'display_text' => 'test file for MA',
'is_active' => '1',
),
'Parent' =>
array (
'id' => '28',
'name' => 'sct_domain.html',
'display_text' => 'Domains',
'Parent' =>
array (
),
'Uploader' =>
array (
),
'SectionApprover' =>
array (
0 =>
array (
'id' => '26',
'kmp_content_id' => '28',
'approver_id' => '467',
'KmpContent' =>
array (
'id' => '28',
'name' => 'sct_domain.html',
'description' => NULL,
'page_title' => 'IVV Knowledge Portal :: mckesson :: domain',
'display_logo_path' => '../images/domains.png',
'display_text' => 'Domains',
'top_logo_path' => '/mckesson_ban.jpg',
'top_logo_url' => 'http://www.mckesson.com/en_us/McKesson.com/',
'header_text' => 'Domains',
'header_desc' => NULL,
'header_logo_path' => '',
'is_section' => '1',
'url_path' => '/university/mckesson/domains.html',
'can_upload_files' => '1',
'is_file' => '0',
'file_path' => NULL,
'display_order' => '1',
'aco_path' => '',
'menu_name' => 'mckesson',
'view_path' => 'download_files',
'is_approved' => '1',
'parent_id' => '11',
'is_active' => '1',
'created' => '2013-06-22 21:18:27',
'created_by' => NULL,
'modified' => '2013-06-22 21:21:01',
'approved_by' => NULL,
),
'Approver' =>
array (
'id' => '467',
'first_name' => 'Nageswara Rao',
'last_name' => 'Menda',
),
),
),
),
'SectionApprover' =>
array (
),
),
I dont want the kmpContent in with in the sectionapprovers for all the records

What if you formatted the statement like
$fileData = $this->KmpContent->find('all', array(
'conditions' => array(
'KmpContent.created_by' => $curUser,
'KmpContent.is_approved' => false,
'KmpContent.is_file' => true,
),
'contain' => array(
'Parent' => array(
'SectionApprover' => array(
'Approver'
)
)
),
'recursive' => 3, // though you don't need to set recursive with contain
'fields' => array('id', 'file_path', 'url_path', 'is_approved', 'name', 'created', 'approved_by','parent_id','display_text','is_active')
));

Related

How to show dynamic output in the wp_bakery vc_map dropdown?

I am using the wp_bakery page builder plugin. What I am doing is, I have to display the custom output in the dropdown
Expected output
Current Output
if I use the below code then I am getting the correct output.
$params=array(
'name' => __('Speaker'),
'base' => 'speaker',
'category' => __(My shortcodes'),
'class' => 'js-example-basic-single',
"icon" => get_template_directory_uri() . "/images/shortcode_slider.png",
'params' => array(
array(
'type' => 'dropdown',
'heading' => __('Speakers'),
'param_name' => 'speakersname',
'class' => 'js-example-basic-single',
'admin_label' => true,
//'value' => array($selectSpeaker),
'value' => array(__( 'Item One', 'textdomain' ) => '1'), //static added
'description' => __('Select speakers')
))
);
echo"<pre />";
print_r($params);
vc_map( $params );
array output
Array
(
[name] => Speaker
[base] => speaker
[category] => My shortcodes
[class] => js-example-basic-single
[icon] => https://example.com/wordpress/wp-content/themes/testing/images/shortcode_slider.png
[params] => Array
(
[0] => Array
(
[type] => dropdown
[heading] => Speakers
[param_name] => speakersname
[class] => js-example-basic-single
[admin_label] => 1
[value] => Array
(
[Item One] => 1
)
[description] => Select speakers
)
)
)
But If I use the below code then I am getting the current output(Screenshot shared above)
$speakerpost_args = array( 'post_type'=>'speaker','post_status' => 'publish', 'posts_per_page' => 80);
$speaker_posts = wp_get_recent_posts($speakerpost_args);
$selectSpeaker="";
foreach ($speaker_posts as $key => $value) {
$selectSpeaker.="__( 'Item One', 'textdomain' ) => '1',";
}
$params=array(
'name' => __('Webinar Speaker'),
'base' => 'speaker',
'category' => __('Hybreed shortcodes'),
'class' => 'js-example-basic-single',
"icon" => get_template_directory_uri() . "/images/shortcode_slider.png",
'params' => array(
array(
'type' => 'dropdown',
'heading' => __('Speakers'),
'param_name' => 'speakersname',
'class' => 'js-example-basic-single',
'admin_label' => true,
'value' => array($selectSpeaker), //dynamic value
//'value' => array(__( 'Item One', 'textdomain' ) => '1'),
'description' => __('Select speakers')
))
);
echo"<pre />";
print_r($params);
vc_map( $params );
Array output
Array
(
[name] => Speaker
[base] => speaker
[category] =>My shortcodes
[class] => js-example-basic-single
[icon] => https://exmple.com/wordpress/wp-content/themes/testing/images/shortcode_slider.png
[params] => Array
(
[0] => Array
(
[type] => dropdown
[heading] => Speakers
[param_name] => speakersname
[class] => js-example-basic-single
[admin_label] => 1
[value] => Array
(
[0] => __( 'Item One', 'textdomain' ) => '1' // here is the issue
)
[description] => Select speakers
)
)
)
Need to append values as array items not as string concatenation.
Change
$selectSpeaker="";
foreach ($speaker_posts as $key => $value) {
$selectSpeaker.="__( 'Item One', 'textdomain' ) => '1',";
}
To:
$selectSpeaker=[];
foreach ($speaker_posts as $key => $value) {
$selectSpeaker[__( 'Item One', 'textdomain' )] = '1';
}

CakePHP 2.x retriving data with i18n in a loop

I'm looping trough Features (belongTo FeatureType) and find() FeatureType.name for each Feature.feature_type_id.
I'm getting first record correct (with i18n translation) but in all the rest the i18n translation is not in place, but given as separate record.
What I am doing wrong?
this is the debug of my result table:
array(
'FeatureType' => array(
'id' => '28',
'name' => 'kolor suwaka',
'comment' => 'kolor suwaka etui notebook',
'locale' => 'pol'
)
)
array(
'FeatureType' => array(
'id' => '7',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor materiału',
'FeatureType__i18n_comment' => 'gra w klasy'
)
)
array(
'FeatureType' => array(
'id' => '11',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'kolor',
'FeatureType__i18n_comment' => 'kółko i krzyżyk (jasnoszare i ciemnoszare)'
)
)
array(
'FeatureType' => array(
'id' => '27',
'name' => '',
'comment' => '',
'locale' => 'pol'
),
(int) 0 => array(
'FeatureType__i18n_name' => 'obwód głowy',
'FeatureType__i18n_comment' => 'rozmiar kapelusza czarownicy'
)
)
This is the code:
$features_str = "";
if (!empty($product['Features'])) {
foreach ($product['Features'] as $featureIndex => $feature) {
$featureTypeModel = new FeatureType();
$feature_type = $featureTypeModel->find("first", array("conditions" => array("FeatureType.id" => $feature['feature_type_id'])));
if (strlen($features_str) > 0) $features_str .= ", ";
$features_str .= $feature_type['FeatureType']['name'] . ': ' . $feature['name'];
unset($featureTypeModel);
}
}

Cakephp find not returning the complete string

I have a model BudgetsProduct that is associated with the model Product, but it's returning the Product data cutting the first character on fields id and title, how you can see above.
Notice that BudgetsProduct.product_id is 7724 and its returning the correct associated product row, but trimming the first character like Product.id is returning just 724.
'BudgetsProduct' => array(
(int) 0 => array(
'id' => '3',
'budget_id' => '2',
'product_id' => '7724',
'quant' => '3.00',
'price' => '3175.00',
'discount' => '0.00',
'discount_type' => '%',
'product_options' => 'null',
'active' => '1',
'creator' => null,
'created' => '2014-08-27 15:52:35',
'modifier' => null,
'modified' => '2014-08-27 15:52:35',
'Product' => array(
'id' => '724',
'title' => 'onjunto Pina (03)',
'opt_title' => null,
'parent_id' => null,
'stock' => false,
'active' => '',
'creator' => null,
'created' => '013-12-11 10:53:30',
'modifier' => null,
'modified' => '014-04-09 15:44:24'
)
)

Accessing the array values in cakephp

I need to access the array elements, from the below array structure.Here i need to access the NewsFeedComment array.I am trying to do this, but it's giving me the error Undefined index: comments
How can i access the elements in the NewsFeedComment array
In Controller:
$this->set('newsfeed',$this->paginate('NewsFeed',$newsfeed));
In view:
<?php foreach ($newsfeeds $newsfeed): ?>
<div class="news_reply_display">
<?php echo $newsfeed['NewsFeedComment'][0]['comments'] ?>
</div>
<?php endforeach; ?>
array(
(int) 0 => array(
'NewsFeed' => array(
'id' => '2',
'title' => 'Best Restaurant',
'group_id' => '1',
'posted_message' => 'message.',
'user_id' => '11',
'created' => '2013-12-03 09:35:02',
'status' => 'A'
),
'Group' => array(
'id' => '1',
'group_name' => 'My group',
'created_user' => '1',
'created_date' => '2013-04-24 15:31:09',
'privacy' => '0',
'membership' => '0',
'description' => 'desc'
),
'NewsFeedComment' => array(
(int) 0 => array(
'id' => '1',
'news_feed_id' => '2',
'comments' => 'comment1',
'status' => 'A'
),
(int) 1 => array(
'id' => '2',
'news_feed_id' => '2',
'comments' => 'comment2',
'status' => 'A'
),
(int) 2 => array(
'id' => '3',
'news_feed_id' => '2',
'comments' => 'comment3',
'status' => 'A'
)
)
),
(int) 1 => array(
'NewsFeed' => array(
'id' => '1',
'title' => 'Test Subject',
'group_id' => '1',
'posted_message' => 'description.',
'user_id' => '1',
'created' => '2013-12-03 08:25:12',
'status' => 'A'
),
'Group' => array(
'id' => '1',
'group_name' => 'secondgroup',
'title' => 'Second Grouups',
'group_slug' => 'secgroup',
'created_user' => '1',
'created_date' => '2013-04-24 15:31:09',
'privacy' => '0',
'membership' => '0',
'description' => 'desc'
),
'NewsFeedComment' => array()
)
)
That index doesn't exist. In that particular case you must use $newsfeed[0]['NewsFeedComment']['comments'].
Now, if you would like to extract all "NewsFeedComent" arrays from $newsfeed then you can use Hash::extract
$newsFeedComments = Hash::extract($users, '{n}.NewsFeedComment');
debug($newsFeedComments);
foreach($newsfeeds as $newsfeed){
foreach($newsfeed['NewsFeedComment'] as $NewsFeedCommentData){
echo $NewsFeedCommentData['comments'];
}
}
Using the above code you will get comments corresponding to each index of array $newsfeeds

How does Set::nest in Cakephp 2 works?

Due to the lack of proper documentation I have difficulties
to understand "Set::nest" CakePhp function.
I would appreciate if somebody can explain generally
and with my example how to use it properly.
Let's say I have this tree:
array(
(0) => array(
'Category' => array('name' => 'Cat 1'),
'Course' => array(
'name' => 'Course 1'
),
'Day' => array('date' => '2012-01-01')
),
(1) => array(
'Category' => array('name' => 'Cat 1'),
'Course' => array(
'name' => 'Course 1'
),
'Day' => array('date' => '2012-01-02')
),
(2) => array(
'Category' => array('name' => 'Cat 1'),
'Course' => array(
'name' => 'Course 3'
),
'Day' => array('date' => '2012-01-06')
),
(3) => array(
'Category' => array('name' => 'Cat 2'),
'Course' => array(
'name' => 'Course 2'
),
'Day' => array('date' => '2012-01-02')
),
)
to be transformed to:
array(
'Cat 1' => array(
'Course' => array(
'name' => 'Course 1'
),
'Day' => array(
array('date' => '2012-01-01'),
array('date' => '2012-01-02')
),
'Course' => array(
'name' => 'Course 3'
),
'Day' => array(
array('date' => '2012-01-06')
),
),
'Cat 2' => array(
'Courses' => array(
'name' => 'Course 2'
),
'Day' => array(
array('date' => '2012-01-03')
)
),
)
As you can see it's kind of a double nesting because "Category" can have multiple "Course" rows and "Course" can have multiple "Day".
I could just loop through it and do it manually.
So the fist question is: can I transform this with Set::nest at all
and if yes how?
What you are asking for is impossible. Your array is not valid. You need to change your format to have each category as a nested array 'cat' => array(array('course 1' => ...), array('course 2' => ...))
print_r(array(
'Cat 1' => array(
'Course' => array(
'name' => 'Course 1'
),
'Day' => array(
array('date' => '2012-01-01'),
array('date' => '2012-01-02')
),
'Course' => array(
'name' => 'Course 3'
),
'Day' => array(
array('date' => '2012-01-06')
),
),
'Cat 2' => array(
'Courses' => array(
'name' => 'Course 2'
),
'Day' => array(
array('date' => '2012-01-03')
)
),
));
Output:
Array
(
[Cat 1] => Array
(
[Course] => Array
(
[name] => Course 3
)
[Day] => Array
(
[0] => Array
(
[date] => 2012-01-06
)
)
)
[Cat 2] => Array
(
[Courses] => Array
(
[name] => Course 2
)
[Day] => Array
(
[0] => Array
(
[date] => 2012-01-03
)
)
)
)

Resources