3

Hi I have a castom module news. When I create news I save all my informations in table news. But I need to save author input to table Authors.enter image description here

And display all informations here enter image description here

This is my News Grid!!! Please Help Me

class Task_News_Block_Adminhtml_News_Grid extends Mage_Adminhtml_Block_Widget_Grid
{


    protected function _prepareCollection()
    {
        $collection = Mage::getModel('tasknews/news')->getCollection();
        $newsAuthorsTable = Mage::getSingleton('core/resource')->getTableName('tasknews/news');

        $collection->getSelect()->join(array('authors'=> $newsAuthorsTable),
           'main_table.news_id=authors.news_id',
           array());
        $this->setCollection($collection);
        return parent::_prepareCollection();
    }

    protected function _prepareColumns()
    {

        $helper = Mage::helper('tasknews');

        $this->addColumn('news_id', array(
            'header' => $helper->__('News ID'),
            'index' => 'news_id'
        ));

        $this->addColumn('title', array(
            'header' => $helper->__('Title'),
            'index' => 'title',
            'type' => 'text',
        ));

    $this->addColumn('author', array(
            'header' => $helper->__('Author'),
            'index' => 'author',
            'type' => 'text',
        ));


        $this->addColumn('created', array(
            'header' => $helper->__('Created'),
            'index' => 'created',
            'type' => 'date',
        ));

        return parent::_prepareColumns();
    }

 protected function _prepareMassaction()
    {
        $this->setMassactionIdField('news_id');
        $this->getMassactionBlock()->setFormFieldName('news');

        $this->getMassactionBlock()->addItem('delete', array(
            'label' => $this->__('Delete'),
            'url' => $this->getUrl('*/*/massDelete'),
        ));
        return $this;
    }

    public function getRowUrl($model)
    {
        return $this->getUrl('*/*/edit', array(
                    'id' => $model->getId(),
                ));
    }

1 Answer 1

4

You are so close you'll kick yourself. There needs to be one change to join()

protected function _prepareCollection()
{
    $collection = Mage::getModel('tasknews/news')->getCollection();
    $newsAuthorsTable = Mage::getSingleton('core/resource')->getTableName('tasknews/news');

    $collection->getSelect()->join(array('authors'=> $newsAuthorsTable),
       'main_table.news_id=authors.news_id',
// must also specify which columns to join
       array('author'));
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Although the above works great it could be neater since it's bad practice to have "model" logic in "block" classes. In the news collection class add:

public function joinAuthors()
{
    $this->join(
        array('authors' => 'tasknews/news'),
        'main_table.news_id=authors.news_id',
        array('author')
    );
    return $this;
}

And now the collection is much easier to use.

protected function _prepareCollection()
{
    $collection = Mage::getModel('tasknews/news')->getCollection()->joinAuthors();
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

To save the news and author at the same time I would do this; In the news model add:

protected function _afterSave()
{
    parent::_afterSave();

    if ($this->dataHasChangedFor('author')) {
        Mage::getModel('tasknews/author')
            ->load($this->getId(), 'news_id')
            ->setNewsId($this->getId()) // in case there wasn't a record before
            ->setAuthor($this->getAuthor())
            ->save();
    }

    return $this;
}

// necessary to fill orig data for dataHasChangedFor() to work above
// only called when loading a single object, for collections use joinAuthors()
protected function _afterLoad()
{
    $author = Mage::getModel('tasknews/author')
            ->load($this->getId(), 'news_id')
            ->getAuthor();
    $this->setData('author', $author); // not setAuthor() in case that's a real method
    $this->setOrigData('author', $author); // this value is compared when saving

    return parent::_afterLoad();
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.