Вопрос

I developing a tool were you can get 10 records from a big table, based on the input in a form. For example, if the table has a column with the record "number", you can input one number and the app will return the 10 following records to that record with the number.

The porpuse of this is to get always the lastest 10 inputs from the last fetch.

The problem is that, AFAIK, there's no way to make a find with the condition of starting the search at an specific position.

Find neighbors is quite close, but unfortunately it only returns two results: the previous and the next one to the search.

Any ideas?


Edit: Right now the query looks like this:

 $data = $this->Tweet->find('all', array(
   'field' => array('Tweet.permalink' => $param2),
   'fields' => array('Tweet.embed'),
   'limit' => 10,
   'order' => array('Tweet.created' => 'desc'),
   'contain' => false,
   // 'offset' => 1,
   'conditions' => array('not' => array('Tweet.embed' => null),
    'Tweet.status' => 'approved',
    ),
 ));

Doesn't work the way i would like. Also if I don't comment the first line ("field") it just return one result. BTW, $param2 is the input of the user


Edit edit: The dirty solution.

I used Nunser approach. I didn't fully understand the findNeighbors function but after some debugging the function ended up like this:

protected function _findNeighbors($state, $query, $results = array()) {
    extract($query);

    if ($state === 'before') {
        $conditions = (array)$conditions;
        if (isset($field) && isset($value)) {
            if (strpos($field, '.') === false) {
                $field = $this->alias . '.' . $field;
            }
        } else {
            $field = $this->alias . '.' . $this->primaryKey;
            $value = $this->id;
        }

        $query['conditions'] = array_merge($conditions, array($field . ' <' => $value));
        $query['order'] = $field . ' DESC';
        $query['limit'] = 10;
        $query['field'] = $field;
        $query['value'] = $value;

        return $query;
    }



    return $results;
}

See the $query['limit'] = 10; field? Originally it was limited to one result, so i added a 0 and chopped the following code, which builds the prev and next array. I don't know how it gets the 10 results, how the SQL query is built, but it works!

It one has one bug: if you make the query and there isn't 10 results ahead of the item that you search for, it will return random results.

Это было полезно?

Решение

It must be because it's late, but I didn't really understand what you are trying to do.

But anyway, since you said find('neighbors') is "quite close", I'll give you an option to custom change that to fit your needs. That been said, if someone else is less sleep deprived than me and offers you a more efficient solution, take it.

Anyway, the solution: build a custom find.
Cake's docs explains how to create one. So do that, a find('lots-of-neighbors') find. Creating a new custom find seems pretty straightforward, so not going to explain that, you should ask a new question if you don't understand something there.

Now, what to actually put in the custom find function? Well, copy the find('neighbors') function and change what you need. I'll give you the link for the code, though I don't have your exact cakephp version, so you should double check.
From what I understood, the only problem you have with that function is that it just return 2 records, so you'll probably have to only change the limits inside the conditions (line 3143, 3156, 3160).

That's it in a nutshell. If it isn't what you want, I'll give it another go tomorrow morning.

Другие советы

Its not a convenience method but might work for you-

$find_id = $this->Tweet->find('first', array(
                                        'conditions' => array(
                                                'Tweet.permalink' => $param2
                                            )
                                        )
                                    );

Now I have the id of that record... Thus id is auto_increment, now I can use >= or <=

$tweets = $this->Tweet->find('all', array(
                                        'conditions' => array(
                                            'Tweet.id >=' =>$find_id['Tweet']['id'] 
                                        ),
                                        'limit' => 10
                                    )
                                );
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top