2014-01-24 3 views
1

Symfony2를 사용하여 웹 응용 프로그램을 만들었으며 PUGX User Bundle과 FosUserBundle을 사용하여 2 명의 다른 사용자를 관리하고 있습니다.in_array()는 매개 변수 2가 배열이 될 것으로 예상합니다.

이 두 사용자 중 하나입니다

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

내가 등록하려고 할 때, 내가보기보다, 제출 양식을 작성 : 다음은

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 

라인 (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; 
      } 

사용자와 임무 사이에 @ORM \ ManyToMany를 연결 한 이래로이 문제가 있기 때문에 모릅니다. 그것은 여기에 나타나지 않는 다른 엔티티입니다. 이 전에는이 문제가 없었습니다.

두 개의 다른 종류의 사용자를 쉽게 관리 할 수 ​​있으므로 PUGXUser Bundle을 사용합니다. User Entity는 내 번들에 있으며 FosUserBundle ..../Model/User.php를 확장하며 UserOperator와 UserGroundStation에 의해 확장됩니다.

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

과 costruct은 다음과 같습니다 :

역할의 정의는 FosUserBundle .../모델/User.php에

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

'$ this-> roles'을 클래스에서'= array();로 초기화하십시오. –

+0

2 명의 다른 사용자에게 2 묶음을 사용하지 않아야합니다. 예를 들어, FOSUserBundle은 사용자 관리에 대해 필요한 모든 것을 제공합니다. – Xatenev

+0

'$ roles' 속성 정의를 보여주십시오. 이것은 단순한 배열인가 아니면 Doctrine ArrayCollection입니까? –

답변

7

봅니다 사용 :

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

작동하지만 다른 문제가 나타납니다. 그러나 나는 생각과는 완전히 다른 것입니다. "무결성 제약 위반 : 1048 열 '소금'은 null 일 수 없습니다." 문제가 무엇인지, 처음에는 왜 확인 되었습니까? 나는 PHP의 아주 새롭고, 나는 정보를 어디에서 찾을 지 모릅니다. –

+4

"두 번째 문제"를 해결하고 parent :: __ construct();를 호출했습니다. 감사합니다. –

+1

의도가 FOSUserBundle 코드를 수정하려는 경우 코드가 업데이트되면이 솔루션이 중단 될 수 있습니다. 대신 수정 된'addRole()'함수를'MyBundle \ Entity \ User'에 위치 시키십시오. – geoB

-1
 

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

5

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

나를 위해,이 문제는 parent::__construct()으로 해결되었습니다. 배열이 항상 있기 때문에 사용자와 in_array이 경우 입증되지 않습니다 AND 절의 처리의

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

을 존재하는 경우 내 기업

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

을에 당신은 ... 한 줄에 nessesary을 코딩 할 수 있습니다 is_array는 false입니다.

0

또한 배열이 비어 있지 않은지 먼저 확인하는 것이 좋습니다. 예 :

if (!empty($this->roles) && !in_array($role, $this->roles, true)) 
관련 문제