0

I have two two entities news and authors, and I also can to create news and authors. So when I create my news I want to display authors-list in my form "select" enter image description here

After that I need to display it in my grid enter image description here

The problem is that news are stored in table news but authors in table authors Can somebody help mith it?

code

/**
 * Create table authors
 *
 */
$installer->getConnection()->dropTable($installer->getTable('tasknews/authors'));
$table = $installer->getConnection()
    ->newTable($installer->getTable('tasknews/authors'))
    ->addColumn('authors_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
        'identity'  => true,
        'nullable'  => false,
        'primary'   => true,
    ), 'Authors ID')
    ->addColumn('authors', Varien_Db_Ddl_Table::TYPE_TEXT, '255', array(
        'nullable'  => false,
    ))
    ->addColumn('created', Varien_Db_Ddl_Table::TYPE_DATE, null, array(
        'nullable'  => false,
        'format' => 'yyyy-MM-dd',
    ));
$installer->getConnection()->createTable($table);


/**
 * Create table news
 *
 */
$installer->getConnection()->dropTable($installer->getTable('tasknews/news'));
$table = $installer->getConnection()
    ->newTable($installer->getTable('tasknews/news'))
    ->addColumn('news_id', Varien_Db_Ddl_Table::TYPE_INTEGER, null, array(
        'identity'  => true,
        'nullable'  => false,
        'primary'   => true,
        ))
//    ->addColumn('authors_id', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
//        'nullable'  => false,
//    ), 'Authors ID')
    ->addColumn('title', Varien_Db_Ddl_Table::TYPE_TEXT, '255', array(
        'nullable'  => false,
        ))
    ->addColumn('description', Varien_Db_Ddl_Table::TYPE_TEXT, null, array(
        'nullable'  => false,
        ))
    ->addColumn('created', Varien_Db_Ddl_Table::TYPE_DATE, null, array(
        'nullable'  => false,
        'format' => 'yyyy-MM-dd',
    ))
    ->addColumn('is_active', Varien_Db_Ddl_Table::TYPE_SMALLINT, null, array(
    'nullable'  => false,
    'default'   => '1',
    ), 'Is Block Active');
//    ->addForeignKey($installer->getFkName('tasknews/news', 'authors_id', 'tasknews/authors', 'authors_id'),
//        'authors_id', $installer->getTable('tasknews/authors'), 'authors_id',
//        Varien_Db_Ddl_Table::ACTION_CASCADE, Varien_Db_Ddl_Table::ACTION_CASCADE);
$installer->getConnection()->createTable($table);
1
  • Do you have the table definition? Could I see it? Commented Oct 31, 2014 at 12:35

1 Answer 1

2

First, you article table must have a foreign key to the authors table.
Let's call it author_id. I see you commented that in your table creation script.
In the article add/edit form you need this field for the author select

$authors = Mage::getModel('[module]/author')->getCollection();
$options = array();
foreach ($authors as $author) {
    $options[$author->getId()] = $author->getName(); //replace getName with a method that returns the author name.
}

$fieldset->addField('author_id', 'select', array(
    'label'     => Mage::helper('[module]')->__('Author'),
    'name'      => 'author_id',
    'required'  => true, //or false
    'options'   => $options,
));

And in your grid you can add the column like this

$authors = Mage::getModel('[module]/author')->getCollection();
$options = array();
foreach ($authors as $author) {
    $options[$author->getId()] = $author->getName(); //replace getName with a method that returns the author name.
}
$this->addColumn('author_id', array(
    'header'    => Mage::helper('[module]')->__('Author'),
    'index'     => 'author_id',
    'type'      => 'options',
    'options'   => $options,
));

In order to not duplicate the code that retrieves the authors you can wrap that in a method an use it.

6
  • When I use news.authors_id like FK to authors.authors_id I can`t create my news. I wrote my sql above
    – Guest
    Commented Oct 31, 2014 at 12:58
  • Plese help me with authors model
    – Guest
    Commented Oct 31, 2014 at 13:35
  • What to you mean you can't create news if you add the FK?
    – Marius
    Commented Oct 31, 2014 at 13:38
  • If i use foreign key (authors_id) in table news to table authors (authors_id) like I wrote above. I have an error(SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (magento.news, CONSTRAINT FK_NEWS_AUTHORS_ID_AUTHORS_AUTHORS_ID FOREIGN KEY (authors_id) REFERENCES authors (authors_id) ON DELETE CASCADE ON UPDATE CASCADE))
    – Guest
    Commented Oct 31, 2014 at 13:44
  • This problem is solved if first create author and next create news
    – Guest
    Commented Oct 31, 2014 at 13:48

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.