Bonjour,
J'ai suivi le tuto sur les taxonomy, mais dans la partie admin pour l'enregistrement j'ai une erreur du coup les informations ne s'enregistre pas dans la base de donnée, j'ai fais le point dessus mais je ne vois pas d'ou cela provient
Code du PostsController.php
App::uses('AppController', 'Controller');
class PostsController extends AppController {
public $helpers = array('Text','Form','Html', 'Session', 'Date');
public $components = array('RequestHandler','Paginator');
/** public $cacheAction= array(
'index'=>'2 DAY',
'show'=>'2 DAY'
);
**/
public $uses = array('Post');
function menu(){
$posts = $this->Post->find('all',array(
'conditions' => array('type' =>'post', 'online'=>1),
'fields' =>array('id','slug','name')
));
return $pages;
}
/**
* permet d'afficher la home des news
**/
function index(){
$d['posts'] = $this->Paginate('Post',array('type'=>'post', 'online'=>1,'created<= NOW()'));
$this->set($d);
}
function show($id = null, $slug = null){
if(!$id)
throw new NotFoundException('Aucun article ne correspond a cet ID');
$this->Post->contain('Term');
$post = $this->Post->find('first',array(
'conditions' => array('Post.id' => $id),
'recursive' => 0
));
if(empty($post))
throw new NotFoundException('Aucun article ne correspond a cet ID');
if($slug != $post['Post']['slug'])
$this->redirect($post['Post']['link'],301);
$d['taxonomy'] = $post['Taxonomy'];
$d['post'] = $post;
$this->set($d);
}
function feed(){
if( $this->RequestHandler->isRss() ){
$d['posts'] = $this->Post->find('all', array('limit' => 20,
'conditions'=>array('type'=>'post')
));
return $this->set($d);
}
}
/**
* parie avec les prefix
**/
function admin_index(){
$this->paginate = array('Post'=> array(
'limit'=>20
));
$d['posts'] = $this->Paginate('Post',array('type'=>'post','online'>=0));
$this->set($d);
}
function admin_edit($id = null){
if($this->request->is('put') || $this->request->is('post')){
$this->Post->set($this->request->data);
//debug($this->request->data);die();
if($this->Post->save()){
$this->Session->setFlash("Le contenu a bien été modifié","notif");
$this->redirect(array('action'=>'index'));
}
}
if($id){
$this->Post->id = $id;
$this->request->data = $this->Post->read();
}else{
$this->request->data = $this->Post->getDraft('post');
}
$d['terms'] = $this->Post->getFixedTerms();
$this->set($d);
}
function admin_delete($id){
$this->Session->setFlash("L'article a bien été supprimer",'notif');
$this->Post->delete($id);
$this->redirect($this->referer());
}
}
Le TermsController.php
class TermsController extends Appcontroller{
function admin_delete($id){
$this->Term->TermR->delete($id);
die();
}
function admin_add($ref,$ref_id){
$type=$_GET['type'];
if(isset($_GET['name'])){
$d = array(
'name'=>$_GET['name'],
'type'=>$type
);
$terms = $this->Term->find('first',array(
'conditions' =>$d,
'fields'=> 'id'
));
if(empty($term)){
$this->Term->save($d);
$term_id = $this->Term->id;
}else{
$term_id = $term['Term']['id'];
}
}
$d = array(
'ref' => $ref,
'ref_id' => $ref_id,
'term_id' =>$term_id
);
$count = $this->Term->TermR->find('count',array(
'conditions' =>$d
));
if($count == 0){
$this->Term->TermR->save($d);
$url = Router::url(array('action'=>'delete',$this->Term->TermR->id));
die('<span class="label">'.$_GET['name'].' <a href="'.$url.'" class="delTaxo">x</a></span>');
}
}
}
?>
TaxonomyBehavior.php
<?php
class TaxonomyBehavior extends ModelBehavior{
protected $_defaults = array(
'fixed' => array('category'),
'dynamic' => array('tag','ingredient')
);
public function getFixedTerms(Model $model){
return $model->Term->find('list',array(
'fields' =>array('Term.id','Term.name','Term.type'),
'conditions' => array('Term.type'=>$this->settings['fixed'])
));
}
public function setup(Model $model,$options = array()){
$model->hasAndBelongsToMany['Term'] = array(
'className' => 'Taxonomy.Term',
'associationForeignKey' => 'term_id',
'with' => 'Taxonomy.TermR',
'foreignKey' => 'ref_id',
'joinTable' => 'term_relationships',
'conditions' => 'ref = "Post"'
);
$this->settings = array_merge($this->_defaults,$options);
}
public function afterSave(Model $model,$created){
if(isset($model->data[$model->name]['terms'])){
$model->deleteTerms();
$terms = $model->data[$model->name]['terms'];
foreach($terms as $term_id){
$model->Term->termR->create();
$model->Term->TermR->save(array(
'term_id' => $term_id,
'ref' => $model->name,
'ref_id' => $model->id
));
}
}
}
public function afterFind(Model $model,$data,$primary){
foreach($data as $k=>$v){
if(!empty($v['Term'])){
$data[$k][$model->name]['terms'] = Set::Combine($v['Term'],'{n}.id','{n}.id');
$data[$k]['Taxonomy'] = Set::Combine($v['Term'],'{n}.id','{n}','{n}.type');
}
}
return $data;
}
public function deteteTerms(Model $model){
$terms = $model->Term->find('list',array(
'fields' => array('id','id'),
'condition' => array('Term.type' => $this->settings['fixed'])
));
$model->Term->TermR->deleteAll(array(
'ref' => $model->name,
'ref_id' => $model->id,
'term_id' => $terms
));
}
}
?>
Lors de la validation j'ai le message suivant
Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 Erreur de syntaxe près de 'deleteTerms' à la ligne 1
SQL Query: deleteTerms
Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
Stack Trace
CORE\Cake\Model\Datasource\DboSource.php line 460 → PDOStatement->execute(array)
CORE\Cake\Model\Datasource\DboSource.php line 426 → DboSource->_execute(string, array)
CORE\Cake\Model\Datasource\DboSource.php line 669 → DboSource->execute(string, array, array)
CORE\Cake\Model\Datasource\DboSource.php line 612 → DboSource->fetchAll(string, array, array)
CORE\Cake\Model\Model.php line 800 → DboSource->query(string, array, Post)
APP\Plugin\Taxonomy\Model\Behavior\TaxonomyBehavior.php line 32 → Model->__call(string, array)
APP\Plugin\Taxonomy\Model\Behavior\TaxonomyBehavior.php line 32 → Post->deleteTerms()
[internal function] → TaxonomyBehavior->afterSave(Post, boolean, array)
CORE\Cake\Utility\ObjectCollection.php line 132 → call_user_func_array(array, array)
[internal function] → ObjectCollection->trigger(CakeEvent)
CORE\Cake\Event\CakeEventManager.php line 248 → call_user_func(array, CakeEvent)
CORE\Cake\Model\Model.php line 1772 → CakeEventManager->dispatch(CakeEvent)
APP\Controller\PostsController.php line 73 → Model->save()
[internal function] → PostsController->admin_edit(string)
CORE\Cake\Controller\Controller.php line 486 → ReflectionMethod->invokeArgs(PostsController, array)
CORE\Cake\Routing\Dispatcher.php line 187 → Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 162 → Dispatcher->_invoke(PostsController, CakeRequest, CakeResponse)
APP\webroot\index.php line 110 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Merci de votre aide