OJS OCS OMP OHS

You are viewing the PKP Support Forum | PKP Home Wiki



access item from another class within a class

OJS development discussion, enhancement requests, third-party patches and plug-ins.

Moderators: jmacgreg, btbell, michael, bdgregg, barbarah, asmecher

Forum rules
The Public Knowledge Project Support Forum is moving to http://forum.pkp.sfu.ca

This forum will be maintained permanently as an archived historical resource, but all new questions should be added to the new forum. Questions will no longer be monitored on this old forum after March 30, 2015.

access item from another class within a class

Postby jjgitties » Wed Apr 23, 2014 2:05 pm

Hello, I am trying to achieve the following.

I have a 2 classes.

PersonClass.inc.php
PersonClassDOA.inc.php

GroupClass.inc.php
GroupClassDOA.inc.php

I have a template file which is getting generated from GroupClassDOA. I want to access an object from PersonClassDOA from that tpl file.

For example, in my handler i have will have

$personDao = new &DAORegistry::getDAO('PersonDAO');

I want to pass
$templateMgr->assign('personDao', $personDao);

to the tpl file

and then within the tpl file access the object like this.

{ $personDao->$person->getName() }

Cant seem to get it to work.
jjgitties
 
Posts: 20
Joined: Tue Nov 12, 2013 11:11 am

Re: access item from another class within a class

Postby asmecher » Wed Apr 23, 2014 3:14 pm

Hi jjgitties,

Part of your question is design, part implementation, and there are many options for each -- but here's what I would do:
  • Don't expose the DAO directly to the template. In MVC terms, DAOs are model; templates are view. It's the controller (the handler class) that needs to link the two and serve as the controller.
  • Check your code for consistency. You've got DAO in some places and DOA in others.
  • In the handler class, where you make your template assignments, is where you should be working with the DAO. For example...
    Code: Select all
    $myDao =& DAORegistry::getDAO('MyDAO');
    $myObject =& $myDao->getById($someIdentifierValue);
    $templateMgr->assign('myObject', $myObject);
    This (pseudocode) will give your template the 'myObject' variable, which was fetched using the DAO; in the template you can access its member functions.
  • You've got some syntax problems -- $personDao->$person->getName() isn't correct.
Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 10015
Joined: Wed Aug 10, 2005 12:56 pm

Re: access item from another class within a class

Postby jjgitties » Thu Apr 24, 2014 6:09 am

asmecher wrote:Hi jjgitties,

Part of your question is design, part implementation, and there are many options for each -- but here's what I would do:
  • Don't expose the DAO directly to the template. In MVC terms, DAOs are model; templates are view. It's the controller (the handler class) that needs to link the two and serve as the controller.
  • Check your code for consistency. You've got DAO in some places and DOA in others.
  • In the handler class, where you make your template assignments, is where you should be working with the DAO. For example...
    Code: Select all
    $myDao =& DAORegistry::getDAO('MyDAO');
    $myObject =& $myDao->getById($someIdentifierValue);
    $templateMgr->assign('myObject', $myObject);
    This (pseudocode) will give your template the 'myObject' variable, which was fetched using the DAO; in the template you can access its member functions.
  • You've got some syntax problems -- $personDao->$person->getName() isn't correct.
Regards,
Alec Smecher
Public Knowledge Project Team


Thanks Alex. My issue is, I don't have a object id, I want to pass all the object ids and access them to the template.

Code: Select all
$myObject =& $myDao->getById($someIdentifierValue);


for example, in handler, for an array of object ids, in temple, interate through everything and getfunction on each object which is in a different class.
jjgitties
 
Posts: 20
Joined: Tue Nov 12, 2013 11:11 am

Re: access item from another class within a class

Postby asmecher » Thu Apr 24, 2014 7:55 am

Hi jjgitties,

In that case, if there should be a DAO function that gets all objects (returning either an array or an iterator object). For example, if you have a submission ID and want to get all authors, AuthorDAO has getAuthorsBySubmissionId. You can assign the resulting data set to the template (an array in the AuthorDAO example) and then use a Smarty {foreach} loop to go through the results in the template.

Regards,
Alec Smecher
Public Knowledge Project Team
asmecher
 
Posts: 10015
Joined: Wed Aug 10, 2005 12:56 pm

Re: access item from another class within a class

Postby jjgitties » Mon Apr 28, 2014 7:37 am

thanks. i will try this.
jjgitties
 
Posts: 20
Joined: Tue Nov 12, 2013 11:11 am


Return to OJS Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron