Ho un'applicazione Web esistente che sto convertendo per utilizzare CakePHP. Il problema è che le chiavi primarie per la maggior parte delle tabelle sono in questo formato "$ {nome_tabella} _id" (story_id) invece del modo CakePHP di 'id'Cakephp Salva con una tabella in cui la chiave primaria non è 'id'
Ogni volta che provo ad aggiornare alcuni campi per una riga nella tabella della storia, la funzione Save() restituirà false. C'è un modo per ottenere un rapporto di errore più dettagliato dalla funzione Salva(). ?
Quando ho impostato Configure::write('debug', 2);
in core.php
e controllo le istruzioni SQL, non vedo alcun comando UPDATE, solo istruzioni SELECT.
Ho provato a modificare il controller aggiungendo la seguente riga per impostare manualmente il campo id per il controller ma non è stato d'aiuto.
$this->Story->id = $this->data['Story']['story_id'] ;
Sono a corto di idee. Eventuali suggerimenti?
Ho incluso il codice sorgente che sto usando qui sotto controllo
Story: modello
function admin_edit($id = null)
{
if (!$id && empty($this->data)) {
$this->Session->setFlash(__('Invalid '. Configure::read('Site.media') , true));
$this->redirect(array('action'=>'index'));
}
$this->layout = 'admin';
if (!empty($this->data)) {
if ($this->Story->save($this->data)) {
$this->Session->setFlash(__('The '. Configure::read('Site.media') .' has been saved', true));
} else {
$this->Session->setFlash(__('The '. Configure::read('Site.media') .' could not be saved. Please, try again.', true));
}
}
$this->data = $this->Story->read(null, $id);
}
Story: vista
class Story extends AppModel {
var $name = 'Story';
var $primaryKey = 'story_id';
var $validate = array(
'author_id' => array('numeric'),
'title' => array('notempty'),
'story' => array('notempty'),
'genra' => array('notempty'),
'form' => array('notempty'),
'wordcount' => array('Please enter a number between 1 and 1000' => array(
'rule' => array('range', 1, 1001),
'message' => 'Please enter a number between 1 and 1000'),
'Required' => array('rule' => 'numeric', 'required' => true)
)
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
var $belongsTo = array(
'Author' => array(
'className' => 'Author',
'foreignKey' => 'author_id'
)
);
var $hasMany = array(
'UserNote' => array(
'className' => 'UserNote',
'foreignKey' => 'story_id',
'dependent' => false,
'conditions' => 'UserNote.notes != ""'
)
);
}
Story:
echo $form->create('Story', array('action' => 'edit'));
echo $form->input('story_id',array('type'=>'hidden'));
echo $form->input('title');
echo $form->input('story');
echo $form->input('bio');
echo $form->end('Update story details');?>
tavolo Story
CREATE TABLE IF NOT EXISTS `stories` (
`story_id` int(11) NOT NULL AUTO_INCREMENT,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`closed` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`author_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`story` text NOT NULL,
`genra` varchar(255) NOT NULL,
`form` varchar(128) DEFAULT NULL,
`wordcount` varchar(255) NOT NULL,
`terms` varchar(255) NOT NULL DEFAULT '0',
`status` varchar(255) NOT NULL DEFAULT 'slush',
`published` date NOT NULL,
`payment` varchar(255) NOT NULL DEFAULT 'none',
`paypal_address` varchar(255) NOT NULL,
`resubmission` tinyint(1) NOT NULL DEFAULT '0',
`bio` text NOT NULL,
`password` varchar(255) NOT NULL DEFAULT 'yyggrrdd',
`comments` text NOT NULL,
PRIMARY KEY (`story_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10905 ;
Come si fa quando la chiave primaria è diversa su una tabella di join? –
@fewpeople danno un esempio? –
funziona benissimo, anche se questo è l'approccio giusto? Questo potrebbe interferire con alcune funzionalità di AutoMagick di CakePHP? –