0
I’m having trouble adding a song that contains on the album, I want to make sure that when I add a song, it’s already related to the album id.
My problem is in the add method() below:
Musicascontroller:
public function adicionarAction() {
$forms = new Application_Form_FormMusicas();
$forms->submit->setLabel('Adicionar');
$this->view->form = $forms;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($forms->isValid($formData)) {
$idmusica = $forms->getValue('idmusica');
$musica = $forms->getValue('musica');
$duracao = $forms->getValue('duracao');
$musicas = new Application_Model_DbTable_Musicas();
$musicas->adicionarMusica($musica, $duracao, $idmusica);
$this->_helper->redirector('index', 'idmusica');
} else {
$forms->populate($formData);
}
}
}
Here’s some other information that might be helpful in helping:
Database:
Tables:
--Albums--
id INT PRIMARY KEY
Artist varchar(40)
title varchar(40)
--musicals--
id INT PRIMARY KEY
musica VARCHAR(40)
duration varchar(40)
idmusica int FOREIGN KEY from Albums(id)
Application_Model_DbTable_Musicas:
protected $dbt;
protected $_name = 'musicas';
protected $_primary = 'id';
protected $_referenceMap = array(
'RelacionamentoTabelas' => array(
'columns' => 'idmusica',
'refTableClass' => 'Application_Model_DbTable_Albums',
'refColumns' => 'id'
),
);
public function adicionarMusica($musica, $duracao, $idalbum) {
//$novoid = new Application_Model_DbTable_Albums();
//$id = (int) $novoid->setValue('id');
$data = array(
'musica' => $musica,
'duracao' => $duracao,
'idmusica' => $idalbum
);
$this->insert($data);
}
Application_Model_DbTable_Albums
protected $_name = 'albums';
protected $_primary = 'id';
protected $_dependentTables = array('Application_Model_DbTable_Musicas');
Call in view to add songs straight from album id:
ps.: check the 'id' where I get the album id and step right over to be armed with the foreign key, but I think it’s wrong.
index.phtml:
<td><a href="<?php echo $this->url(array('controller'=>'musicas',
'action'=>'adicionar', 'id' => $album->id));?>">Adicionar música</a></td>
All other methods are working except this one!
From what I’ve seen, you’re passing the album id (idmusica) by the url (GET) but you’re picking up the value by the
$idmusica = $forms->getValue('idmusica');
. You have an Hidden field in the form for the album id?– Lucas
You say add a Hidden Album id field in the music form or album form? I added it in album form like this:
$id = new Zend_Form_Element_Hidden('id');
 $id->addFilter('Int');
E in the song form I put your id and your foreign key as Hidden too:$id = new Zend_Form_Element_Hidden('id');
 $id->addFilter('Int');

 $idalbum = new Zend_Form_Element_Hidden('idalbum');
 $id->addFilter('Int');
The problem is precisely to add idalbum to addirAction. I don’t have to call a Function to fetch the album id?– Jorge2014
Hey, now that I realized that the name in the field Hidden was wrong (idalbum for idmusica), but look it’s still giving problem, do you think my foreign key should be NULL? Mine’s like NOT NULL. Hugs.
– Jorge2014
What is being saved in the idmusica field? I think the problem is that the album id is not coming filled in the form. Check this or correct me if I’m wrong.
– Lucas
An int is being saved which is the same as the table id field of the songs. Do you say, for example, do a Hidden in the album id in the Album or Music form? If that’s what I did, in the Album form here’s the code:
$id = new Zend_Form_Element_Hidden('id');
 $id->addFilter('Int');
And gave addElements too$this->addElements(array($id, $artist, $title, $submit));
. What I think is wrong is this index.phtml when I call additionAction in the music controller, when I do $album->id, does it already automatically connect to the idalbum foreign key? I keep thinking about it. Hugs.– Jorge2014
Did you find a solution? Poste as an answer to help other people.
– Maniero