0

How can I get the type of current user with PUGXMultiUserBundle ? This code returns this error

{% if app.user.type == 'user_one' %}
//...
{% endif %}

This is the error

Method "type" for object "AppBundle\Entity\UserOne" does not exist

This is entity User

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"user_one" = "UserOne", "user_two" = "UserTwo"})
 *
 */

abstract class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

}

after updating database there is a new field named type created in table user

hous
  • 2,577
  • 2
  • 27
  • 66

1 Answers1

1

Ah, I see the problem. In the twig file, you are calling:

{% if app.user.type == 'user_one' %}

Where "app.user" specifies the object, and "type" specifies the method. But you don't have a "method" defined in the Class. But instead you have the DiscriminatorColumn.

A method would be something like:

public function type(){
   ...
}

Hopefully that makes sense.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • I think that I must forget the DiscriminatorColumn and create another attribute. [http://stackoverflow.com/questions/21284964/map-a-discriminator-column-to-a-field-with-doctrine-2?answertab=active#tab-top] – hous Jun 16 '16 at 17:32
  • Did I answer your question? If so, you should upvote my answer. – Alvin Bunk Jun 16 '16 at 17:38