แท็กค้นหา Cakephp 3

จาก Cakebook เราได้ตัวอย่างการทำงานนี้:

public function findTagged(Query $query, array $options) {
  return $this->find()
    ->distinct(['Articles.id'])
    ->matching('Tags', function ($q) use ($options) {
    if (empty($options['tags'])) {
        return $q->where(['Tags.title IS' => null]);
    }
    return $q->where(['Tags.title IN' => $options['tags']]);
  });
}

ตัวอย่างเช่น ที่อยู่ article/tagged/test/new จะค้นหาบทความที่มีแท็ก "test" หรือแท็ก "new" ฉันต้องการแก้ไขโค้ดนี้ด้วยวิธี CakePHP เพื่อรับบทความที่มีแท็ก "test" และแท็ก "new"

คุณมีความคิดบ้างไหม?


person nexequ    schedule 11.03.2017    source แหล่งที่มา
comment
ดูเหมือนคุณกำลังมองหา stackoverflow.com/questions/30938440/   -  person ndm    schedule 13.03.2017
comment
ดู สิ่งนี้ โดยใช้ find(tagged) และ friendsofcake/search Plugin :)   -  person mark    schedule 19.07.2018


คำตอบ (1)


นี่คือวิธีแก้ปัญหาสำหรับคำถามของคุณ : อัปเดตการค้นหาของคุณดังต่อไปนี้ -

public function findTagged(Query $query, array $options){
     $this->opt = $options['tags'];
     $articles = $this->find()
        ->select(['Articles.id', 'Articles.url', 'Articles.title', 'Articles.description'])
        ->matching('Tags', function ($query) {
            return $query->where(['Tags.title IN' =>$this->opt]);
        })->group(['Articles.id'])
        ->having([
            $this->Users->query()->newExpr('COUNT(DISTINCT Tags.title) = '.count($this->opt))
        ]);

    return $articles;
}

ข้อความค้นหาข้างต้นจะส่งกลับเฉพาะ matching tags สำหรับบทความ

person Sumon Sarker    schedule 16.03.2017