Quantcast

Bug or feature? Can't modify ViewModel after calling setVariables()

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

Bug or feature? Can't modify ViewModel after calling setVariables()

demiankatz
I have discovered that (in beta5) if I initialize a Zend\View\Model\ViewModel with an array or explicitly call setVariables(), I lose the ability to set additional view variables on the fly.  Is this a bug, or am I just using the class incorrectly?  Some examples:

        $view = new ViewModel();
        $view->setVariables(array('a' => 'this'));
        $view->b = 'that';
        echo $view->a . $view->b;    // this

        $view = new ViewModel(array('a' => 'this'));
        $view->b = 'that';
        echo $view->a . $view->b;    // this

        $view = new ViewModel();
        $view->a = 'this';
        $view->b = 'that';
        echo $view->a . $view->b;    // thisthat

I would expect all three of these scenarios to output "thisthat," but only the third one actually does.

thanks,
Demian

From: Demian Katz [[hidden email]]
Sent: Friday, July 13, 2012 7:12 AM
To: David Muir; Marco Pivetta
Cc: [hidden email]
Subject: RE: [zf-contributors] Zend\Db: Why no __set() in AbstractRowGateway?

Yes, thank you, that is definitely a more direct illustration of the problem.

I have run these tests with ZF2 beta5, and they behave exactly as your comments predict.  This certainly feels like a bug to me -- the object should either work consistently or completely disallow one mode of access!  Should I open a JIRA ticket?

- Demian

From: David Muir [[hidden email]]
Sent: Thursday, July 12, 2012 8:25 PM
To: Marco Pivetta
Cc: Demian Katz; [hidden email]
Subject: Re: [zf-contributors] Zend\Db: Why no __set() in AbstractRowGateway?

If I'm understanding the problem, then this would illustrate it better:

$r['username'] = 'something';
echo $r['username']; //prints something
echo $r->username; //prints something

$r->username = 'fishy';
echo $r['username']; //prints something
echo $r->username; //prints fishy

Cheers,
David

On 13/07/12 01:20, Marco Pivetta wrote:
What would the advantage in having `__set` and `__get` here be?

Marco Pivetta

http://twitter.com/Ocramius     

http://marco-pivetta.com    



On 12 July 2012 17:18, Demian Katz <[hidden email]> wrote:

The documentation and examples led me to believe that I could use object or array access for interacting with Zend\Db\RowGateway objects.  However, object access doesn’t work for creating new objects.  For example:

 

// works

$r = new RowGateway('id', 'user', $adapter);

$r['username'] = 'fishy';

$r->save();

 

// doesn’t work

$r = new RowGateway('id', 'user', $adapter);

$r->username = 'fishy';

$r->save();

 

It appears that this could be easily remedied by simply adding this to the AbstractRowGateway class:

 

public function __set($offset, $value)

{

    return $this->offsetSet($offset, $value);

}

 

Is there a compelling reason why this feature is not present?  If it’s an oversight, I’ll go ahead and patch my copy of the framework until the next release.  If it’s an intentional design decision, a note about it in the documentation would probably be a good idea – the fact that object access works in some contexts and not others is potentially confusing.

 

thanks,

Demian




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

Re: Bug or feature? Can't modify ViewModel after calling setVariables()

weierophinney
Administrator
-- Demian Katz <[hidden email]> wrote
(on Friday, 13 July 2012, 11:39 AM +0000):

> I have discovered that (in beta5) if I initialize a Zend\View\Model\ViewModel
> with an array or explicitly call setVariables(), I lose the ability to set
> additional view variables on the fly.  Is this a bug, or am I just using the
> class incorrectly?  Some examples:
>
>         $view = new ViewModel();
>         $view->setVariables(array('a' => 'this'));
>         $view->b = 'that';
>         echo $view->a . $view->b;    // this
>
>         $view = new ViewModel(array('a' => 'this'));
>         $view->b = 'that';
>         echo $view->a . $view->b;    // this
>
>         $view = new ViewModel();
>         $view->a = 'this';
>         $view->b = 'that';
>         echo $view->a . $view->b;    // thisthat
>
> I would expect all three of these scenarios to output "thisthat," but only the
> third one actually does.

It's a regression; I'll have a patch in shortly. Thanks for the report!


> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> From: Demian Katz [[hidden email]]
> Sent: Friday, July 13, 2012 7:12 AM
> To: David Muir; Marco Pivetta
> Cc: [hidden email]
> Subject: RE: [zf-contributors] Zend\Db: Why no __set() in AbstractRowGateway?
>
> Yes, thank you, that is definitely a more direct illustration of the problem.
>
> I have run these tests with ZF2 beta5, and they behave exactly as your comments
> predict.  This certainly feels like a bug to me -- the object should either
> work consistently or completely disallow one mode of access!  Should I open a
> JIRA ticket?
>
> - Demian
> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> From: David Muir [[hidden email]]
> Sent: Thursday, July 12, 2012 8:25 PM
> To: Marco Pivetta
> Cc: Demian Katz; [hidden email]
> Subject: Re: [zf-contributors] Zend\Db: Why no __set() in AbstractRowGateway?
>
> If I'm understanding the problem, then this would illustrate it better:
>
>
> $r['username'] = 'something';
> echo $r['username']; //prints something
> echo $r->username; //prints something
>
> $r->username = 'fishy';
> echo $r['username']; //prints something
> echo $r->username; //prints fishy
>
>
> Cheers,
> David
>
> On 13/07/12 01:20, Marco Pivetta wrote:
>
>     What would the advantage in having `__set` and `__get` here be?
>
>     Marco Pivetta
>
>     http://twitter.com/Ocramius     
>
>     http://marco-pivetta.com   
>
>
>
>     On 12 July 2012 17:18, Demian Katz <[hidden email]> wrote:
>
>
>         The documentation and examples led me to believe that I could use
>         object or array access for interacting with Zend\Db\RowGateway
>         objects.  However, object access doesn’t work for creating new
>         objects.  For example:
>
>          
>
>         // works
>
>         $r = new RowGateway('id', 'user', $adapter);
>
>         $r['username'] = 'fishy';
>
>         $r->save();
>
>          
>
>         // doesn’t work
>
>         $r = new RowGateway('id', 'user', $adapter);
>
>         $r->username = 'fishy';
>
>         $r->save();
>
>          
>
>         It appears that this could be easily remedied by simply adding this to
>         the AbstractRowGateway class:
>
>          
>
>         public function __set($offset, $value)
>
>         {
>
>             return $this->offsetSet($offset, $value);
>
>         }
>
>          
>
>         Is there a compelling reason why this feature is not present?  If it’s
>         an oversight, I’ll go ahead and patch my copy of the framework until
>         the next release.  If it’s an intentional design decision, a note about
>         it in the documentation would probably be a good idea – the fact that
>         object access works in some contexts and not others is potentially
>         confusing.
>
>          
>
>         thanks,
>
>         Demian
>
>
>
>
>

--
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Bug or feature? Can't modify ViewModel after calling setVariables()

weierophinney
Administrator
-- Matthew Weier O'Phinney <[hidden email]> wrote
(on Friday, 13 July 2012, 09:14 AM -0500):

> -- Demian Katz <[hidden email]> wrote
> (on Friday, 13 July 2012, 11:39 AM +0000):
> > I have discovered that (in beta5) if I initialize a Zend\View\Model\ViewModel
> > with an array or explicitly call setVariables(), I lose the ability to set
> > additional view variables on the fly.  Is this a bug, or am I just using the
> > class incorrectly?  Some examples:
> >
> >         $view = new ViewModel();
> >         $view->setVariables(array('a' => 'this'));
> >         $view->b = 'that';
> >         echo $view->a . $view->b;    // this
> >
> >         $view = new ViewModel(array('a' => 'this'));
> >         $view->b = 'that';
> >         echo $view->a . $view->b;    // this
> >
> >         $view = new ViewModel();
> >         $view->a = 'this';
> >         $view->b = 'that';
> >         echo $view->a . $view->b;    // thisthat
> >
> > I would expect all three of these scenarios to output "thisthat," but only the
> > third one actually does.
>
> It's a regression; I'll have a patch in shortly. Thanks for the report!

PR is here: https://github.com/zendframework/zf2/pull/1880

Looking at it, I'm unsure it should have ever worked, tbh -- __set() was
not populating the internal $variables array.


> > ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> > From: Demian Katz [[hidden email]]
> > Sent: Friday, July 13, 2012 7:12 AM
> > To: David Muir; Marco Pivetta
> > Cc: [hidden email]
> > Subject: RE: [zf-contributors] Zend\Db: Why no __set() in AbstractRowGateway?
> >
> > Yes, thank you, that is definitely a more direct illustration of the problem.
> >
> > I have run these tests with ZF2 beta5, and they behave exactly as your comments
> > predict.  This certainly feels like a bug to me -- the object should either
> > work consistently or completely disallow one mode of access!  Should I open a
> > JIRA ticket?
> >
> > - Demian
> > ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
> > From: David Muir [[hidden email]]
> > Sent: Thursday, July 12, 2012 8:25 PM
> > To: Marco Pivetta
> > Cc: Demian Katz; [hidden email]
> > Subject: Re: [zf-contributors] Zend\Db: Why no __set() in AbstractRowGateway?
> >
> > If I'm understanding the problem, then this would illustrate it better:
> >
> >
> > $r['username'] = 'something';
> > echo $r['username']; //prints something
> > echo $r->username; //prints something
> >
> > $r->username = 'fishy';
> > echo $r['username']; //prints something
> > echo $r->username; //prints fishy
> >
> >
> > Cheers,
> > David
> >
> > On 13/07/12 01:20, Marco Pivetta wrote:
> >
> >     What would the advantage in having `__set` and `__get` here be?
> >
> >     Marco Pivetta
> >
> >     http://twitter.com/Ocramius     
> >
> >     http://marco-pivetta.com   
> >
> >
> >
> >     On 12 July 2012 17:18, Demian Katz <[hidden email]> wrote:
> >
> >
> >         The documentation and examples led me to believe that I could use
> >         object or array access for interacting with Zend\Db\RowGateway
> >         objects.  However, object access doesn’t work for creating new
> >         objects.  For example:
> >
> >          
> >
> >         // works
> >
> >         $r = new RowGateway('id', 'user', $adapter);
> >
> >         $r['username'] = 'fishy';
> >
> >         $r->save();
> >
> >          
> >
> >         // doesn’t work
> >
> >         $r = new RowGateway('id', 'user', $adapter);
> >
> >         $r->username = 'fishy';
> >
> >         $r->save();
> >
> >          
> >
> >         It appears that this could be easily remedied by simply adding this to
> >         the AbstractRowGateway class:
> >
> >          
> >
> >         public function __set($offset, $value)
> >
> >         {
> >
> >             return $this->offsetSet($offset, $value);
> >
> >         }
> >
> >          
> >
> >         Is there a compelling reason why this feature is not present?  If it’s
> >         an oversight, I’ll go ahead and patch my copy of the framework until
> >         the next release.  If it’s an intentional design decision, a note about
> >         it in the documentation would probably be a good idea – the fact that
> >         object access works in some contexts and not others is potentially
> >         confusing.
> >
> >          
> >
> >         thanks,
> >
> >         Demian
> >
> >
> >
> >
> >
>
> --
> 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
>

--
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
Loading...