Tengo una aplicación web existente que estoy convirtiendo para utilizar CakePHP. El problema es que las claves principales para la mayoría de las tablas están en este formato "$ {table_name} _id" (story_id) en lugar de la forma de CakePHP de 'id'Cakephp Guardar con una tabla donde la clave principal no es 'id'
Cuando intento actualizar algunos de los campos para una fila en la tabla de historias, la función Guardar() devolverá falso. ¿Hay alguna forma de obtener un informe de error más detallado de la función Guardar()? ?
Cuando establezco Configure::write('debug', 2);
en core.php
y verifico las sentencias de SQL, no veo ningún comando ACTUALIZAR, solo las instrucciones SELECT.
Traté de editar el controlador agregando la siguiente línea para establecer manualmente el campo de identificación para el controlador, pero no ayudó.
$this->Story->id = $this->data['Story']['story_id'] ;
Me estoy quedando sin ideas. ¿Alguna sugerencia?
he incluido el código fuente que estoy usando a continuación el controlador
historia: Modelo
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);
}
historia: 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 != ""'
)
);
}
Historia:
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');?>
mesa historia
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 ;
¿Cómo se hace esto cuando la clave principal es diferente en una tabla de unión? –
@fewpeople dan un ejemplo? –
esto funciona muy bien, ¿aunque este es el enfoque correcto? ¿Podría esto interferir con alguna funcionalidad AutoMagick de CakePHP? –