J'ai suivi le tutoriel sur le developpement d'un site avec cakephp mais je suis blocké sur la liason des articles et des categories:
voici le model Complaint.php
<?php
class Complaint extends AppModel{
public $recursive = -1;
public $belongsTo = array(
'Category'=> array(
'counterCache'=>true
)
);
public $displayField='name';
public $validate = array(
'slug'=> array(
'rule' =>'/^[a-z0-9\-]+$/',
'allowEmpty' => true,
'message' => 'Invalid url'
),
'name' => array(
'rule' => 'notEmpty',
'message' => 'Invalid title'
)
);
public $order = 'Complaint.created DESC';
public function afterFind($data, $primary = false){
foreach ($data as $k => $d) {
if (isset($d'Complaint']'slug']) && isset($d'Complaint']'id'])) {
$d'Complaint']'link'] = array(
'controller'=>'complaints',
'action'=>'view',
'id'=>$d'Complaint']'id'],
'slug'=>$d'Complaint']'slug']
);
}
$data$k] = $d;
}
return $data;
}
public function beforeSave($options = null){
if (empty($this->data'Complaint']'slug']) && isset($this->data'Complaint']'slug']) && !empty($this->data'Complaint']'name']))
$this->data'Complaint']'slug'] = strtolower(Inflector::slug($this->data'Complaint']'name'],'-'));
return true;
}
}
voici les codes de ComplaintsController.php
<?php
class ComplaintsController extends AppController{
public $uses = array('Complaint');
public $helpers = array('Date');
/**
*RequestAction, permet d'avoir la liste des contenus pour le menu
**/
function admin_index(){
$d'complaints'] = $this->Paginate('Complaint');
$this->set($d);
}
/**
*Permet d'afficher une categorie
**/
function category($category){
$this->loadModel('Category');
$cat = $this->Category->find('first');
if(empty($cat))
throw new NotFoundException("This category is not found");
$d'complaints'] = $this->Paginate('Complaint');
$this->set($d);
$this->render('index');
}
function admin_add($id = null)
{
//$this->helpers] = 'Media.Media';
if($this->request->is('put') || $this->request->is('post')){
if($this->Complaint->save($this->request->data)){
$this->Session->setFlash('The Complaint has been saved', 'notif');
$this->redirect(array('action'=>'index'));
}
}elseif($id){
$this->Complaint->id = $id;
$this->request->data = $this->Complaint->read();
}
//$d'categories'] =$this->Post->Category->find('list');
//$this->set($d);
$this->loadModel('Category');
$d'categories'] =$this->Category->find('list');
$this->set($d);
}
function admin_edit($id = null){
//$this->helpers] = 'Media.Media';
if($this->request->is('put') || $this->request->is('post')){
if($this->Complaint->save($this->request->data)){
$this->Session->setFlash('The Complaint has been updated', 'notif');
$this->redirect(array('action'=>'index'));
}
}elseif($id){
$this->Complaint->id = $id;
$this->request->data = $this->Complaint->read();
}
$this->loadModel('Category');
$d'categories'] = $this->Category->find('list');
$this->set($d);
}
function admin_delete($id){
$this->Session->setFlash('The post has been deleted', 'notif');
$this->Complaint->delete($id);
$this->redirect($this->referer());
}
}
ceux de Category.php:
<?php
class Category extends AppModel{
public $hasMany = array('Complaint');
public function afterFind($data, $primary = false){
foreach ($data as $k => $d) {
if (isset($d'Category']'slug']) && isset($d'Category']'id'])) {
$d'Category']'link'] = array(
'controller'=>'posts',
'action'=>'category',
'slug'=>$d'Category']'slug']
);
}
$data$k] = $d;
}
return $data;
}
public function beforeSave($options = null){
if (empty($this->data'Category']'slug']) && isset($this->data'Category']'slug']) && !empty($this->data'Category']'name']))
$this->data'Category']'slug'] = strtolower(Inflector::slug($this->data'Category']'name'],'-'));
return true;
}
}
et enfin ceux de la vue admin_add.php:
<h1>Add a post</h1>
<?php echo $this->form->create('Complaint'); ?>
<?php echo $this->form->input('name',array('label'=>'Title'));?>
<?php echo $this->form->input('slug',array('label'=>'Slug','type'=>'hidden'));?>
<?php echo $this->Form->input('category_id',array('label'=>'Category')); ?>
<?php //echo $this->Media->iframe('Post', $this->request->data'Post']'id']); ?>
<?php echo $this->form->input('message',array('message'=>'Title'));?>
<?php //echo $this->Media->tinymce('Post.content'); ?>
<?php echo $this->form->input('id');?>
<?php //echo $this->form->input('online',array('label'=>'Online','type'=>'checkbox'));?>
<?php echo $this->form->end('Save'); ?>
Aidez moi s'il vous plait car je dois remettre ce travail ce matin!!
Merci.
tu doit chercher category par son slug puis tu affiche les article lié a la category
function category($category){
$this->loadModel('Category');
$cat = $this->Category->findBySlug($category);
if(empty($cat))
throw new NotFoundException("This category is not found");
$d'complaints'] = $this->Paginate('Complaint',array('category_id' => $cat'Category']'id']));
$this->set($d);
$this->render('index');
}
Ré-indente ton code c'est dégelasse la :D
Sinon, il faudrait plus nous dire les difficultés que tu rencontres.