Gianni Alessandro
Gianni Alessandro

Reputation: 910

in_array() expects parameter 2 to be array, null given

I made a web-application with Symfony2, and I'm using PUGX User Bundle and FosUserBundle to manage 2 different users.

This is one of the two user:

  /**
   * @ORM\Entity
   * @ORM\Table(name="user_Operator")
   * @ORM\HasLifecycleCallbacks()
   * @UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User", message="Username already_used")
   * @UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User", message="Email already_used")
   */
  class UserOperator extends User
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;

          /**
       * @ORM\PrePersist
       */
      public function setCreatedAtValue()
      {
          $this->addRole('ROLE_OPERATOR');
      }    
  }

When I try to register, I fill the form, submit, than appears:

Warning: in_array() expects parameter 2 to be array, null given in C:\BitNami\wampstack-
5.4.23-0\frameworks\symfony\vendor\friendsofsymfony\user-    
bundle\FOS\UserBundle\Model\User.php line 142

The line 142 is the following:

   135    public function addRole($role)
          {
              $role = strtoupper($role);
              if ($role === static::ROLE_DEFAULT) {
                  return $this;
              }

   142        if (!in_array($role, $this->roles, true)) {
                  $this->roles[] = $role;
              }

              return $this;
          }

Don't know because I have this problem since I made an association @ORM\ManyToMany between User and Mission, that is another entity that here doesn't appear. Before this I hadn't this problem.

I use PUGXUser Bundle because it helps to easily manage two different kind of user. The User Entity is in my bundle, extends FosUserBundle..../Model/User.php and is extended by UserOperator and UserGroundStation.

The definition of the role is in the FosUserBundle.../Model/User.php:

/**
 * @var array
 */
protected $roles;

and the costruct is:

public function __construct()
{
    $this->roles = array();
}

Upvotes: 3

Views: 51778

Answers (5)

Victor Bocharsky
Victor Bocharsky

Reputation: 12306

Try to use:

if (is_array($this->roles)) {
    if (!in_array($role, $this->roles, true)) {
        $this->roles[] = $role;
    }
}

Upvotes: 16

B.K
B.K

Reputation: 897

It can also help to first check if the array is not empty. For example:

if (!empty($this->roles) && !in_array($role, $this->roles, true)) 

Upvotes: 1

Netzelch
Netzelch

Reputation: 41

You can code it in one line... nessesary if an array not always exists

if ( is_array($maybe_array) AND in_array($needle, $maybe_array) ) {...

because of the handling of the AND-clause your in_array will not be proved if is_array is false.

Upvotes: 4

FuturWebs
FuturWebs

Reputation: 61

in_array() expects parameter 2 to be array, null given

For me, i solved this problem just with parent::__construct(); on my entity

public function __construct()
{
    parent::__construct();
}

Upvotes: 6

Mohd Abdul Baquee
Mohd Abdul Baquee

Reputation: 449


/*Item's lists */
    $items = array('Apple', 'Banana', 'Cherry', 'Coconut', 'Guava', 'Lemon', 'Mango');
/*Fruit's lists to be search in Items */
    $fruits = array('Apple', 'Coconut', 'Mango');
    foreach ($items as $item) {
        if (in_array($item, $fruits)) {
    $checked = "checked";
        } 
        else{
    $checked = "unchecked";
        }
   echo '< input type="checkbox" name="checkboxes[]"' . $checked . '  />';
   }

Upvotes: -1

Related Questions