2011-04-20 26 views
10

Tôi đã làm việc với các tùy chọn pagphp pagoting trong 2 ngày. Tôi cần tạo một ĐỐI TÁC INNER để liệt kê một vài trường, nhưng tôi phải đối phó với tìm kiếm để lọc kết quả. Đây là phần mã trong đó tôi đối phó với các tùy chọn tìm kiếm bằng $this->passedArgs

function crediti() { 

    if(isset($this->passedArgs['Search.cognome'])) { 
       debug($this->passedArgs); 

       $this->paginate['conditions'][]['Member.cognome LIKE'] = str_replace('*','%',$this->passedArgs['Search.cognome']); 

     } 
     if(isset($this->passedArgs['Search.nome'])) { 
       $this->paginate['conditions'][]['Member.nome LIKE'] = str_replace('*','%',$this->passedArgs['Search.nome']); 

     } 

và sau

$this->paginate = array(

      'joins' => array(array('table'=> 'reservations', 
      'type' => 'INNER', 
      'alias' => 'Reservation', 
      'conditions' => array('Reservation.member_id = Member.id','Member.totcrediti > 0'))), 
      'limit' => 10); 
     $this->Member->recursive = -1; 
     $this->paginate['conditions'][]['Reservation.pagamento_verificato'] = 'SI'; 
     $this->paginate['fields'] = array('DISTINCT Member.id','Member.nome','Member.cognome','Member.totcrediti'); 
     $members = $this->paginate('Member'); 
     $this->set(compact('members')); 

INNER JOIN việc lành, nhưng $ this-> paginations bỏ qua mỗi $this->paginate['conditions'][] by $this->passedArgs và tôi không thể có ý tưởng làm thế nào tôi có thể làm việc nó ra. Không có truy vấn nào trong gỡ lỗi, chỉ cần INNER JOIN gốc. Ai đó có thể giúp tôi không? Cảm ơn bạn rất nhiều

Cập nhật: Không may mắn về điều đó. Tôi đã xử lý phần mã này trong nhiều giờ. Nếu tôi sử dụng

if(isset($this->passedArgs['Search.cognome'])) { 
        $this->paginate['conditions'][]['Member.cognome LIKE'] = str_replace('*','%',$this->passedArgs['Search.cognome']); 

      } 
$this->paginate['conditions'][]['Member.sospeso'] = 'SI'; 
     $this->Member->recursive = 0; 
     $this->paginate['fields'] = array(
      'Member.id','Member.nome','Member.cognome','Member.codice_fiscale','Member.sesso','Member.region_id', 
      'Member.district_id','Member.city_id','Member.date','Member.sospeso','Region.name','District.name','City.name'); 
     $sospesi = $this->paginate('Member'); 

mọi việc suôn sẻ, và từ debug tôi nhận được điều kiện đầu tiên và các điều kiện từ $this->paginate['conditions'][]['Member.cognome LIKE'], như bạn có thể nhìn thấy mảng $this->passedArgs

Array 
(
    [Search.cognome] => aiello 
) 

Array $this->paginate['conditions'][] 
(
    [0] => Array 
     (
      [Member.cognome LIKE] => aiello 
     ) 

    [1] => Array 
     (
      [Member.sospeso] => NO 
     ) 

Nhưng, nếu tôi viết tham gia với paginate, $this->paginate['conditions'][] sẽ bỏ qua tất cả nội dung và cho tôi gỡ lỗi, chỉ $this->paginate['conditions'][]['Reservation.pagamento_verificato'] = 'SI'; Một chút thông tin khác. Nếu tôi đặt tất cả nội dung đang xử lý với $this->paginate['conditions'][]['Reservation.pagamento_verificato'] = 'SI'; trước $this->paginate JOIN, không có nội dung nào trong số $this->paginate['conditions'][].

+0

Có một giải pháp cho đây này: CakePHP 2.3.4 http://stackoverflow.com/questions/13645296/cakephp-paginate-conditions-on-join-table/ 16789816 # 16789816 –

Trả lời

2

Tôi không chắc chắn nếu bạn cần những [] - cố gắng chỉ làm điều này:

$this->paginate['conditions']['Reservation.pagamento_verificato'] = 'SI'; 
1

tôi sử dụng các điều kiện khi tôi gọi phương thức đánh số trang.

$this->paginate($conditions) 

Điều này phù hợp với tôi, tôi hy vọng nó phù hợp với bạn!

Nếu bạn đã setted params trước, bạn có thể sử dụng:

$this->paginate(null,$conditions) 
15

Đây là một câu hỏi cũ, vì vậy tôi sẽ chỉ xem xét làm thế nào để làm một JOIN trong một paginate cho những người khác đến đây từ Google như Tôi đã làm. Dưới đây là đoạn code mẫu từ điều khiển của Widget, tham gia một Widget.user_id FK đến một cột User.id, chỉ hiển thị cho người dùng hiện tại (trong điều kiện):

// Limit widgets shown to only those owned by the user. 
$this->paginate = array(
    'conditions' => array('User.id' => $this->Auth->user('id')), 
    'joins' => array(
     array(
      'alias' => 'User', 
      'table' => 'users', 
      'type' => 'INNER', 
      'conditions' => '`User`.`id` = `Widget`.`user_id`' 
     ) 
    ), 
    'limit' => 20, 
    'order' => array(
     'created' => 'desc' 
    ) 
); 
$this->set('widgets', $this->paginate($this->Widget)); 

Điều này làm cho một truy vấn tương tự như:

SELECT widgets.* FROM widgets 
INNER JOIN users ON widgets.user_id = users.id 
WHERE users.id = {current user id} 

Và vẫn còn phân lớp.

+1

Ngoài ra, tài liệu duy nhất tôi có thể tìm thấy trên tùy chọn 'tham gia' nằm trong chính mã CakePHP, trong khối tài liệu dành cho Model.find(). –

+0

Tính năng này hoạt động. bạn cũng có thể '$ this-> paginate ('Widget');' – Bob

+1

Tôi tìm thấy hàm paginate() kém được suy nghĩ qua bánh. Cảm ơn bạn cho câu trả lời này - Tôi đã tìm kiếm một giải pháp cho vấn đề này. – Michaelkay

1

Điều này có thể giúp ích cho ai đó .... Đây là cách tôi đã tham gia phức tạp với phân trang trong cakephp.

$parsedConditions['`Assessment`.`showme`'] = 1; 
$parsedConditions['`Assessment`.`recruiter_id`'] = $user_id; 

$this->paginate = array(
'conditions' => array($parsedConditions), 
    'joins' => array(
    array(
     'alias' => 'UserTest', 
     'table' => 'user_tests', 
     'type' => 'LEFT', 
     'conditions' => '`UserTest`.`user_id` = `Assessment`.`testuser_id`' 
    ), 
    array(
     'alias' => 'RecruiterUser', 
     'table' => 'users', 
     'type' => 'LEFT', 
     'conditions' => '`Assessment`.`recruiter_id` = `RecruiterUser`.`id`' 
    ) 
    , 
    array(
     'alias' => 'OwnerUser', 
     'table' => 'users', 
     'type' => 'LEFT', 
     'conditions' => '`Assessment`.`owner_id` = `OwnerUser`.`id`' 
    ) 
), 
    'fields' => array('Assessment.id', 'Assessment.recruiter_id', 'Assessment.owner_id', 'Assessment.master_id', 'Assessment.title', 'Assessment.status', 'Assessment.setuptype','Assessment.linkkey', 'Assessment.review', 'Assessment.testuser_email', 'Assessment.counttype_2', 'Assessment.bookedtime', 'Assessment.textqstatus', 'Assessment.overallperc', 'UserTest.user_id', 'UserTest.fname', 'UserTest.lname', 'RecruiterUser.company_name', 'OwnerUser.company_name'), 
    'limit' => $limit, 
     'order'=> array('Assessment.endtime' => 'desc') 
    ); 
+0

Làm thế nào không tìm ra cách thực hiện một 'OR' đúng đắn và đơn giản mặc dù ... ghi lại câu trả lời ở đâu ('Đánh giá'.'recruiter_id' = '. $ User_id.' OR' Assessment'.'owner_id' = '. $ user_id. ') – gabrielkolbe

Các vấn đề liên quan