简体   繁体   English

in_array()期望参数2为数组,给定null

[英]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. 我使用Symfony2创建了一个Web应用程序,并且使用PUGX User Bundle和FosUserBundle来管理2个不同的用户。

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: 142行如下:

   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. 不知道,因为我在用户和任务之间建立了一个@ORM \\ ManyToMany关联,所以我遇到了这个问题,这是另一个未出现的实体。 Before this I hadn't this problem. 在此之前,我没有这个问题。

I use PUGXUser Bundle because it helps to easily manage two different kind of user. 我使用PUGXUser Bundle,因为它有助于轻松管理两种不同类型的用户。 The User Entity is in my bundle, extends FosUserBundle..../Model/User.php and is extended by UserOperator and UserGroundStation. 用户实体在我的包中,扩展了FosUserBundle .... / Model / User.php,并由UserOperator和UserGroundStation扩展。

The definition of the role is in the FosUserBundle.../Model/User.php: 角色的定义在FosUserBundle ... / Model / User.php中:

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

and the costruct is: 的构造是:

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

Try to use: 尝试使用:

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

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

For me, i solved this problem just with parent::__construct() ; 对我来说,我只是用parent::__construct()解决了这个问题; on my entity 在我的实体上

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

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. 由于处理了AND子句,如果is_array为false,则不会证明in_array。

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)) 
/*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 . '  />';
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM