Populate date in MultiOptions element of Zend Form - zend-framework

Hello I have an array like this :
Array (
[id] => 1
[code] => Dep98
[description] => Hello World
[facility] => Array (
[0] => FacName1
[1] => FacName2
)
)
But when I populate this array to Zend_Form it only show data in textboxes elements having same id as defined in array index not in multiselect dropdown element. for example:
'code' id is also define in form's first textbox element,
'description' id is also define in form's second textbox element,
'facility' id is also define in form's third MultiOptions element
But in MultiOptions it does not show any record.

I agree with Travis, you should pass an array with following values to populate:
$vals = array('code'=>5,
'description' => 'testing',
'facility' => array(1=>'FacName2'));
$form->populate($vals);
But note this - options must be filled in the facility form element before attempting to populate or validate, dont expect facility value to be set if there is an empty list of options in the facility element.

What exactly do you want in the drop down box?
The array you pass to multiOptions must be in the form of value => title.
You may want to loop through your results and generate an options array.
For example
$options = array();
foreach ( $data as $value ) {
$options[$value['id']] = $value['description'];
}
$select = Zend_From_Element_Select("select_field");
$select->multiOptions($options);

Try this:
Array (
[id] => 1
[code] => Dep98
[description] => Hello World
[facility] => Array (
FacName1 => [0]
FacName2 => [1]
)
)

Related

Cakephp table query

I have a members table. It has these columns:
ID
fname
lname
parent
spouse
I want 'parent' field to have select option in a form which uses 'fname' & 'lname' and store it in 'parent' field. I have tried to do this Members controller:
$results = $this->Members->find('list',
array('order'=>array('fname DESC')));
$parent = $results->toArray();
I am calling $parent variable as an input. However that method doesn't work. I don't know how to figure it out.
When calling list you can configure the fields used for the key and value with the keyField and valueField options respectively.
Doc: https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs
Like this
$query = $articles->find('list', [
'keyField' => 'slug',
'valueField' => 'title'
]);
In your case it will be like this
$results = $this->Members->find('list', array(
'keyField' => 'ID',
'valueField' => 'parent'
'order'=>array('fname DESC'),
));
$parent = $results->toArray();

PHP MongoDB Search in Sub-array

Data stored in collection as below
[field_1] => Array
(
[fields] => Array
(
[0] => MongoInt64 Object
(
[value] => 1233
)
[1] => MongoInt64 Object
(
[value] => 1234
)
)
)
I need to search 1234 in field.
I used below code in php to search
$param = array('field_1.fields.$' => 1234);
But this is not working
You need to use the $in query criteria, to find elements within an array
$cursor = $collection->find(array("field_1.fields" => array('$in' => array("1234"))));
This will find all items that have 1234 within "fields"
$in doc: https://docs.mongodb.org/v3.0/reference/operator/query/in/

Retrieving values of Form::select

I have a select box with an array of data to populate it like this:
{!! Form::select('language',$languageArray,'null', ['id'=>'language','multiple',]) !!}
I am passing $languageArray with view , and is simply an array of values like ['A','B','C']...
Now while fetching the selected values i am getting numeric value of selected options. Is there a way to change the values from numeric indexes to Text written in option. i did it using an associative array as second argument like this:
['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..']
But it creates a long list and view seems to be overloaded with data is there a better way for achieving below output ???
<select name="language">
<option value="english" ">English</option>
<option value="french">French</option>
....
I suggest you to use Config values ,
Create a file like languages.php in config folder.
<?php
return ['english' => 'English',
'french' => 'French',
'so on ...' => 'So On ..'
'german' => Lang::get('languages.german')
];
View :
{!! Form::select('language',Config::get('languages'),'null', ['id'=>'language','multiple',]) !!}
As you can see, in this way you can use config values in another view too and support multi language(its important too.. look at 'german')
Last Option is creating your own Form macro for example,
Form::macro('slugiySelect', function($name , $list , $selected = null , $options = [])
{
$options = [];
foreach($list as $row)
$options[Str::slug($row)] = $row;
return Form::select($name , $options , $selected , $options);
});
In this macro your options array is slugify with Laravel string function and uses as a key , I kept value as yours. You can do it with your way.

HTML::FormHandler access to form field

Is there any way to access the value of form field 'wklloc_id' in the form field options method of field 'prg_id'?
My Form contains (amongst others) these fields:
has_field 'wklloc_id' => ( type => 'Select', label => 'Winkel(locatie)' );
has_field 'prg_id' => ( type => 'Select', empty_select => 'Kies eerst een Winkel(locatie)', label => 'Productgroep' );
At this point my options method for field 'prg_id' contains:
sub options_prg_id
{
my ($self) = shift;
my (#prg_select_list,$productgroep,$productgroepen);
return unless ($self->schema);
$productgroepen = $self->schema->resultset( 'WinkelLocatieProductgroepen' )->search( {}, { bind => [ 2 ] } );
Is is possible to set the value of the bind variable (i.e. 2) to the value of field 'wklloc_id' and how does one do that? Mind you this is needed before any submit.
The value of a select field is set the same way as other fields, i.e. it comes from an init_object, from parameter values, or from a default. For your case, if you want this field to start with the 'value' 2, then just put: default => 2 in your field definition.

SugarCRM: how to get all contacts for an account via REST API

I am trying to get all contacts for a particular account (i know the account id) from SugarCRM using the v2 REST API.
I am sending a GET request with the following parameters:
input_type => 'JSON'
response_type => 'JSON'
method => 'get_entry_list'
rest_data => '{session:"some-valid-session-id", module_name:"Contacts", query:"contacts.account_id=some-valid-id"}'
I expect to get all contacts that are related to this accoutn, but instead I get an error "... MySQL error 1054: Unknown column 'contacts.account_id' in 'where clause'"
However, when I try to get all contacts without providing any query (query='') I get all the contacts with all their properties and I can see that there is an account_id property.
Can anyone help?
Try query:"accounts.id=some-valid-id".
It has worked for me in the past with the SOAP API.
Here's my method. It uses this wrapper class: http://github.com/asakusuma/SugarCRM-REST-API-Wrapper-Class/
/**
* returns an array of contacts that are related to the accountId passed as a param.
* The array returned will be an array of associative arrays.
* #param $accountId
* #param array $contactSelectFields optional sets the different items to return, default includes id, email1, name, title, phone_work, and description
* #return array
*
*/
public function getAllContactsAtOrganization( $accountId, $contactSelectFields=array("id", "email1", "name", "title", "phone_work", "description")) {
$sugar = new Sugar_REST( SUGAR_REST_URL, SUGAR_USER_NAME, SUGAR_PASSWORD);
$fields = array( "Accounts" => array("id", "name"),
"Contacts" => $contactSelectFields);
$options = array(
'where' => "accounts.id='$accountId'"
);
$apiResult = $sugar->get_with_related("Accounts", $fields, $options );
$contacts = array();
foreach( $apiResult['relationship_list'][0]['link_list'][0]['records'] as $almostContact) {
$curr = array();
foreach($contactSelectFields as $key) {
$curr[$key] = $almostContact['link_value'][$key]['value'];
}
$contacts[] = $curr;
}
//print_r($contacts);
return $contacts;
}
Sample Return
Array
(
[0] => Array
(
[id] => 47e1376c-3029-fc42-5ae2-51aeead1041b
[email1] => johndoe#gmail.com
[name] => Blake Robertson
[title] => CTO
[phone_work] => 8881112222
[description] => Opinionated developer that hates SugarCRM's REST API with a passion!
)
[1] => Array
(
[id] => 4c8e3fcf-8e69-ed7d-e239-51a8efa4f530
[email1] => csmith#mailinator.com
[name] => Carolyn Smith
[title] => Director of Something
[phone_work] => 832-211-2222
[description] => She's a smooth operator...
)
)
For Reference Purposes
Here's the "rest-data" (nicely formatted)
Used print_r of the php array
Array
(
[session] => 9j7fm4268l0aqm25kvf9v567t3
[module_name] => Accounts
[query] => accounts.id='e583715b-7168-5d61-5fb1-513510b39705'
[order_by] =>
[offset] => 0
[select_fields] => Array
(
[0] => id
[1] => name
)
[link_name_to_fields_array] => Array
(
[0] => Array
(
[name] => contacts
[value] => Array
(
[0] => id
[1] => email1
[2] => name
[3] => title
[4] => phone_work
[5] => description
)
)
)
[max_results] => 20
[deleted] => FALSE
)
Post Body
method=get_entry_list&input_type=JSON&response_type=JSON&rest_data={"session":"iov5a257lk5acsg9l3ll6kuej3","module_name":"Accounts","query":"accounts.id='e583715b-7168-5d61-5fb1-513510b39705'","order_by":null,"offset":0,"select_fields":["id","name"],"link_name_to_fields_array":[{"name":"contacts","value":["id","email1","name","title","phone_work","description"]}],"max_results":20,"deleted":"FALSE"}method=logout&input_type=JSON&response_type=JSON&rest_data={"session":"iov5a257lk5acsg9l3ll6kuej3"}
I'm not familiar with SugarCRM yet, but did you try with just account_id=some-valid-id ? because I also did a REST request to add a contact to sugarcrm and I didn't mention the table's name, just the fields. I didn't try this but it seems logical to me since you already mentionned the module's name, so I guess sugar kind of knows what table(s?) to look for when processing your query.