2013-11-28 5 views
4

내 Contact.php 모델에서 이러한 두 가지 방법이 있습니다이 Laravel 4

public function getSubscribers($listId) 
{ 
    return $this->withTrashed() 
     ->where(DB::raw("concat('',email * 1)"), '!=', DB::raw('email')) 
     ->where('opt_out', '0') 
     ->select('email') 
     ->chunk(1000, function($results) use ($listId) { $this->subscribeEmails($listId, $results); }); 
} 

public function subscribeEmails($listId, $subscribers) 
{ 
    $emails = array(); 

    foreach ($subscribers as $key => $subscriber) 
    { 
     $memberActivity = $subscriber->memberActivity($listId); 

     if (! $memberActivity['data']) 
     { 
      $emails[] = array('email' => $subscriber->email); 
     } 
     else 
     { 
      foreach ($memberActivity['data'] as $data) 
      { 
       foreach ($data['activity'] as $activity) 
       { 
        if ($activity['action'] !== 'unsub') 
        { 
         $emails[] = array('email' => $subscriber->email); 
        } 
       } 
      } 
     } 
    } 

    MailchimpWrapper::lists()->batchSubscribe($listId, $emails, false, true); 
} 

그리고 getSubscribers을() 메소드는 방법을 통해 내 AdminContactsController.php의 컨트롤러라고 라는 updateMailchimp은() :

public function updateMailchimp() 
{ 
    $this->contact->getSubscribers($this->listId); 

    $message = (object) array(
     'title'   => 'Excellent!', 
     'content'  => 'The Mailchimp newsletter list has been updated with the latest contacts from within the system.', 
     'alert_type' => 'success' 
    ); 

    return Redirect::back()->with('message', $message); 
} 

로컬이 전혀하지만 준비 서버에 좋은, 아무 문제도 작동하지 않습니다, 나는 라인 cotaining을 참조하는 다음과 같은 오류가 -> 덩어리 (1000, 기능 ($ 결과)를 사용 ($ listId) {$ this-> subscribeEmails ($ listId, $ results);}) ;:

Using $this when not in object context 

여기가 PHP 버전 문제입니까? 아니면 여기에없는 것이 있습니까?

+0

당신이 우리에게 말해 줄 수, 바로 당신이이 오류를받을 수 있나요 라인 : 당신은 use 키워드 내 $this에 대한 참조를 전달해야합니까? – matewka

+0

어쨌든, 청크 (1000, function ($ results) use ($ listId) {$ this-> subscribeEmails ($ listId, $ results);}); –

답변

2

코드가 로컬 호스트에서는 작동하지만 원격 서버에서는 작동하지 않는 이유는 아마도 PHP 버전의 차이 일 것입니다. PHP 5.4.0 이전 it is not possible to use $this from anonymous function.

public function getSubscribers($listId) 
{ 
    $that = $this; // <---- create reference to $this 
    return $this->withTrashed() 
     ->where(DB::raw("concat('',email * 1)"), '!=', DB::raw('email')) 
     ->where('opt_out', '0') 
     ->select('email') 
     ->chunk(1000, function($results) use (&$that, $listId) { $this->subscribeEmails($listId, $results); }); 
} 
+0

그래, Laravel 채팅의 anlutro가 올바른 길로 나를 잡았고 이것이 올바른 대답입니다. 건배. –