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 9035007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:38:52+00:00 2026-06-16T08:38:52+00:00

Could you help to extend a little bit about the Zend Quickstart : In

  • 0

Could you help to extend a little bit about the Zend Quickstart: In the tutorial, we use the Mapper to update a single Guestbook. What if I want to update more than one Guestbook? And based on some conditions?

For example, I have an action to delete all Guestbooks that were created before 2012-12-21. What should I update to achieve that?

Does my approach make sense?

// application/models/GuestbookMapper.php 
class Application_Model_GuestbookMapper
{
    public function deleteByCreatedBefore($date)
    {
        $this->getDbTable()->deleteByCreatedBefore($date);
    }
}

// application/models/DbTable/Guestbook.php 
class Application_Model_DbTable_Guestbook extends Zend_Db_Table_Abstract
{
    public function deleteByCreatedBefore($date) {
        $where = $this->getAdapter()->quoteInto('created < ?', $date);
        $this->delete($where);
    }
}

Thanks,

  • 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-06-16T08:38:54+00:00Added an answer on June 16, 2026 at 8:38 am

    If you are using the quickstart model/mapper and want to stay true to that data mapper paradigm you wouldn’t have anything in your Application_Model_DbTable_Guestbook except for properties (‘name’, ‘primary’…). The DbTable model would exist as the database adapter for that single table.

    Your delete function would be placed in the mapper.

    class Application_Model_GuestbookMapper
    {
        public function deleteByCreatedBefore($date)
        {
            $where = $this->getDbTable()->quoteInto('created < ?', $date);
            //delete() returns num of rows deleted
            $this->getDbTable()->delete($where);
        }
    }
    

    This will work but may not be the best/safest way to achieve the required functionality.

    This particular example of the Data Mapper is very simple and might be somewhat misleading to some people. The Guestbook example of the Mapper is really not a good representation of the mapper as the database row and the domain model (Application_Model_Guestbook) map 1 to 1 (one database column to one model property).

    Where the Data Mapper starts to shine is when you need to map several database tables to a single Domain Model. With the understanding that your Domain Model (Application_Model_Guestbook) may have to effect more then one database table each time delete() is called, the structure for the delete() function is important.

    What should you do to accomplish a delete with the mapper?

    First: update Application_Model_GuestbookMapper::fetchAll() to accept a $where parameter, I usually setup this type of function to accept an array that sets the column and the value.

    //accepted parameters: Zend_Db_Table::fetchAll($where = null, $order = null, $count = null, $offset = null)
    //accepts array (column => value )
    public function fetchAll(array $where = null)
        {
            $select = $this->getDbTable()->select();
            if (!is_null($where) && is_array($where)) {
                //using a column that is not an index may effect database performance
                $select->where($where['column'] = ?, $where['value']);
            } 
            $resultSet = $this->getDbTable()->fetchAll($select);
            $entries   = array();
            foreach ($resultSet as $row) {
                $entry = new Application_Model_Guestbook();
                $entry->setId($row->id)
                      ->setEmail($row->email)
                      ->setComment($row->comment)
                      ->setCreated($row->created);
                $entries[] = $entry;
            }
            return $entries;
        }
    

    Second: Refactor your Application_Model_GuestbookMapper::deleteByCreatedBefore() to accept the output from fetchAll() (actually it would be simpler to just build a delete() function that accepts the output: array of Guestbook objects)

     //accepts an array of guestbook objects or a single guestbook object
     public function deleteGuestbook($guest)
        {
            if (is_array($guest) {
                foreach ($guest as $book) {
                    if ($book instanceof Application_Model_Guest){
                        $where = $this->getDbTable()->quoteInto('id = ?', $book->id);
                        $this->getDbTable()->delete($where);
                    }
                }
            } elseif ($guest instanceof Application_Model_Guest) {
                $where = $this->getDbTable()->quoteInto('id = ?', $guest->id);
                        $this->getDbTable()->delete($where);
            } else {
                throw new Exception;
            }
        }
    

    Deleting a domain object as an object will become more important as you have to consider how deleting an object will affect other objects or persistence (database) paradigms. You will at some point encounter a situation where you don’t want a delete to succeed if other objects still exist.

    This is only an opinion but I hope it helps.

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

Sidebar

Related Questions

hi I've could use a little help understanding the imageadapter extending the baseadapter. I'm
I wonder if you could help me with something I've been thinking about. Say
What could help me in helping writing highly compact(least byte code count) programs in
I was hoping someone could help me with the following script: jQuery(document).ready(function($) { $(.infoBoxBtn
I hope you could help me. I' m using QT and try to do
was hoping you could help me out. I am trying to check if a
I was hoping someone could help me with submitting the answers to an HTML
Wondering if someone could help me. I have next to no knowledge with Ajax,
Hope someone could help me with this: I would like to have a column
I wonder if someone could help me figure out how to parse a string

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.