1

At the moment I have a page where I have retrieved information on a club by the id of that club. I now have a comments box where I want to retrieve the comments about that club, in the comments table I have the club_id and the parameter "club_id" is passed into this page. At the moment I am retrieving all of the comments from the table but I want just the comments for that club. A point in the right direction would be great!

Controller:

class ClubDescriptionController extends Zend_Controller_Action
{

public $auth = null;

public function init()
{
    $this->auth=Zend_Auth::getInstance();
}

http://pastebin.com/m66Sg26x
protected function authoriseUser()
{
    if (!$this->auth->hasIdentity()) {
            $route = array('controller'=>'auth', 'action'=>'index');
            $this->_helper->redirector->gotoRoute($route);

        }
    }
}

Model:

class Application_Model_DbTable_Comments extends Zend_Db_Table_Abstract
{

protected $_name = 'comments';

public function getComment($id) {
    $id = (int) $id;
    $row = $this->fetchRow('id = ' . $id);
    if (!$row) {
        throw new Exception("Count not find row $id");
    }
    return $row->toArray();
}

public function addComment($comment, $club_id) {
    $data = array(
        'comment' => $comment,
        'club_id' => $club_id,
        'comment_date' => new Zend_Db_Expr('NOW()'),
        );
    $this->insert($data);
    }   

    public function deleteComment($id) {
    $this->delete('id =' . (int) $id);
    }
}

The view:

<div id="view-comments">
        <?php foreach($this->comments as $comments) : ?>
            <p id="individual-comment">
                <?php echo $this->escape($comments->comment);?> - 
                <i><?php echo $this->escape($comments->comment_date);?></i>
            </p>
        <?php endforeach; ?>
</div>

I realise I am going to have to use the getComment(); function in my model and query it by the id but I'm getting confused on exactly how...

Thanks

1
  • Could you show the code in Application_Model_DbTable_Clubs? I think it will be relevant to your question. Commented Apr 24, 2012 at 19:22

3 Answers 3

0

It's been a while since I used Db_Table but I think you want to create a select object, which allows you to build a query that will select comments with the correct club_id:

$comments = new Application_Model_DbTable_Comments();
$select = $comments->select();
$select->where('club_id = ?', $id);

$this->view->comments = $comments->fetchAll($select);

you may want to order the comments by date, if so, you can do this by adding an order clause to the select:

$select->order('comment_date ASC');

take a look at the docs for Zend_Db_Table_Select, which has quite a few examples: http://framework.zend.com/manual/en/zend.db.table.html#zend.db.table.fetch-all

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, I was not aware you could do it like this! Thanks!
0

In your controller you are calling

$this->view->comments = $comments->fetchAll();

it should be

$this->view->comments = $comments->getComment($this->_request->getParam('club_id'));

where id variable will be fetched from url.

12 Comments

I thought that should of worked but I get the error -> pastebin.com/Dj00Kq8A Oh and the param is "club_id" to avoid confusion..
with the controller updated you will see I could change -> $this->view->comments = $comments->getComment($this->_request->getParam('club_id')); to $this->view->comments = $comments->getComment($id); Sorry for the lack of code.
try this $id = (int)$this->_request->getParam(’club_id’, 0);
make sure the row you are trying to access exist in the database. and i am seeing some flaw in your logic now. since club_id should be the foreign key in comments table, you should be fetching the row by foreign key not primary key. if this still doesn't help you. post your db table for comments and club.
change $row = $this->fetchRow('club_id = ' . $id); and check.
|
0

Here is the working controller:

public function indexAction() {
    //authorisation
    $this->authoriseUser();
    //to get the paramter club_id to query for specific club information
    $id = (int) $this->_request->getParam('club_id', 0);

    //submit a comment
    $form = new Application_Form_Comment();
    $form->submit->setLabel('Comment');
    $this->view->form = $form;
    if ($this->getRequest()->isPost()) {
        $formData = $this->getRequest()->getPost();
        if ($form->isValid($formData)) {
            $comment = new Application_Model_DbTable_Comments();
            $comment->addComment($formData['comment'], $id);
        } else {
            $form->populate($formData);
        }
    }

    //initialise table
    $clubs = new Application_Model_DbTable_Clubs();
    $clubs = $clubs->getClub($id);
    $this->view->clubs = $clubs;

    //to get the comments for the club
    $comments = new Application_Model_DbTable_Comments();
    $select = $comments->select();
    $select->where('club_id = ?', $id);
    $select->order('comment_date ASC');

    $this->view->comments = $comments->fetchAll($select);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.