Bonjour,
Voilà mon problème, je fais un site web, et là je suis sur l'administration et je rencontre un problème avec ma gestion des articles

Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Champ 'User.username' inconnu dans field list

SQL Query: SELECT `Post`.`id`, `Post`.`name`, `Post`.`slug`, `Post`.`created`, `Post`.`content`, `Category`.`slug`, `Category`.`name`, `User`.`username`, `Category`.`id` FROM `sitefin`.`posts` AS `Post` LEFT JOIN `sitefin`.`categories` AS `Category` ON (`Post`.`category_id` = `Category`.`id`) WHERE `type` = 'post' AND online >= 0 ORDER BY `Post`.`created` DESC, `Post`.`id` DESC LIMIT 5

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 461 ? PDOStatement->execute(array)
CORE\Cake\Model\Datasource\DboSource.php line 427 ? DboSource->_execute(string, array)
CORE\Cake\Model\Datasource\DboSource.php line 669 ? DboSource->execute(string, array, array)
CORE\Cake\Model\Datasource\DboSource.php line 1108 ? DboSource->fetchAll(string, boolean)
CORE\Cake\Model\Model.php line 3022 ? DboSource->read(Post, array)
CORE\Cake\Model\Model.php line 2994 ? Model->_readDataSource(string, array)
CORE\Cake\Controller\Component\PaginatorComponent.php line 196 ? Model->find(string, array)
CORE\Cake\Controller\Controller.php line 1086 ? PaginatorComponent->paginate(string, array, array)
APP\Controller\PostsController.php line 99 ? Controller->paginate(string, array)
[internal function] ? PostsController->admin_index()
CORE\Cake\Controller\Controller.php line 490 ? ReflectionMethod->invokeArgs(PostsController, array)
CORE\Cake\Routing\Dispatcher.php line 193 ? Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 167 ? Dispatcher->_invoke(PostsController, CakeRequest)
APP\webroot\index.php line 57 ? Dispatcher->dispatch(CakeRequest, CakeResponse)

Mais ce champ est déjà dans ma BDD donc je pense que ca viens de mon PostController.php (voir ci-dessous)

PostController.php :

<?php
class PostsController extends AppController{

    public $paginate = array(
        'fields'  => array('Post.id','Post.name','Post.slug','Post.created','Post.content','Category.slug','Category.name','User.username'),
        'contain' => array('Category','User'),
        'limit'   => 5,
        'paramType' => 'querystring'
    );
    public $helpers = array('Date','Markdown.Markdown');
    public $components = array('RequestHandler');

    function index(){
        $conditions = null;

        // Category filter
        if(isset($this->request->params['category'])){
            $conditions = array('Category.slug' => $this->request->params['category']);
            $this->request->params['named']['category'] = $this->request->params['category'];
        }
        if(isset($this->request->params['user'])){
            $conditions = array('Post.user_id' => $this->request->params['user']);
            $this->request->params['named']['user'] = $this->request->params['user'];
        }

        $d['posts'] = $this->Paginate('Post',array('type'=>'post','online'=>1,'created <= NOW()'));
        $this->set($d);
    }

    function view($id = null,$slug = null){
        $this->Post->contain('Category', 'User', 'Comment');
        //$post = $this->Post->findBySlug($slug, array('Post.name','Post.slug','Post.created','Post.content','Category.slug','Category.name','User.username'));
        if(!$id)
            throw new NotFoundException('Aucunes pages ne correspond à cet ID');
        $post = $this->Post->find('first',array(
            'conditions' => array('Post.id' => $id,'Post.name','Post.slug','Post.created','Post.content','Category.slug','Category.name','User.username'),
            'recursive' => 0
            ));
        if(empty($post))
            throw new NotFoundException('Aucunes pages ne correspond à cet ID');

        if($slug != $post['Post']['slug'])
            $this->redirect($post['Post']['link'],301);
        $d['post'] = $post;
        $this->set($d);

        // Comment submission
        if(!empty($this->request->data)){
            $this->Post->Comment->create($this->request->data, true);
            $this->request->data['Comment']['post_id'] = $post['Post']['id'];
            if($this->Post->Comment->save(null, true, array('mail', 'username', 'content', 'post_id'))){
                $this->Session->setFlash("Thanks for your comment","success");
                return $this->redirect($this->referer());
            }else{
                $this->Session->setFlash("You have to correct your errors first","error");
            }
        }
    }

    /**
    * Requested, get datas for sidebar
    **/
    public function sidebar(){
        $last_posts = $this->Post->find('all', array(
            'limit' => 2,
            'fields'=> array('Post.id','Post.slug','Post.name')
        ));
        $categories = $this->Post->Category->find('all');
        return compact('last_posts', 'categories');
    }

    function feed(){
    if ($this->RequestHandler->isRss() ) {
        $d['posts'] = $this->Post->find('all', array(
            'limit' => 20,
            'conditions' => array('type'=>'post')
            ));
        return $this->set($d);
        }
    } 

    function category($category){
        $cat = $this->Post->Category->find('first',array(
            'conditions' => array('slug' => $category)
        ));
        if(empty($cat))
            throw new NotFoundException('Aucunes catégorie ne correspond à cet ID');
        $d['posts'] = $this->Paginate('Post',array('type'=>'post','online >= 0','category_id' => $cat['Category']['id']));
        $this->set($d);
        $this->render('index');

    }

    /**
    * Admin panel
    **/
    function admin_index(){
        $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')){
            if($this->Post->save($this->request->data)){
                $this->Session->setFlash("L'article a bien été sauvegardé","notif");
                $this->redirect(array('action'=>'index'));
            }

        }elseif($id){
            $this->Post->id = $id;
            $this->request->data = $this->Post->read(); 
        }else{
            $this->request->data = $this->Post->getDraft('post');
        }
        $d['categories'] = $this->Post->Category->find('list');
        $this->set($d);
    }

    public function admin_delete($id){
        $this->Post->delete($id);
        $this->Session->setFlash('La page a bien été supprimée','notif');
        $this->_clean_cache();
        $this->redirect($this->referer());
    }

    private function _clean_cache(){
        if(file_exists(CACHE . 'cake_element__sidebar_cache_callbacks')){
            unlink(CACHE . 'cake_element__sidebar_cache_callbacks');
        }
    }

}

Merci d'avance !

4 réponses


flashios09
Réponse acceptée

visiblement le problème n'est pas dans PostsController mais dans Model/Post.php
il faut vérifier que la table posts est lié avec la table users (belongsTo)

Il faut lire la doc de cakephp avant
http://book.cakephp.org/2.0/fr/models/associations-linking-models-together.html#belongsto

Regarde la requête sql générée

SELECT `Post`.`id`, `Post`.`name`, `Post`.`slug`, `Post`.`created`, `Post`.`content`, `Category`.`slug`, `Category`.`name`, `User`.`username`, `Category`.`id`
FROM `sitefin`.`posts` AS `Post` 
LEFT JOIN `sitefin`.`categories` AS `Category` ON (`Post`.`category_id` = `Category`.`id`) 
WHERE `type` = 'post' AND online >= 0 
ORDER BY `Post`.`created` DESC, `Post`.`id` DESC LIMIT 5

Il manque la jointure avec la table users

LEFT JOIN `sitefin`.`users` AS `User` ON (`Post`.`user_id` = `User`.`id`) 

il faut voir la ligne 99 du PostsController

APP\Controller\PostsController.php line 99 ? Controller->paginate(string, array)

Merci et comment on joint une table à un controlleur ?

Merci beaucoup !