Bonjour,
je suis un débutant en cakePhp, j'ai suivi le tuto CakePHP 2 de grafikArt, je me suis tombé sur une erreur lors de la sauvegarde ou la suppression d'une article POST
qui a une relation avec User .
la sauvegarde s'effectue avec succès, mais ce message d'erreur apparaît :
Database Error
Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'created' in where clause is ambiguous
SQL Query: SELECT COUNT(*) AS count FROM posts AS Post LEFT JOIN users AS User ON (Post.user\_id = User.id) LEFT JOIN categories AS Category ON (Post.category\_id = Category.id) WHERE Post.online = 1 AND created <= NOW() AND Post.category\_id = 1 AND created <= NOW()
Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp
avant que je mets cette relation le système ça marche bien.
voici mon Post Model
<?php
class Post extends AppModel {
public $hasMany = array(
'Media' => array(
'dependent' => true
)
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Category' => array(
'counterCache' => array(
'post_count' => array('Post.online' => 1, 'created <= NOW()')
)
)
);
public $recursive = -1;
public $order = 'Post.created DESC';
public $validate = array(
'slug' => array(
'rule' => '/^[a-z0-9\-]+$/',
'allowEmpty' => true,
'message' => "L'URL n'est pas valide !"
),
'name' => array(
'rule' => 'notEmpty',
'message' => 'Vous devez précisez un titre !'
)
);
/*
* AfterFind
*/
function afterFind($data, $primary = false) {
foreach ($data as $k => $d) {
if (isset($d'Post']'slug']) && isset($d'Post']'id']) && isset($d'Post']'type'])) {
$d'Post']'link'] = array(
'controller' => Inflector::pluralize($d'Post']'type']),
'action' => 'show',
'id' => $d'Post']'id'],
'slug' => $d'Post']'slug']
);
}
$data$k] = $d;
}
return $data;
}
public function beforeSave($options = array()) {
if (empty($this->data'Post']'slug']) && isset($this->data'Post']'slug']) && !empty($this->data'Post']'name'])) {
$user_id = AuthComponent::user('id');
$this->data'Post']'user_id'] = $user_id;
$this->data'Post']'slug'] = strtolower(Inflector::slug($this->data'Post']'name'], '-'));
}
//($this->data);
//die();
return true;
}
}
- le PostController
<?php
class PostsController extends AppController {
public $uses = array('Post');
public $helpers = array('Date');
public $components = array('RequestHandler');
public $paginate = array(
'limit' => 5
);
/*
* Index
*/
function admin_index() {
$this->paginate = array('Post' => array('limit' => 10, 'recursive' => 0));
$d'posts'] = $this->Paginate('Post', array(
'type' => 'post',
'online >= 0',
));
debug($d);
$this->set($d);
}
/**
* Editer
*/
function admin_edit($id = null) {
if ($this->request->is('put') || $this->request->is('post')) {
$data = $this->request->data'Post'];
if ($this->Post->save($data)) {
$this->Session->setFlash('Le contenu a été bien modifié', '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);
}
/*
* Supprimer
*/
function admin_delete($id) {
$this->Session->setFlash('L\'article a été bien supprimée', 'notif');
$this->Post->delete($id);
$this->redirect($this->referer());
}
}
?>
- et voici la vue Admin_edit
<?php echo $this->Form->create('Post'); ?>
<?php echo $this->Form->input('name', array('label' => 'Titre')); ?>
<?php echo $this->Form->input('category_id', array('label' => 'Catégorie')); ?>
<?php echo $this->Form->input('slug', array('label' => 'URL')); ?>
<?php echo $this->Form->input('id'); ?>
<?php echo $this->Form->input('type', array('value' => 'post', 'type' => 'hidden')); ?>
<?php echo $this->Form->input('content', array('label' => 'Contenu')); ?>
<?php echo $this->Form->input('Post.created', array('label' => 'Date de publication','dateFormat' => 'DMY','timeFormat' => false)); ?>
<?php echo $this->Form->input('online', array('label' => 'En ligne ?','type' => 'checkbox')); ?>
<?php echo $this->Form->end('Envoyer'); ?>
Merci d'avance.