Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6013865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:36:37+00:00 2026-05-23T02:36:37+00:00

I’ve been wrestling with this code for a while, trying to clean it up

  • 0

I’ve been wrestling with this code for a while, trying to clean it up but it just doesn’t feel right. I have a Model, called Library. Libraries can own books, and the Library Model class has a public books property which is simply an array of Books. The model might look something like this:

Application_Model_Library {
    public $books = null;

    protected $_id;
    protected $_name;

    /* Getters, setters, etc. */
}

I also have a Book model. Users can have access to Books which is defined by a many-to-many table, users_books_permissions.

The library model stores the number of books the current user has access to, which is found using a JOIN on the books table and COUNT(*). Obviously, the library table itself doesn’t store this information, so it is a calculated field.

protected $_accessibleBookCount;

All of my models are contained in one file, with (static) mappers built it. For example:

// In the Application_Model_Library class
public static function fetchAll(){
    $db = Zend_Registry::get("db");
    /* etc */
}

However, my pagination plugin needs access to the raw select statement so it can add the LIMIT clause. This is also useful for sorting and filtering. To handle this, fetching a list of books actually requires two calls. The first one creates and returns a select object. This is modified in my controller based on the parameters to add sorting, filtering, and paginating. Then, another method in my Model takes the select object and performs the actual fetching and initialing of objects.

The first method is also responsible for injecting the JOIN that populates the $_accessibleBookCount field.

Fetching a single Library involves getting the select statement from the aforementioned function, adding a WHERE clause so only the library I want is selected, and then feeding this into the method to actually instantiate the Library model object.

Once I have a library object, fetching its books is handled by a method in the Library model called getBooks. If $books is null, then the method populates it. Then, the array is returned. Access to individual books can be achieved because the $books property is public. Upon fetching, the method also uses a JOIN to determine whether the user has access to the book.

However, what if I need to fetch a list of all libraries AND list each library’s books at the same time? It’s two method calls to get the list of libraries, but then getBooks must be called on each Library. This results in A LOT of repeated queries.

Whew. I want to thank anyone who was able to hold on long enough to get to the end of my post. I’d also like to thank anyone ahead of time for their help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:36:38+00:00Added an answer on May 23, 2026 at 2:36 am

    The only solution I see to fetch all books and libraries in one query is to actually fetch for all books and join with the libraries table. This might or might not be a better solution depending on what you are doing with libraries and books in your program.

    Something like this (in Books model):

    $select = $this->select();
    $select->setIntegrityCheck(false)
           ->from(array("b" => "books"))
           ->join(array("l" => "libraries"),"b.lid = l.id",array("libraryName" => "l.name")) // lid = Library ID in Books table
           ->order("l.name ASC"); // Order by Library Name
    

    You could then do some PHP magic to sort out Libraries and Books (create new objects,arrays, or whatever you want), for instance:

    $libraries = array(); 
    foreach($books as $book) {
      $libraries[$book->libraryName]["books"][] = $book; 
    }
    

    You have to see if it’s worth the trouble and is faster/more efficient than SQL queries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I need to clean up various Word 'smart' characters in user input, including but
I'm looking for suggestions for debugging... If you view this site in Firefox or
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.