Perl: Accessing and handling objects in an array - arrays

I try to handle the following Array which was generated by get_pool_member_object_status_($pool)
from http://search.cpan.org/~ltp/BigIP-iControl/lib/BigIP/iControl.pm#get_pool_member_object_status_($pool)
$VAR1 = [
[
bless( [
bless( {
'member' => bless( {
'address' => '192.168.100.141',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.142',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.143',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.144',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' )
], 'LocalLB::PoolMember::MemberObjectStatus[]' )
]
];
I couldn't figure out how this should work. I tried several examples from perldsc. The examples there mostly concern ARRAYS OF HASHES. But the above array is...? I don't even know what kind of array this is. Can someone point me in the right direction?
So for example how do I access the "object_status"->"availability_status" of a "member"->"address"
Thx simbabque. This is the output I get from Data::Printer:
[
[0] [
[0] LocalLB::PoolMember::MemberObjectStatus[] {
public methods (0)
private methods (0)
internals: [
[0] LocalLB::PoolMember::MemberObjectStatus,
[1] LocalLB::PoolMember::MemberObjectStatus,
[2] LocalLB::PoolMember::MemberObjectStatus,
[3] LocalLB::PoolMember::MemberObjectStatus
]
}
]
]

So you are trying to access the 'availability_status' and 'address'
I didn't traverse this whole object but with this code I am able to print the required fields.
#!/usr/bin/perl -w
use strict;
use warnings;
use Data::Dumper;
my $obj = [
[
bless( [
bless( {
'member' => bless( {
'address' => '192.168.100.141',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.142',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.143',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' ),
bless( {
'member' => bless( {
'address' => '192.168.100.144',
'port' => '80'
}, 'Common::IPPortDefinition' ),
'object_status' => bless( {
'availability_status' => 'AVAILABILITY_STATUS_GREEN',
'status_description' => 'Pool member is available',
'enabled_status' => 'ENABLED_STATUS_ENABLED'
}, 'LocalLB::ObjectStatus' )
}, 'LocalLB::PoolMember::MemberObjectStatus' )
], 'LocalLB::PoolMember::MemberObjectStatus[]' )
]
];
print Dumper($obj->[0]->[0]->[0]->{member}->{address});
print Dumper($obj->[0]->[0]->[0]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[1]->{member}->{address});
print Dumper($obj->[0]->[0]->[1]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[2]->{member}->{address});
print Dumper($obj->[0]->[0]->[2]->{object_status}->{availability_status});
print Dumper($obj->[0]->[0]->[3]->{member}->{address});
print Dumper($obj->[0]->[0]->[3]->{object_status}->{availability_status});
Output:
$VAR1 = '192.168.100.141';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.142';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.143';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
$VAR1 = '192.168.100.144';
$VAR1 = 'AVAILABILITY_STATUS_GREEN';
Hope this helps and now you should be able to traverse this data structure with loops.

Related

Belongs to Many: save custom field

I need help with saving/updating belongsToMany data with custom field.
I have a tables acl, managers, groups, sections.
I need to save/update data in acl table from sections/edit or sections/add
acl table
id|target_id|target_type|section_id
I need set target_id data 'user' or 'group' depending entities.
edit.ctp
echo $this->Form->control('managers._ids', ['class' => 'multiple', 'multiple' => true]);
echo $this->Form->control('groups._ids', ['class' => 'multiple', 'multiple' => true]);
SectionsTable.php
$this->belongsToMany('Managers', [
'joinTable' => 'acl',
'through' => 'Acl',
'foreignKey' => 'section_id',
'targetForeignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsToMany('Groups', [
'joinTable' => 'acl',
'through' => 'Acl',
'foreignKey' => 'section_id',
'targetForeignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
AclTable.php
$this->belongsTo('Managers', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsTo('Groups', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
I tried to use _joinData, but nothing work
before and after patchEntity()
foreach ($section->groups as &$group) {
$group->_joinData = ['target_type' => 'group'];
}
whithout save()
foreach ($this->request->data['managers']['_ids'] as $id) {
$manager = $this->Sections->Managers->get($id);
$manager->_joinData = new Entity(['target_type' => 'user'], ['markNew' => true]);
$this->Sections->Managers->link($section, [$manager]);
}
$this->request->data has a follow structure:
[
'controller' => '*',
'action' => '*',
'title' => '*',
'managers' => [
'_ids' => [
'0' => '*',
'1' => '*',
'2' => '*',
]
],
'groups' => [
'_ids' => [
'0' => '*',
'1' => '*',
'2' => '*',
]
]
]
Alltimes error: Cannot insert row, some of the primary key values are missing. Got (3, 114, ), expecting (target_id, section_id, target_type)
Now working with following code
$section = $this->Sections->patchEntity($section, $this->request->data, ['associated' => ['Managers', 'Groups']]);
foreach ($section->managers as &$manager) {
$manager->_joinData = new Entity([
'target_id' => $manager->id,
'target_type' => 'user',
'section_id' => $section->id
], ['markNew' => true]);
}
foreach ($section->groups as &$group) {
$group->_joinData = new Entity([
'target_id' => $group->id,
'target_type' => 'group',
'section_id' => $section->id
], ['markNew' => true]);
}
if ($this->Sections->save($section, ['associated' => ['Managers', 'Groups']])) {
$this->Flash->success(__('The section has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The section could not be saved. Please, try again.'));
But, maybe can do it don't listing all fields?
Also I need to create/add new records to acl table when adding new section
But section_id unavailable until save(). Can I save/add acl records by associations
Updated
AclTAble.php
$this->setPrimaryKey(['target_type', 'target_id', 'section_id']);
.
.
.
$this->belongsTo('Managers', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'user'
]
]);
$this->belongsTo('Groups', [
'foreignKey' => 'target_id',
'conditions' => [
'target_type' => 'group'
]
]);
With primaryKey id, adding and editing entities working, but editing makes dublicates, it's are not perfect for me. And in documentations use composite primaryKey, so i use ['target_id', 'target_type', 'section_id'], with it cake don't make dublicated, but aggain show me error: Cannot insert row, some of the primary key values are missing. Got (8, 197, ), expecting (target_id, section_id, target_type)

cakephp: plumSearch - Changing filter's label in parameters

Using PlumSearch plugin, it is mentioned in its documentation that we can modify 'label':
formConfig: Contains Form::input $options settings like class, input type, label name...
In my controller/initialize(), I need to change the label of some fields ("IP Address" instead of "Ip Adress", "Status" instead of "Asset Status"):
public function initialize()
{
parent::initialize();
$parameters = [
['name' => 'serial_number', 'className' => 'Input'],
['name' => 'model_number', 'className' => 'Input'],
['name' => 'ip_address', 'label' => 'IP Address', 'className' => 'Input'],
];
if ($this->request->param('action') == 'reportFacility') {
$statuses = $this->AssetsAssignations->AssetStatuses->find('list')->order(['name' => 'asc']);
$this->set(compact('asset_statuses'));
$parameters = [
['name' => 'asset_status_id', 'className' => 'Select', 'label' => 'Status',
'finder' => $statuses
],
];
} elseif ($this->request->param('action') == 'reportClient') {
$clients = $this->AssetsAssignations->Clients->find('list')->order(['last_name' => 'asc', 'first_name' => 'asc']);
$this->set(compact('clients'));
$parameters = [
['name' => 'client_id', 'className' => 'Select', 'label' => 'Client',
'finder' => $clients
],
];
} elseif ($this->request->param('action') == 'reportRoom') {
$rooms = $this->AssetsAssignations->Rooms->find('list')->order(['name' => 'asc']);
$this->set(compact('rooms'));
$parameters = [
['name' => 'room_id', 'className' => 'Select', 'label' => 'Room',
'finder' => $rooms
],
];
}
$this->loadComponent('PlumSearch.Filter', ['parameters' => $parameters]);
}`
The code above did not work for labels.
I was told to use the following code:
$inputOptions = [
'search' => [
'placeholder' => __('Type to search...'),
'class' => 'form-control',
'label' => 'Search'
]
];
$this->set(compact('inputOptions'));
but I failed to determine where and how in my code.
Any help please ?
simply you have to add it to the $parameters array this way
$parameters = [
/* other fields here */
[
'name' => 'ip_address',
'className' => 'Input',
'formConfig' => ['label' => 'IP Address']
],
];

doctrine2 multiple db connect in zf3?

I used doctrine2 in zf3, while connect multiple db caused error.
Then following is my config in global.php
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => PDOMySqlDriver::class,
'params' => [
'host' => '127.0.0.1',
'user' => 'root',
'password' => '123456',
'dbname' => 'zf3.com',
'charset' => 'utf8',
]
],
'orm_passport' => [
'driverClass' => PDOMySqlDriver::class,
'params' => [
'host' => '127.0.0.1',
'user' => 'root',
'password' => '123456',
'dbname' => 'zf3.com.passport',
'charset' => 'utf8',
]
],
],
'entitymanager' => [
'orm_passport' => [
'connection' => 'orm_passport',
]
],
],
];
And driver config in module.config.php as following:
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => AnnotationDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/../src/Entity']
],
'orm_passport' => [
'drivers' => [
__NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver'
]
]
]
],
In my IndexController.php
public function indexAction()
{
// Get recent users
$users = $this->entityManager->getRepository(Users::class)
->findBy(['status'=>Users::ACTIVE_STATUS_NO],['timeCreated'=>'DESC']);
//\Doctrine\Common\Util\Debug::dump($users);
return new ViewModel([
'users' => $users
]);
}
The error message :
The class 'Passport\Entity\Users' was not found in the chain configured namespaces Application\Entity
the following is my global.php
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => PDOMySqlDriver::class,
'params' => [
'host' => '127.0.0.1',
'user' => 'root',
'password' => '123456',
'dbname' => 'zf3.com',
'charset' => 'utf8',
]
],
'orm_passport' => [
'driverClass' => PDOMySqlDriver::class,
'params' => [
'host' => '127.0.0.1',
'user' => 'root',
'password' => '123456',
'dbname' => 'zf3.com.passport',
'charset' => 'utf8',
]
],
],
'entitymanager' => [
'orm_passport' => [
'connection' => 'orm_passport',
'configuration' => 'orm_passport',
]
],
'migrations_configuration' => [
'orm_passport' => [
'directory' => 'data/DoctrineORMModule/Migrations',
'name' => 'Doctrine Database Migrations',
'namespace' => 'DoctrineORMModule\\Migrations',
'table' => 'migrations',
'column' => 'version',
],
],
'configuration' => [
'orm_passport' => [
'metadata_cache' => 'array',
'query_cache' => 'array',
'result_cache' => 'array',
'hydration_cache' => 'array',
'driver' => 'orm_passport',
'generate_proxies' => true,
'proxy_dir' => 'data/DoctrineORMModule/Proxy',
'proxy_namespace' => 'DoctrineORMModule\\Proxy',
]
],
'authentication' => [
'odm_passport' => [],
'orm_passport' => [
'objectManager' => 'doctrine.entitymanager.orm_passport',
],
],
'authenticationadapter' => [
'odm_passport' => true,
'orm_passport' => true,
],
'authenticationstorage' => [
'odm_passport' => true,
'orm_passport' => true,
],
'authenticationservice' => [
'odm_passport' => true,
'orm_passport' => true,
],
],
];
it works!

CakePHP 3: issue saving hasMany associations

I'm having trouble saving and updating hasMany associations. It seems that Cake can't patch the entity correctly somehow.
ProductGroups Table:
$this->hasMany('ProductIdentities', [
'foreignKey' => 'product_group_id',
'saveStrategy' => 'replace'
]);
ProductIdentities Table:
$this->belongsTo('ProductGroups', [
'foreignKey' => 'product_group_id'
]);
ProductGroupsController:
// patch entity
$productGroup = $this->ProductGroups->patchEntity($productGroup, $this->request->data, [
'associated' => [
'StaffMembers' => ['onlyIds' => true],
'ProductIdentities' => ['onlyIds' => true]
]
]);
$this->request->data:
[
'group_name' => 'Creative and Performing Arts',
'active' => '1',
'product_type_id' => '2',
'staff_members' => [
'_ids' => [
(int) 0 => '103',
(int) 1 => '11',
(int) 2 => '3'
]
],
'product_identities' => [
'_ids' => [
(int) 0 => '1760',
(int) 1 => '1762',
(int) 2 => '1763',
(int) 3 => '1764'
]
]
]
edit.ctp
<?php
echo $this->Form->input('group_name',
['class' => 'form-control']);
echo $this->Form->input('active',
['class' => 'form-control']);
echo $this->Form->input('product_type_id',
['class' => 'form-control']);
echo $this->Form->input('staff_members._ids',
['options' => $staffMembers,
'class' => 'form-control height-200']);
echo $this->Form->input('product_identities._ids',
['options' => $productIdentities,
'class' => 'form-control height-500']);
?>
If a productGroup is not associated with any productIdentities, it saves fine. However if you want to select more productIdentities or unselect some existing ones, the data won't be saved. Instead, Cake will create new records in product_identites table.
After doing a debug, I could see that Cake did not patch entity correctly. See below:
'product_identities' => [
(int) 0 => object(App\Model\Entity\ProductIdentity) {
(int) 0 => '1760',
(int) 1 => '1762',
(int) 2 => '1763',
(int) 3 => '1764',
'[new]' => true,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
(int) 0 => true,
(int) 1 => true,
(int) 2 => true,
(int) 3 => true
],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'ProductIdentities'
}
],
The onlyIds option is supposed to stop Cake from creating new records but obviously it didn't work.
And I've formatted my data into the format suggested in Cake's documentation so I'm very confused.
$data = [
'title' => 'My new article',
'body' => 'The text',
'user_id' => 1,
'comments' => [
'_ids' => [1, 2, 3, 4]
]
];
http://book.cakephp.org/3.0/en/orm/saving-data.html#converting-hasmany-data
Any comments/help is appreciated.

CakePHP filter a deep associated find

I have a deep associated find and one association is retrieving too many none related records for modules_employees.
I should see only one record for modules_employees under the course_modules but it retrieves many because they can be many with course_modules_id but only one with courses_employee_id.
The modules_employees table
'id' => (int) 18,
'courses_employee_id' => (int) 31,
'course_module_id' => (int) 7,
'completed_on' => null,
CoursesEmployee->course->course_modules->modules_employees
CoursesEmployeesController.php
public function player($id = null)
{
$coursesEmployee = $this->CoursesEmployees->get($id, [
'contain' =>
[
'Employees',
'Courses',
'CourseModules',
'Courses.CourseModules',
'Courses.CourseModules.ModulesEmployees',
'Courses.CourseFiles'
]
]);
$this->set('coursesEmployee', $coursesEmployee);
debug($coursesEmployee);
$this->set('_serialize', ['coursesEmployee']);
}
The current find object, you will see one of the course_modules has two modules_employees when I should have one.
object(App\Model\Entity\CoursesEmployee) {
'id' => (int) 31,
'employee_id' => (int) 3,
'course_id' => (int) 3,
'course_module_id' => (int) 7,
'course_module' => object(App\Model\Entity\CourseModule) {
'id' => (int) 7,
'course_id' => (int) 3,
'name' => 'Module 2',
},
'course' => object(App\Model\Entity\Course) {
'id' => (int) 3,
'name' => 'Treacys Hotel Induction Training',
'course_files' => [
(int) 0 => object(App\Model\Entity\CourseFile) {
'id' => (int) 2,
'name' => 'Manual_Handling_doc.txt',
'type' => 'doc',
}
],
'course_modules' => [
(int) 0 => object(App\Model\Entity\CourseModule) {
'id' => (int) 6,
'course_id' => (int) 3,
'name' => 'Module 1',
'module_order' => (int) 1,
'modules_employees' => [
(int) 0 => object(App\Model\Entity\ModulesEmployee) {
'id' => (int) 1,
'courses_employee_id' => (int) 0,
'course_module_id' => (int) 6,
'started_on' => object(Cake\I18n\Time) {
'time' => '2015-09-08T04:16:16+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed_on' => object(Cake\I18n\Time) {
'time' => '2015-09-09T08:22:16+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed' => true,
'deleted' => null,
'[new]' => false,
'[accessible]' => [
'employee_id' => true,
'module_id' => true,
'started_on' => true,
'completed_on' => true,
'completed' => true,
'employee' => true,
'module' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[repository]' => 'ModulesEmployees'
}
],
'[repository]' => 'CourseModules'
},
(int) 1 => object(App\Model\Entity\CourseModule) {
'id' => (int) 7,
'course_id' => (int) 3,
'name' => 'Module 2',
'module_order' => (int) 2,
'modules_employees' => [
(int) 0 => object(App\Model\Entity\ModulesEmployee) {
'id' => (int) 2,
'courses_employee_id' => (int) 31,
'course_module_id' => (int) 7,
'started_on' => object(Cake\I18n\Time) {
'time' => '2015-09-17T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed_on' => null,
'[repository]' => 'ModulesEmployees'
},
(int) 1 => object(App\Model\Entity\ModulesEmployee) {
'id' => (int) 18,
'courses_employee_id' => (int) 32,
'course_module_id' => (int) 7,
'started_on' => object(Cake\I18n\Time) {
'time' => '2015-09-17T00:00:00+0000',
'timezone' => 'UTC',
'fixedNowTime' => false
},
'completed_on' => null,
'[repository]' => 'ModulesEmployees'
}
],
'[repository]' => 'CourseModules'
},
],
},
'employee' => object(App\Model\Entity\Employee) {
'id' => (int) 3,
'user_id' => (int) 4,
},
'[repository]' => 'CoursesEmployees'
}
You should look into matching
http://book.cakephp.org/3.0/en/orm/query-builder.html#filtering-by-associated-data
$query = $this->CoursesEmployees->findById($id)
->contain(['Your_Models_You_Wanna_Contain'])
->matching('Courses.CourseModules.ModulesEmployees', function ($q) use ($id) {
return $q->where(['courses_employee_id' => $id]);
});
if nothing matches then you wont get a CourseEmployee back aswell, if you would still need that you could also use contain:
http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#passing-conditions-to-contain
$query = $this->CoursesEmployees->findById($id)->contain([
'Courses.CourseModules.ModulesEmployees' => function ($q) use ($id) {
return $q
->where(['courses_employee_id' => $id]);
}
]);

Resources