Saving related data in MVC (cakephp)
I'm reading again and again the book from cakephp
without understand how to save related data. I have a section called Works
and a section called Furnitures which can contain Images
:
-
Works
- id
- title
- body
- url
- user_id
- created
- modified
- active
-
Furnitures
- id
- title
- body
- url
- user_id
- created
- modified
- active
-
Images
- id
- name
- type
- filepath
- user_id
- created
- modified
- active
I've created one MVC for every entity (Work
, Furniture
and Image
), and now I'm trying to add a Work
to my site with the add
view:
<script>/* script for .add_image, Work and other models can have 0 or more images */</script>
<div><?php echo $this->Form->input('Work.title', array('value'=>'titolo', 'label' => 'Nome lavoro')); ?></div>
<div><?php echo $this->Form->input('Work.body', array('type' => 'textarea', 'value'=>'description', 'label' => 'Descrizione')); ?></div>
<div><?php echo $this->Form->input('Work.url', array('value'=>'http://', 'label'=> false)); ?></div>
<div>Immagini</div>
<div><?php echo $this->Form->input('Image.name', array('label'=> false, 'value'=>'Nome immagine'));?></div>
<div><?php echo $this->Form->file('Image.file'); ?></div>
<div><div class="add_image">Add image</div></div>
<div><?php echo $this->Form->end('Inserisci lavoro'); ?></div>
This saves only the Work
data, without Image
related, I suppose I'm missing something in the Model
and in the Controller
:
<?php
class Work extends AppModel {
var $name = 'Work';
var $belongsTo = array ('User');
var $hasAndBelongsToMany = array (
'Image' => array (
'className' => 'Image',
'order' => 'Image.created DESC',
'exclusive' => true,
'dependent' => true // this will remove all images related to article
),
'Tag' => array (
'className' => 'Tag',
'order' => 'Tag.name DESC'
)
);
var $validate = array (
'title' => array(
'rule' => array('maxLength', 60),
'allowEmpty' => false
),
'body' => array (
'rule' => array('maxLength', 250),
'allowEmpty' => false
),
'url' => array (
'rule' => array('url', true),
'allowEmpty' => true
)
);
function parentNode () {
return null;
}
}
?>
WorksController:
<?php
class WorksController extends AppController {
var $name = 'Works';
var $components = array ('Session');
function index () {
$this->set('works', $this->Work->find('all'));
}
function view () {
$this->Work->id = $id;
$this->set('work', $this->Work->read());
}
function add () {
if (!empty($this->data)) {
$work = $this->Work->save($this->data);
if (!empty($work)) {
// this is wrong, but what should I write?
// $this->data['Image']['user_id'] = $this->Work->User->id;
// $this->Work->Image->saveAll($this->data);
$this->Session->setFlash ('Lavoro inserito correttamente');
$this->redirect(array('action'=>'index'));
}
}
}
}
?>
ImageModel:
<?php
class Image extends AppModel {
var $name = 'Image';
var $hasAndBelongsToMany = array (
'Work' => array (
开发者_如何学C 'className' => 'Work'
),
'Furniture' => array (
'className' => 'Furniture'
)
);
var $actsAs = array ('FileUpload.FileUpload' => array (
'uploadDir' => 'img/shared',// 'forceWebroot' => true, is default
'allowedTypes' => array(
'jpg' => array('image/jpeg', 'image/pjpeg'),
'jpeg' => array('image/jpeg', 'image/pjpeg'),
'gif' => array('image/gif'),
'png' => array('image/png','image/x-png'),
),
'maxFileSize' => 614400, // 600kB in bytes
'unique' => true
));
// in the wiew file: echo $form->input('Image.file', array('type' => 'file'));
var $validate = array (
'name' => array (
'rule' => 'notEmpty',
'message' => 'Nome dell\'immagine in fase di cariamento mancante'
),
'filename' => array (
'rule' => 'notEmpty',
'message' => 'Nome dell\'immagine in fase di cariamento mancante'
)
);
}
?>
This saves only part of of Works
data:
-
Works
- id - ok
- title - ok
- body - ok
- url - ok
- user_id - is 0
- created - ok
- modified - ok
- active - is 0
How should I implement Images
in the Works
?
I thought to add a db table named images_works
to link Works
with Images
and furnitures_images
to link Furnitures
with Images
, what should I do to continue and fix the user_id missing error
?
first
<?php
class WorkdsController extends AppController
var $components = array('Session', 'Auth');
if($this->Wok->create($data) && $this->Work->save($this->data)){
$work_id = $this->Work->getID;
// and than use work_id work with work id. then you should somehow get user_id
// for example $this->Auth->user(id');
}
}
then if you want to link images to works in the way you have described above (hasMany) then you should add a work_id to images table.
otherwise create a new table images_works (id, image_id, work_id) and use $hasAndBelongsToMany, instead of $hasMay
The input's name must be different and according to data structure that CakePHP expect for Image to saveAll(). Either make the name to be [Image][counter][name] or [Counter][Image][name]. (Not sure which one it is that cake model expect) Or else you'll receive the only one image and only one image will be saved.
Also make sure the form element has the attribute enctype
set for file upload.
精彩评论