Skip to main content
Question was a two part-er.
Source Link
clockworkgeek
  • 1.6k
  • 12
  • 19

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();
}

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();
}
added 622 characters in body
Source Link
clockworkgeek
  • 1.6k
  • 12
  • 19

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();
}

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();
}

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();
}
Source Link
clockworkgeek
  • 1.6k
  • 12
  • 19

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();
}