简体   繁体   English

如何在cakephp中按组件保存数据

[英]How to Save Data by Component In cakephp

I want to store data by using component in cakephp 我想通过在cakephp中使用组件来存储数据

My component name is UpdatedateComponent: 我的组件名称是UpdatedateComponent:

<?php
class UpdatedateComponent extends Component
    {   
        public function clean($string)
            {
                $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
                return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
            }

        public function updatedatefunction($proid,$edit,$model1,$model2,$field)
            {
                if ($this->request->is(array('post', 'put'))) {
                    $this->$model1->$model2->id = $edit;
                    $this->request->data[$model2][$field] = $proid;
                    date_default_timezone_set('Asia/Kolkata');
                    $date = date('Y/m/d H:i:s');
                    $this->request->data[$model2]['cganges'] = $date;
                    $this->$model1->$model2->save($this->request->data);
                } 
            }
    }
?>

My controller name is SlidermasterControler 我的控制器名称是SlidermasterControler

I am calling this component by like this 我这样称呼这个组件

$this->Updatedate->updatedatefunction($proid,1,'Slidermaster','Homesliderchangemaster','slidermaster_id'); $ this-> Updatedate-> updatedatefunction($ proid,1,'Slidermaster','Homesliderchangemaster','slidermaster_id');

but data not being stored 但是没有存储数据

You can't access models directly from the component, since components are meant to perform some common tasks for controllers. 您不能直接从组件访问模型,因为组件旨在执行控制器的一些常见任务。

To access a model from within a component you need to first access the controller instance and then the respective model. 要从组件内部访问模型,您需要首先访问控制器实例,然后访问相应的模型。

$this->controller->ModelOne->ModelTwo-save($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM