TIP: Use Markdown or, <pre> for multi line code blocks / <code> for inline code.
These forums are read-only and for archival purposes only!
Please join our new forums at discourse.kohanaframework.org
ORM query and error - Call to a member function offset() on a non-object
  • I am not really sure if i should do this as a DB query or ORM. Right now I have, not very elegant, but working piece.

    $db_query = "SELECT D.id,
           D.score,
           D.comment,
           B.id ,
           B.author,
            B.title
    FROM descriptions D
    INNER JOIN details P
        ON D.id = P.description_id
    INNER JOIN books B
        ON P.book_id = B.id
    WHERE P.user_id = $user";
            
            
            
    $query = Db::query(Database::SELECT, $db_query)
    
    ->execute()->as_array();
    

    Any thought how I could write it as an ORM query?

    ORM::factory('description')
    ->join('details', 'INNER')->on('details.description_id', '=', 'description.id')
    ->join('books', 'INNER')->on ('details.book_id', '=', 'books.id')
    ->where('details.user_id', '=', '1')
    ->find_all()
    ->as_array();

    But I cannot get anything from books, seems it is not loaded. I am trying to replace ORM with DB query, but I am having problem with pagination. I cannot set offset, because count_all() can't be applied.

    edit:

    It was really silly... I forgot to add ->select('*') to the query. So the query looks now:

    $databook = ORM::factory('description')
    ->select('*')
    ->join('details', 'INNER')
    ->on('description.id','=', 'details.description_id')
    ->join('books', 'INNER')
    ->on('books.id', '=', 'details.book_id')
    ->where('details.user_id', '=', $user->id)
    ->find_all()
    ->as_array();
    

    However, in this case I cannot use pagination

     $data = $orm -> offset($pagination -> offset) -> limit($pagination -> items_per_page) -> find_all();
    

    It shoots the error:

    Call to a member function offset() on a non-object
    

    Now I am totally lost.

  • You have to apply the offset before find_all(), so it can add it to the query correctly. So:

    $databook = ORM::factory('description')
    ->join('details', 'INNER')
    ->on('description.id','=', 'details.description_id')
    ->join('books', 'INNER')
    ->on('books.id', '=', 'details.book_id')
    ->where('details.user_id', '=', $user->id)
    ->offset($pagination->offset)
    ->limit($pagination->items_per_page)
    ->find_all()
    ->as_array();
    
  • That was it, thanks!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion