Model view controller 在MVC(cakephp)中保存相关数据

Model view controller 在MVC(cakephp)中保存相关数据,model-view-controller,cakephp,save,Model View Controller,Cakephp,Save,我一次又一次地阅读cakephp中的内容,却不知道如何保存相关数据。我有一个叫做作品的部分和一个叫做家具的部分,可以包含图像: 有效 身份证 头衔 身体 网址 用户id 创造 修改 活跃的 家具 身份证 头衔 身体 网址 用户id 创造 修改 活跃的 图像 身份证 名字 类型 文件路径 用户id 创造 修改 活跃的 我已经为每个实体创建了一个MVC(工作,家具和图像),现在我正尝试使用添加视图将工作添加到我的站点: <script>/* script for .add_i

我一次又一次地阅读
cakephp
中的内容,却不知道如何保存相关数据。我有一个叫做
作品
的部分和一个叫做家具的部分,可以包含
图像

    有效
  • 身份证
  • 头衔
  • 身体
  • 网址
  • 用户id
  • 创造
  • 修改
  • 活跃的
    家具
  • 身份证
  • 头衔
  • 身体
  • 网址
  • 用户id
  • 创造
  • 修改
  • 活跃的
    图像
  • 身份证
  • 名字
  • 类型
  • 文件路径
  • 用户id
  • 创造
  • 修改
  • 活跃的
我已经为每个实体创建了一个MVC(
工作
家具
图像
),现在我正尝试使用
添加
视图将
工作
添加到我的站点:

<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>
工作控制器:

<?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'));
            }
        }
    }
}
?>

图像模型:

<?php

class Image extends AppModel {
    var $name = 'Image';

    var $hasAndBelongsToMany = array (
        'Work' => array (
            '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'
        )
    );
}
?>

这只保存部分
工作
数据:

    有效
  • id-好的
  • 标题-ok
  • 身体-好的
  • url-确定
  • 用户id-为0
  • 创建-确定
  • 修改-ok
  • 活动-为0
我应该如何在
作品
中实现
图像

我想添加一个名为
images\u works
的db表来链接
works
images
furnitures\u images
来链接
furnitures
,我应该怎么做才能继续并修复
用户id缺失的错误

<?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');
    }
}

输入的名称必须不同,并且必须符合CakePHP希望Image saveAll()的数据结构。将名称设置为[Image][counter][name]或[counter][Image][name]。(不确定蛋糕模型期望的是哪一个)否则您将只收到一个图像,并且只保存一个图像


还要确保表单元素为文件上载设置了属性
enctype

数据库中是否缺少外键?如果没有相关的外键,Cake就无法看到事物是如何组合在一起的,即使你在模型中定义了它们。首先感谢你宝贵的帮助,它返回了一个错误
注意(8):未定义的变量:数据[APP/controllers/works\u controller.php,第21行]
我认为在
if($this->Work->create($data)中&&&$this->Work->save($this->data))
,您能修复它吗?确定修复了
$this->Work->create($this->data)
$this->Work->getID()
现在
user\u id
可以正常工作,但是
images\u works
具有所有
user\u id
work\u id
images\u id
0
并且
images
表仍然为空
<?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');
    }
}