Bonjour,
j'ai repris le code de Crafikart pour uploader une image et l'associer à une personne
Ce que je fais
Décrivez ici votre code ou ce que vous cherchez à faire
Controller Personne
<?php
/**
* Application level Controller
*
* This file is application-wide controller file. You can put all
* application-wide controller-related methods here.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Controller
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('AppController', 'Controller');
/**
* Application Controller
*
* Add your application-wide methods in the class below, your controllers
* will inherit them.
*
* @package app.Controller
* @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
*/
class PersonnesController extends AppController {
var $name = 'Personnes';
// méthode qui récupère tous les articles et les transmet à la vue :
public function index(){
$personnes = $this->Personne->find('all');
$this->set('p',$personnes);
}
public function nationalite(){
$personnes = $this->Nationalite->find('all',array("Personne.nationalite_id"=>$id));
$this->set('p',$personnes);
$this->render("index");
}
public function profession(){
$personnes = $this->Personne->find('all',array("Personne.profession_id"=>$id));
$this->set('p',$personnes);
$this->render("index");
}
public function ajout($id=null){
$nationalite = $this->Personne->Nationalite->find('list',array('recursive'=> -1,'fields'=>array('id','libelle')));
$this->set('n',$nationalite);
$profession = $this->Personne->Profession->find('list',array('recursive'=> -1,'fields'=>array('id','libelle')));
$this->set('pr',$profession);
if ($this->Personne->save($this->request->data)) {
$this->Flash->success(__('Cette personne a été ajouté.'));
return $this->redirect(array('action' => 'index'));
}
if($id != null){
$this->Personne->id= $id;
$this->data=$this->Personne->read();
}
}
public function modif($id = null){
if (!$id) {
throw new NotFoundException(__('Personne invalide'));
}
$post = $this->Personne->findById($id);
if (!$post) {
throw new NotFoundException(__('Personne invalide'));
}
$nationalite = $this->Personne->Nationalite->find('list',array('recursive'=> -1,'fields'=>array('id','libelle')));
$this->set('n',$nationalite);
$profession = $this->Personne->Profession->find('list',array('recursive'=> -1,'fields'=>array('id','libelle')));
$this->set('pr',$profession);
if ($this->request->is(array('post', 'put'))) {
$this->Personne->id = $id;
if ($this->Personne->save($this->request->data)) {
$this->Flash->success(__('Cette personne a été modifié.'));
return $this->redirect(array('action' => 'index'));
}
$this->Flash->error(__('Impossible de mettre à jour cette personne.'));
}
if (!$this->request->data) {
$this->request->data = $post;
}
}
public function delete($id){
$this->Personne->delete($id);
$this->redirect('index');
}
}
Model personne
<?php
/**
* Application model for CakePHP.
*
* This file is application-wide model file. You can put all
* application-wide model-related methods here.
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.Model
* @since CakePHP(tm) v 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
App::uses('AppModel', 'Model');
/**
* Application model for Cake.
*
* Add your application-wide methods in the class below, your models
* will inherit them.
*
* @package app.Model
*/
class Personne extends AppModel {
public $name='Personne';
public $belongsTo = array(
'Nationalite' => array(
'className' => 'Nationalite',
'conditions' => '',
'order' => '',
'foreignKey' => 'nationalite_id' //or your external key
),
'Profession' => array(
'className' => 'Profession',
'conditions' => '',
'order' => '',
'foreignKey' => 'profession_id' //or your external key
)
);
public $hasMany = 'Film';
public $actAs = array(
'Upload.Upload' => array(
'fields' => array(
'photo' => 'img/photos/:id1000/:id'
)
)
);
public $validate = array(
'photo_file' => array(
'rule' => array('extension', array('gif', 'jpeg', 'png', 'jpg')),
'required' => false,
'allowEmpty' => true,
'message' => 'Merci de soumettre une image valide.'
)
);
}
?>
formulaire ajout
<?php echo $this->form->create('Personne',array('url'=>array('action'=>'ajout','type'=>'file'))); ?>
<?= $this->Form->create('Personne', array('type' => 'file')); ?>
<script type="text/javascript">
$(document).ready(function() {
$('.datepick').datepicker({ dateFormat: "yy-mm-dd"});
});
</script>
<?php echo $this->Html->script("jquery.min.js"); ?>
<body>
<h1>Ajouter la personne</h1>
<form>
<div class="form-group">
<?php echo $this->form->input('Personne.nom',array('class'=>'form-control', 'label'=>'Nom:')); ?>
</div>
<div class="form-group">
<?php echo $this->form->input('Personne.prenom',array('class'=>'form-control', 'label'=>'Prénom:')); ?>
</div>
<!-- Date de sortie -->
<div class="form-group">
<?php echo $this->form->input('Film.date_de_sortie',array('class'=>"datepick",'label'=>'Sortie:')); ?>
</div>
<!-- photo -->
<div class="form-group">
<!--<?php echo $this->form->input('photo_file',array('class'=>"datepick",'label'=>'Photo:', 'type' => 'file')); ?>-->
<?php echo $this->Form->input('photo_file', array('type' => 'file')); ?>
</div>
<div class="input-group">
<?php echo $this->form->input('Personne.nationalite_id',array('class'=>'form-control', 'label'=>'Nationalité:','options'=>$n)); ?>
</div>
<div class="input-group">
<?php echo $this->form->input('Personne.profession_id',array('class'=>'form-control', 'label'=>'Profession:','options'=>$pr)); ?>
</div>
<div><?php echo $this->form->end(array("Envoyer",'class'=>'btn btn-default'));?></div>
</form >
</body>
Ce que je veux
j'essaie d'afficher l'image et que le nom entre dans la base de données dans le champs "photo" qui sur trouve de base null
Ce que j'obtiens
Rien ne se passe
merci d'avance pour votre aide =)