Skip to content
This repository has been archived by the owner on May 14, 2018. It is now read-only.

Using BjyAuthorize with Doctrine 2 and FormMultiCheckbox #117

Closed
lampi87 opened this issue Mar 11, 2013 · 11 comments
Closed

Using BjyAuthorize with Doctrine 2 and FormMultiCheckbox #117

lampi87 opened this issue Mar 11, 2013 · 11 comments

Comments

@lampi87
Copy link

lampi87 commented Mar 11, 2013

Hi!

I started to rewrite site with new bjyauthorize version cloned today. The navigation works. But using FormMultiCheckbox does not work.

If have two roles, user & admin. User is the parent role of admin. So every admin can access all users sites and more. My current user has the role admin. On my PersonForm the wrong option, user, is selected. And I got the php notice "Object of class Base\Entity\Role could not be converted to int in vendor/zendframework/zendframework/library/Zend/Form/View/Helper/FormMultiCheckbox.php on line 293"

Save is not possible too (Call to a member function toArray() on a non-object in vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/Strategy/AllowRemoveByValue.php on line 57)

Please, can you help me?

Greetings

@Ocramius
Copy link
Contributor

@lampi87 this does not look like a BjyAuthorize issue. It's more about how you're managing your entities. You should use DoctrineModule's form elements and show some code first.

@lampi87
Copy link
Author

lampi87 commented Mar 11, 2013

The entities are more or less the same which are provided with bjyauthorize. The problems started with updating my vendor directory.

Person entity (main part):

    public function getRoles()
    {
        return $this->roles->getValues();
    }

    public function addRoles(Collection $roles)
    {
        foreach ($roles as $role) {
            $role->getPersons()->add($this);
            $this->roles->add($role);
        }
    }

    public function removeRoles(Collection $roles)
    {
        foreach ($roles as $role) {
            $role->getPersons()->removeElement($this);
            $this->roles->removeElement($role);
        }
    }

Role entity:

class Role implements HierarchicalRoleInterface
{
    /**
     * @var int
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string
     * @ORM\Column(type="string", length=255, unique=true, nullable=true)
     */
    protected $roleId;

    /**
     * @var Role
     * @ORM\ManyToOne(targetEntity="Vrcts\Entity\Role")
     */
    private $parent;

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = (int)$id;
    }

    public function getRoleId()
    {
        return $this->roleId;
    }

    public function setRoleId($roleId)
    {
        $this->roleId = (string) $roleId;
    }

    public function getParent()
    {
        return $this->parent;
    }

    public function setParent(Role $parent)
    {
        $this->parent = $parent;
    }
}

PersonForm:

    $this->add(
            array(
                'type' => 'DoctrineORMModule\Form\Element\EntityMultiCheckbox',
                'name' => 'roles',
                'options' => array(
                    'object_manager' => $this->getObjectManager(),
                    'target_class' => 'Base\Entity\Role',
                    'property' => 'roleId'
                )
            )
        );

The wrong role is selected (this should be a bjyauthorized issue) selected. User (parent of admin) is selected instead of admin.
After pressing save at this form i get the error mentioned above

@lampi87
Copy link
Author

lampi87 commented Mar 11, 2013

Maybe there is a problem at the point where the role collection is built up or is returned

@Ocramius
Copy link
Contributor

I have no idea of what is going on here, but this is not BjyAuthorize issue. Ask on the mailing list first (zf mailing list) or on stackoverflow.

Also not sure where things happen since you didn't provide a stack trace. As said, ask on the ML/StackOverflow first.

@lampi87
Copy link
Author

lampi87 commented Mar 11, 2013

The stack trace after saving the form

( ! ) Fatal error: Call to a member function toArray() on a non-object in /home/alex/git/base/vendor/doctrine/doctrine-module/src/DoctrineModule/Stdlib/Hydrator/Strategy/AllowRemoveByValue.php on line 56
Call Stack

Time Memory Function Location

1 0.0001 257816 {main}( ) ../index.php:0
2 0.1356 15036112 Zend\Mvc\Application->run( ) ../index.php:12
3 0.1411 15411376 Zend\EventManager\EventManager->trigger( ) ../Application.php:294
4 0.1411 15411376 Zend\EventManager\EventManager->triggerListeners( ) ../EventManager.php:204
5 0.1412 15413136 call_user_func ( ) ../EventManager.php:460
6 0.1412 15413168 Zend\Mvc\DispatchListener->onDispatch( ) ../EventManager.php:460
7 0.1478 16308848 Zend\Mvc\Controller\AbstractController->dispatch( ) ../DispatchListener.php:114
8 0.1478 16309568 Zend\EventManager\EventManager->trigger( ) ../AbstractController.php:117
9 0.1478 16309568 Zend\EventManager\EventManager->triggerListeners( ) ../EventManager.php:204
10 0.1481 16322928 call_user_func ( ) ../EventManager.php:460
11 0.1481 16322960 Zend\Mvc\Controller\AbstractActionController->onDispatch( ) ../EventManager.php:460
12 0.1481 16323184 Tableeditor\Controller\TableeditorController->editAction( ) ../AbstractActionController.php:83
13 0.1630 18432160 Zend\Form\Form->isValid( ) ../TableeditorController.php:143
14 0.1701 19004968 Zend\Form\Form->bindValues( ) ../Form.php:453
15 0.1706 19009136 Zend\Form\Fieldset->bindValues( ) ../Form.php:305
16 0.1706 19011176 DoctrineModule\Stdlib\Hydrator\DoctrineObject->hydrate( ) ../Fieldset.php:531
17 0.1706 19011240 DoctrineModule\Stdlib\Hydrator\DoctrineObject->hydrateByValue( ) ../DoctrineObject.php:107
18 0.1709 19014360 DoctrineModule\Stdlib\Hydrator\DoctrineObject->toMany( ) ../DoctrineObject.php:239
19 0.1709 19014872 Zend\Stdlib\Hydrator\AbstractHydrator->hydrateValue( ) ../DoctrineObject.php:392
20 0.1709 19014936 DoctrineModule\Stdlib\Hydrator\Strategy\AllowRemoveByValue->hydrate( ) ../AbstractHydrator.php:129

@Ocramius
Copy link
Contributor

Yep, as you see this is only happening in the abstract hydrator. You should really ask on the mailing list first. There's no single BjyAuthorize in that stack trace.

@lampi87
Copy link
Author

lampi87 commented Mar 11, 2013

I know, that there is not single BjyAuthorize in that stack trace, but this happens till I have updated my entities to fit to the new version.

one main change is that my getRole function

public function getRoles()
{
    return $this->roles->getValues();
}

now returns only the values and not the collection as it was done before the main change of handling Doctrine entities. If I switch back to return the whole collection byjauthorize errors occur

@Ocramius
Copy link
Contributor

That's because the getRoles returns an array instead of a collection.
Either remove it from the dropdown in your form or handle hydration for
that field on your own

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/

On 11 March 2013 14:11, Alex Lampret [email protected] wrote:

I know, that there is not single BjyAuthorize in that stack trace, but
this happens till I have updated my entities to fit to the new version.

one main change is that my getRole function

public function getRoles()
{
return $this->roles->getValues();
}

now returns only the values and not the collection as it was done before
the main change of handling Doctrine entities. If I switch back to return
the whole collection byjauthorize errors occur


Reply to this email directly or view it on GitHubhttps://github.com//issues/117#issuecomment-14712269
.

@edmeister
Copy link

@lampi87 were you able to fix this issue? I've run into the same problem.

@lampi87
Copy link
Author

lampi87 commented Jun 17, 2013

I have created a second function (i.e. getUserRoles) which returns the arraycollection

public function getUserRoles()
{
    $this->roles;
}

and I'm using this function for my forms

@webdevilopers
Copy link

Maybe this issue about managing roles with ZfcUserAdmin is interesting:
Danielss89/ZfcUserAdmin#30

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants