Note that there are some explanatory texts on larger screens.

plurals
  1. POZFCUser Identity within another entity
    primarykey
    data
    text
    <h2>Is there a way where you can get the ZfcUser Identity or user_id in another entity?</h2> <p>I've been watching the following link, dealing with the servicemanager.</p> <p><a href="https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in#service-manager" rel="nofollow">https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in#service-manager</a></p> <p>When using the service manager, I get the following:</p> <blockquote> <p>Call to a member function get() on a non-object</p> </blockquote> <p>at the following line:</p> <pre><code>$auth = $sm-&gt;get('zfcuser_auth_service'); </code></pre> <h2><strong>Using ServiceManager</strong></h2> <pre><code>public class CustomEntity implements ServiceManagerAwareInterface { protected $sm; /** * @ORM\Column(type="integer", nullable=false) */ protected $creator_id; /** * * @ORM\Column(type="datetime", nullable=false) */ protected $creation_date; /** * @ORM\Column(type="integer", nullable=true) */ protected $last_modifier_id; /** * @ORM\Column(type="datetime", nullable=true) */ protected $last_modified_date; public function setServiceManager(ServiceManager $serviceManager) { $this-&gt;sm = $serviceManager; return $this-&gt;sm; } public function getLoggedUserId() { $auth = $this-&gt;getServiceManager()-&gt;get('zfcuser_auth_service'); return $auth-&gt;getIdenty()-&gt;getId(); } /** * @ORM\PrePersist */ public function onPrePersist() { //$this-&gt;creator_id = $this-&gt;getLoggedUserId(); $this-&gt;creation_date = new \DateTime('now'); } /** * @ORM\PreUpdate */ public function onUpdate() { //$this-&gt;last_modifier_id = $this-&gt;getLoggedUserId(); $this-&gt;last_modified_date = new \DateTime('now'); } </code></pre> <p>In the above code I've been trying to use the ServiceManager but without any luck. As I said, its going wrong at this point:</p> <blockquote> <p>Call to a member function get() on a non-object</p> </blockquote> <p>at the following line:</p> <pre><code>$auth = $this-&gt;getServiceManager()-&gt;get('zfcuser_auth_service'); </code></pre> <p>Aswell as the ServiceManager Interfaces don't have a Get Method, to retreive a ServiceManager. So in that case I also tried to use the ServiceLocator instead of the Manager, but also without any luck.</p> <h2><strong>Old version</strong></h2> <p>I've been able to set the User_id with the controller by creating a set method within the entity.</p> <p><strong>Controller:</strong></p> <pre><code>public function addAction() { ... $customEntity-&gt;setCreator($this-&gt;zfcUserAuthentication()-&gt;getIdentity()-&gt;getId()); $this-&gt;getEntityManager()-&gt;persist($customEntity); $this-&gt;getEntityManager()-&gt;flush(); ... } </code></pre> <p><strong>Entity:</strong> </p> <pre><code>public function setCreator($user_id){ $this-&gt;creator_id = $user_id; $this-&gt;creation_date = new \DateTime; } public function setModifier($user_id){ $this-&gt;last_modifier_id = $user_id; $this-&gt;last_modified_date = new \DateTime; } </code></pre> <h2><strong>New version</strong></h2> <p>Instead of setting setCreator or setModifier everytime you are executing an action, it would have been lovely to get the User Identity at the Doctrine Persist or Update. </p> <p>I would like to have the zfcUser_id inside my Entity. Some code example to show you what I ment:</p> <pre><code>/** * @ORM\Entity * @ORM\HasLifecycleCallbacks */ class CustomEntity { /** * @ORM\Column(type="integer", nullable=false) */ protected $creator_id; /** * * @ORM\Column(type="datetime", nullable=false) */ protected $creation_date; ... /** * @ORM\PrePersist */ public function onPrePersist() { $this-&gt;creator_id = ZfcUser_id $this-&gt;creation_date = new \DateTime('now'); } /** * @ORM\PreUpdate */ public function onUpdate() { $this-&gt;last_modifier_id = ZfcUser_id $this-&gt;last_modified_date = new \DateTime('now'); } </code></pre> <p>As you can see, I would like to have the ZfcUser_id at the following functions: onPrePersist and onUpdate to save the creator or updater of this entity/object. And for this version I was looking for a way to get the ZfcUser Identity, and found this link: </p> <p><a href="https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in#service-manager" rel="nofollow">https://github.com/ZF-Commons/ZfcUser/wiki/How-to-check-if-the-user-is-logged-in#service-manager</a> </p> <h2>Result based on replies</h2> <p>Create your Entity, create some Set Methods for your fields/attributes. Let your Controller handle this part. For the date(Time) as an default value is being set, you can use onPrePresist, onUpdate or you should check out other Doctrine Block Annotations.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload