CakePHP: How To Use ‘neighbors’ With ‘find’ Method

CakePHP: How To Use ‘neighbors’ With ‘find’ Method
Social sharing

CakePHP ‘find’ method makes it easier to retrieve data from database. The ‘find’ method can be used to construct anything from extremely simple queries to more complex ones without writing much code. This method can handle most SQL type requests and can be extended for more specific SQL queries.  I will walk you through the below example about the basics of working with the ‘find’  method

Contents

Here Are Some Inbuilt Types in CakePHP

  1. $this->Model->find(‘all’,$condition);
  2. $this->Model->find(‘first’,$condition);
  3. $this->Model->find(‘count’,$condition);
  4. $this->Model->find(‘list’,$condition);
  5. $this->Model->find(‘neighbors’,$condition);
  6. $this->Model->find(‘threaded’,$condition);

First four types are the most commonly used in CakePHP
Now, let’s take a look at an example of ‘neighbors’ type

Example

Let’s assume QuizQuestion is your model and you want to fetch the previous and next entries

Your Controller/action will look like,

public function getNeighbors($id){
 
$this->QuizQuestion->id = $id;
 
$neighbors = $this->QuizQuestion->find('neighbors',array('fields'=>array('id','question_no','description')));
}

A couple of queries will be generated in SQL as,

Query: SELECT 'QuizQuestion'.'id', 'QuizQuestion'.'question_no', 'QuizQuestion'.'description',
'QuizQuestion'.'id' FROM 'quiz_questions' WHERE 'QuizQuestion'.'id' < 38   ORDER BY
'QuizQuestion'.'id' DESC  LIMIT 1
Query: SELECT 'QuizQuestion'.'id', 'QuizQuestion'.'question_no', 'QuizQuestion'.'description',
'QuizQuestion'.'id' FROM 'quiz_questions' WHERE 'QuizQuestion'.'id' > 38   ORDER BY
'QuizQuestion'.'id' ASC  LIMIT 1

Here’s the output

Array
(
[prev] => Array
(
[QuizQuestion] => Array
(
[id] => 37
[question_no] => 1
[description] => Mathematics
)
 
)
[next] => Array
(
[QuizQuestion] => Array
(
[id] => 39
[question_no] => 3
[description] => Mathematics
)
 
)
 
)

Voila! Using the result keys ‘prev’ and ‘next’ you can view the results the way you want.

Summary
CakePHP: How To Use ‘neighbors’ With ‘find’ Method
Article Name
CakePHP: How To Use ‘neighbors’ With ‘find’ Method
Description
CakePHP ‘find’ method makes it easier to retrieve data from database. The ‘find’ method can be used to construct anything from extremely simple queries to more complex ones without writing much code.
Author
Publisher Name
Andolasoft
Publisher Logo

Your recently viewed posts:

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box