CakePHP: Retrieving data - php

I'm stuck in retrieving some sort of data in my view. I have to list information of a certain "Job", which has different attributes already stored in the database.
For example, I'd like to get the field of 'weighting' listed.
The debug($this->viewVars) prints me this (which represents what's stored in the DB):
'JobValues' => array(
(int) 0 => array(
'id' => '1',
'yearly_job_id' => '1',
'institution_type_id' => '1',
'norm_rate' => '5',
'weighting' => '5.00',
'standard_value' => '5.00',
'education_weeks' => '5.0',
'group_distribution' => '0.50',
'YearlyJob' => array(
'id' => '1',
'year_id' => '1',
'job_id' => '1',
'full_time_jobs_needed' => true,
'education_count_needed' => true,
'is_care' => true
),
Normally I would just print this: echo([JobValue][weighting]);, but there is a number in between, so echo([JobValue][1][weighting]); won't lead to success. What could I implement in my view (or model)? Any advice is highly appreciated.

For printing out the data, if there's only one top level of array, just remove it. print_r(array_shift($foo['JobValues']['weighting']));
If there are multiple arrays on the same level, you need to do a foreach to iterate into each one of the record.

Related

TYPO3 (7.6.14) Extbase TCA: Field missing in mask after setting foreign_sortby

After I set the 'foreign_sortby' => 'datum'; this field is missing now at my edit mask. Does anybody know why?
TCA of tx_veranstaltungen_domain_model_terminblock (1 terminblock can have multiple termine)
'termine' => array(
'exclude' => 1,
'label' => 'LLL:EXT:veranstaltungen/Resources/Private/Language/locallang_db.xlf:tx_veranstaltungen_domain_model_terminblock.termine',
'config' => array(
'type' => 'inline',
'foreign_table' => 'tx_veranstaltungen_domain_model_termin',
'foreign_field' => 'terminblock',
'foreign_sortby' => 'datum',
'maxitems' => 9999,
'appearance' => array(
'collapseAll' => 0,
'levelLinksPosition' => 'top',
'showSynchronizationLink' => 1,
'showPossibleLocalizationRecords' => 1,
'useSortable' => 1,
'showAllLocalizationLink' => 1
),
),
),
Okay I found it out whats the problem, after reading Bug report
You should use foreign_default_sortby instead of foreign_sortby, because IRRE hides the field you use for sorting. The dangerous thing is, that the field could be overwritten by "sorting values". So I wondered why all my fields (of type date (timestamp)) has the value 01.01.1970... because the field values changed to 1,2,....
I hope this can help others, before they destroy their data. :)
(I'm using Typo3 7.6.14)

CakePHP getting data form an ID which is inside another table?

Right, so my data returns in the following way,
(int) 0 => array(
'MODEL-XX' => array(
//DATA HERE
'xxs_id' => '11',
'aas_id' => '44',
'vvs_id' => '2'
),
'xxs' => array(
'id' => '11',
'customername' => 'XX name here'
),
'aas' => array(
'id' => '44',
'clientname' => 'aa name here',
'xxs_id' => '11'
),
'vvs' => array(
'id' => '2',
'start' => '1405296000',
'end' => '1405814400',
'users_id' => '1'
)
This works fine, but I want to know how to link my users table to this model. So the details of each user for my VV model would become apart of the data. My MODEL-XX does not have any links with my users table so the place I need to call in the users details are held with my VV model.
I have been looking into this but have not been able to find a simple easy method for doing this?
I was thinking that this would be doable with my model, so I opened my my XX model and added the following within my '$belongsTo' section,
'Users' => array(
'className' => 'Users',
'foreignKey' => 'vvs.users_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
So is there a easy method for linking data like this?
Please give me time, if I have not explained myself right or not enough data, please tell me and let me fix or explain better.
Thanks,
Either set your recusive higher:
$this->MODEL-XX->recursive = 1; //or 2
Or and this should be your prefered way to go, start using the containable behaviour:
http://book.cakephp.org/2.0/en/core-libraries/behaviors/containable.html
In your appModel:
public $actsAs = array('Containable');
Then try this find:
$this->MODEL-XX->recursive = -1;
$data = $this-MODEL-XX>find(
'all', array(
'conditions' => $conditions,
'contain' => array('xxs', 'aas', 'vvs', 'user')
)
);
I might be 'vvs.user' but I forgot what is used for deeper models

Taking a sub panel in sugarcrm and making it into two, one that displays entries depending if a checkbox is checked, the other non checked entries

Taking a sub panel in sugarcrm and making it into two, one that displays entries depending if a checkbox is checked, the other non checked entries.
I have a module called mod_loaninvestor, and its sub panel is whole_subpanel_mod_loaninvestor_contacts
Now I know vardefs is where you create the duplicate and rename it like this
<?php
//WARNING: The contents of this file are auto-generated
// created: 2014-01-24 13:12:28
$layout_defs["Contacts"]["subpanel_setup"]['mod_loaninvestor_contacts'] = array (
'order' => 100,
'module' => 'mod_LoanInvestor',
'subpanel_name' => 'default',
'sort_order' => 'asc',
'sort_by' => 'id',
// 'where' => "(mod_LoanInvestor.active_investment == "1")",
'title_key' => 'LBL_MOD_LOANINVESTOR_CONTACTS_FROM_MOD_LOANINVESTOR_TITLE',
'get_subpanel_data' => 'mod_loaninvestor_contacts',
'top_buttons' =>
array (
/*
0 =>
array (
'widget_class' => 'SubPanelTopButtonQuickCreate',
),
1 =>
array (
'widget_class' => 'SubPanelTopSelectButton',
'mode' => 'MultiSelect',
),
*/
),
);
// created: 2014-01-24 13:12:28
$layout_defs["Contacts"]["subpanel_setup"]['mod_loaninvestor_contacts1'] = array (
'order' => 100,
'module' => 'mod_LoanInvestor',
'subpanel_name' => 'default',
'sort_order' => 'asc',
'sort_by' => 'id',
'title_key' => 'LBL_MOD_LOANINVESTOR_CONTACTS_FROM_MOD_LOANINVESTOR_TITLE',
'get_subpanel_data' => 'mod_loaninvestor_contacts',
'top_buttons' =>
array (
/*
0 =>
array (
'widget_class' => 'SubPanelTopButtonQuickCreate',
),
1 =>
array (
'widget_class' => 'SubPanelTopSelectButton',
'mode' => 'MultiSelect',
),
*/
),
);
?>
How would I make it so the first one would only show entries with the following checked
and the other display only entries with that field unchecked?
Thanks for your help!
You're halfway there. On top of defining new layoutdefs, you need to create a new subpanel definition.
Dig into mod_LoanInvestor and the metadata/subpanels/default.php and copy it to default2.php or whatever you'd like to call it. You'll want to use a WHERE clause to separate the two.
https://gist.github.com/matthewpoer/8871568
Here's a gist where I'm doing this with transactions records to seperate scheduled (future) transactions from past (posted payments).

How to count the result using find() in CakePHP 2.2.4?

I am using Cakephp 2.2.4 and I need to retrive a list of Lead that belongs to the user (id = 106).
The result of the query is:
array(
(int) 0 => array(
'Lead' => array(
'id' => '6',
'user_id' => '106',
'date' => '2012-12-31 22:15:23',
'ip' => '127.0.0.1',
'service_id' => '1',
'location' => 'Rome',
'message' => 'Message Message',
'telephone' => null,
'active' => null
),
'User' => array(
'id' => '106',
'email' => 'daje#daje.it',
'pwd' => '0433c024cb08be13000d59a347e640482843f46f177e95749dc6599c259617fd3491dcb940b47693cbbc7f65a2cc5ef62deca2e600c1be133ad54170f7d1fbd1',
'role_id' => '3',
'active' => '1'
),
'Service' => array(
'id' => '1',
'name' => 'Primo servizio'
),
'Estimate' => array(
(int) 0 => array(
'id' => '1',
'lead_id' => '6',
'user_id' => '106'
)
)
)
)
It looks good but I need to count the Estimates (Estimate array), I would like to retrive the number of the estimates, and not the array with all the fields (of estimates table).
How can i do it?
I need :
Lead array as it shown
User array as it shown
Service array as it shown
Estimate (only the total number of the estimates... in this case 1)
The find is very simple:
$options = array('conditions' => array('User.id' => 106));
debug($this->Lead->find('all', $options));
Try something like this, not 100% sure it'll work but worth a go if not I'd advise trawling the cakephp docs for retrieving your data:
$options = array(
'fields' => array('Lead.*', 'User.*', 'Service.*', 'count(Estimate.id)'),
'conditions' => array('User.id' => 106)
);
Without diving too far into the internals of the Cake ORM, assuming you don't need to do this immediately at query time, couldn't you just get the count of the estimate array programmatically after the fetch?
http://php.net/manual/en/function.count.php
$leads = $this->Lead->find('all',$options);
foreach($leads as $lead){
//get the number of estimates for every lead result
$lead_num = count($lead['Estimate']);
}
Alternatively, you could manually write a join query for this one fetch and execute it using the Cake Model class's query method. Without knowing the specifics of your table schema and model relations its hard to give specifics about how to structure the query, but this shouldn't be too difficult by just look at your table spec and extracting a sql COUNT statement for every Estimate with given id.

Including '0' in numberArray dropdown

Is it possible to include the option '0' in a dropdown generated by:-
'options' => Mage::helper('menu')->numberArray(5,Mage::helper('menu')->__('')),
This is part of...
$fieldset->addField('column', 'select', array(
'name' => 'column',
'label' => Mage::helper('menu')->__('Column'),
'title' => Mage::helper('menu')->__('Column'),
'required' => true,
'options' => Mage::helper('menu')->numberArray(5,Mage::helper('menu')->__('')),
));
Which generates a dropdown 1-5 but I'm wanting to include 0 at the beginning of this dropdown (or 'none' or anything other value that isn't an integer of 1+) but I'm unsure how to edit the above to allow this.
If anyone could advise what is needed please, that would be great.
Thanks in advance.
Since numberArray() is not a native Magento method, we can't know what this custom method really does in the end.
But, Varien_Data_Form_Element_Fieldset::addField() usually expects just a simple assoc array as value for the options key, so most probably something like this will work:
'options' => array(
0 => 'none',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five'
),

Categories