Bonjour,
Je suis actuellement débutant sur Cake et je m'aide beaucoup de toute ces ressources. Donc tout d'abord, merci à Grafikart et à la communauté pour les tutos et pour vos aides précieuses. J'ai cherché dans le forum et je n'ai pas trouvé de solution à mon problème.
Dans l’administration, je cherche à hiérarchiser mes pages et je souhaiterais que certaines pages aient une page parente.
Dans ma BDD, j'ai une table "pages" avec les champs :
id
name
slug
content
type
media_id
online
display_menu
parent_id
created
modfied
Je créé mes pages à l'aide de l'administration et dans ma BDD, le "type" devient "page". Cette partie fonctionne. Si je veux qu'une page devienne parente, je fais appel à la fonction admin_parent.
Le menu déroulant qui hiérarchise les pages fonctionne, mais je n'arrive pas à changer dans la BDD le "type""page" en "subpage" et à mettre une id dans "parent_id". Auriez-vous une petite idée ?
Voici le PagesContoller.php :
<?php
App::uses('AppController', 'Controller');
class PagesController extends AppController {
public $uses = array('Page');
/**
* ADMIN
**/
public function admin_index() {
$d'pages'] = $this->Page->find('all', array(
'conditions' => array('type' => array('page', 'subpage'), 'online >= 0'),
'order' => 'Page.id DESC'
));
$this->set($d);
}
public function admin_edit($id = null) {
$d'pagesList'] = $this->Page->find('list', array(
'conditions' => array('type' => 'page', 'parent_id' => null)
));
$this->helpers] = 'Media.Uploader';
if (!empty($this->request->data)) {
if (!empty($this->request->data'Page']'id'])) {
$this->request->data'Page']'type'] = 'page';
$this->Page->id = $this->request->data'Page']'id'];
if ($this->Page->save($this->request->data)) {
$this->Session->setFlash("La page a été ajoutée !","notif", array('type'=>'success'));
$this->redirect(array('action' => 'index'));
}
}
}
if ($id) {
$this->request->data = $this->Page->find('first', array(
'conditions' => array('Page.id' => $id)
));
} else {
$this->request->data = $this->Page->getDraft();
}
$this->loadModel('File');
$d'files'] = $this->File->find('all', array(
'conditions' => array('ref' => 'Page', 'ref_id' => $this->request->data'Page']'id'])
));
$this->set($d);
}
public function admin_parent($id) {
if (!empty($this->request->query)) {
$this->Page->id = $id;
$this->Page->saveField('parent_id', key($this->request->query));
$this->Page->saveField('type', 'subpage');
}
$page = $this->Page->find('first', array(
'conditions' => array('Page.id' => $id, 'Page.type' => 'subpage')
));
$d'parent_page'] = $this->Page->find('first', array(
'conditions' => array('Page.id' => $page'Page']'parent_id'], 'type' => 'page'),
'fields' => array('id', 'name')
));
$d'pagesList'] = $this->Page->find('list', array(
'conditions' => array('Page.type' => 'page', 'Page.parent_id' => null, 'Page.id !=' => $id)
));
$this->set($d);
}
}
Voici le admin_parent.ctp :
<?php echo $this->Form->create('Page', array('id' => 'parentForm')); ?>
<?php echo $this->Form->input('parent_id',array('label'=>"Page parente :", 'options' => $pagesList)); ?>
<div class="spacer30"></div>
<?php echo $this->Form->end('Valider'); ?>
admin.js :
$('.editCreatedShow').hide();
$('.editCreated').live('click', function(event){
elem = $(this).parent().find('.editCreatedShow');
if ( elem.hasClass( 'showing' ) ) {
$(this).removeClass('btn-warning').text("Modifier la date de création");
elem.removeClass('showing').fadeOut('fast');
} else {
$(this).addClass('btn-warning').text('Ne pas modifier');
elem.addClass('showing').fadeIn('fast');
}
return false;
});
$('#parentForm').submit(function(event){
form = $(this);
data = form.find('#PostParentId').val();
url = form.attr('action');
$.ajax({
data: data,
url: url
});
event.preventDefault();
});
Pour info, j'obtiens une erreur : Notice (8): Undefined index: Page [APP\Controller\PagesController.php, line 72]
qui correspond à
'conditions' => array('Page.type' => 'page', 'Page.parent_id' => null, 'Page.id !=' => $id)
je vous remercie par avance.