0

I build my business logic with propel. All the models extends an abstract model, in which I want to inject the security context:

Services.yml

parameters:
    Abstract.Model : 'FooBundle\Model\AbstractModel'

services:
    Abstract.Model:
        class: 'FooBundle\Model\AbstractModel'
        calls:
            - [setSecurity, ["@security.context"]]

The AbstractModel

abstract class AbstractModel extends BaseObject
{
    /**
     * @param $security \Symfony\Component\Security\Core\SecurityContext
     */
    protected $security;

    /**
     * Sets the security context
     *
     * @param $security \Symfony\Component\Security\Core\SecurityContext
     */
    public function setSecurity($security)
    {
        $this->security = $security;
    }

    /**
     * Code to be run before persisting the object
     *
     * @param PropelPDO $con
     * @return boolean
     */
    public function preSave(\PropelPDO $con = null)
    {
        $token = $this->security->getToken();
    }
}

If propel runs the preSave method, it responses with a 500:

FatalErrorException: Error: Call to a member function getToken() on a non-object

Anyone know whats going wrong here?

Greetings

Update_01:

public function save(PropelPDO $con = null, $skipReload = false)
{
    ...
    $ret = $this->preSave($con);
}
  • Can you show us the *user* code that's triggering `preSave` ? – Touki Mar 03 '14 at 11:19
  • Hey, I've updated my code example from above. Thanks for your help. –  Mar 03 '14 at 12:02
  • An Abstract class cannot be instantiated. – stwe Mar 03 '14 at 12:29
  • Well, to do short, you need to call your model from the service container. Since your class is abstract, my guess is that you didn't set any [`parent`](http://symfony.com/doc/current/components/dependency_injection/parentservices.html) on the concrete class declaration – Touki Mar 03 '14 at 13:14
  • Ah I see...Well, seems that I cant do this with DI, because I never call this model. Its just an wrapper for all of my concrete models, and I want to be able, to insert created_by and updated_by user id. thats the reason, why I need the security context. –  Mar 03 '14 at 13:20

1 Answers1

1

An abstract class cannot be instantiated.

Extend your Abstract class:

class MyModel extends AbstractModel 
{
}

and use this class as service:

services:
    My.Model:
        class: 'FooBundle\Model\MyModel'
        calls:
            - [setSecurity, ["@security.context"]]
stwe
  • 1,262
  • 15
  • 18
  • Hm...I just kicked the abstract keyword for testing purposes, but it doesn't works anyway. same error. Is there something wrong with my configuration? Please tell me, if you need more information. –  Mar 03 '14 at 13:12
  • two reasons: 1. You have to empty the Chache (php app/console cache:clear) and 2. The security.context acts as a cache. Remove getToken() and call it when needed. See: http://stackoverflow.com/questions/22116642/getuser-in-symfony2s-service-constructor#comment33554223_22116642 – stwe Mar 03 '14 at 13:51