Quantcast

ZF2: Access Service Manager from Mapper

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

ZF2: Access Service Manager from Mapper

Ralf Eggert
Hi,

I don't get it. I have a data mapper in
/module/User/src/User/Mapper/User.php which is just a normal class.

How can I access the service locator from this class?

Best regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2: Access Service Manager from Mapper

Ralf Eggert
Hi again,

forget it. I found the solution myself. I actually don't want to access
the SM from the Mapper. I only needed the corresponding table object.
Like this:

class Module
{
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'User\Table\User' =>  function($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $table     = new UserTable($dbAdapter);
                    return $table;
                },
                'User\Mapper\User' =>  function($sm) {
                    $table  = $sm->get('User\Table\User');
                    $mapper = new UserMapper($table);
                    return $mapper;
                },
            ),
        );
    }
}

Best regards,

Ralf


--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2: Access Service Manager from Mapper

Ralf Eggert
Hi again,

another question came to my mind. Which way do you prefer.

Using the constructor to pass objects:

  'User\Mapper\User' =>  function($sm) {
    $table  = $sm->get('User\Table\User');
    $mapper = new UserMapper($table);
    return $mapper;
  },

Or using setter methods to pass objects

  'User\Mapper\User' =>  function($sm) {
    $table  = $sm->get('User\Table\User');
    $mapper = new UserMapper();
    $mapper->setDbTable($table);
    return $mapper;
  },

Any comments?

Regards,

Ralf

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2: Access Service Manager from Mapper

Alex Major
For me it depends on two things:

1) What it *absolutely* required for the Class to be usable? Everything
that must be there should be in the constructor, other things via set
methods.

2) How long is the constructor? I'm generally not a fan of having a
constructor with 10's of parameters. This usually comes about by not
following point #1

But it comes down to personal preference :)

Alex.

On Thu, Aug 16, 2012 at 7:43 PM, Ralf Eggert <[hidden email]> wrote:

> Hi again,
>
> another question came to my mind. Which way do you prefer.
>
> Using the constructor to pass objects:
>
>   'User\Mapper\User' =>  function($sm) {
>     $table  = $sm->get('User\Table\User');
>     $mapper = new UserMapper($table);
>     return $mapper;
>   },
>
> Or using setter methods to pass objects
>
>   'User\Mapper\User' =>  function($sm) {
>     $table  = $sm->get('User\Table\User');
>     $mapper = new UserMapper();
>     $mapper->setDbTable($table);
>     return $mapper;
>   },
>
> Any comments?
>
> Regards,
>
> Ralf
>
> --
> List: [hidden email]
> Info: http://framework.zend.com/archives
> Unsubscribe: [hidden email]
>
>
>
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: ZF2: Access Service Manager from Mapper

weierophinney
Administrator
In reply to this post by Ralf Eggert
-- Ralf Eggert <[hidden email]> wrote
(on Thursday, 16 August 2012, 08:43 PM +0200):

> Hi again,
>
> another question came to my mind. Which way do you prefer.
>
> Using the constructor to pass objects:
>
>   'User\Mapper\User' =>  function($sm) {
>     $table  = $sm->get('User\Table\User');
>     $mapper = new UserMapper($table);
>     return $mapper;
>   },
>
> Or using setter methods to pass objects
>
>   'User\Mapper\User' =>  function($sm) {
>     $table  = $sm->get('User\Table\User');
>     $mapper = new UserMapper();
>     $mapper->setDbTable($table);
>     return $mapper;
>   },
>
> Any comments?

Constructor arguments are for required collaborators -- stuff that, if
it's missing, will mean no operations you do in the object will work.
Additionally, I typically only like constructor arguments if changing
the collaborator will cause problems later.

Setter injection is for either collaborators that are optional (i.e.,
some or all operations can work without them), or which you allow
replacing at any point during operations without side-effects.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]


Loading...