加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

CakePHP 3实体混乱

发布时间:2020-12-13 21:53:05 所属栏目:PHP教程 来源:网络整理
导读:这是我开始学习Cake PHP 3以来一直面临的问题 实体的这个概念是什么,现实世界的例子会有所帮助. public function add(){ // why do we have to create new entity / what is the role of entity here. $comment = $this-Comments-newEntity(); if ($this-req
这是我开始学习Cake PHP 3以来一直面临的问题

实体的这个概念是什么,现实世界的例子会有所帮助.

public function add()
{
            // why do we have to create new entity / what is the role of entity here. 
    $comment = $this->Comments->newEntity();

    if ($this->request->is('post','put')) {

                    // why do we have to use this line after posting / what is the role of this line. 

        $comment = $this->Comments->patchEntity($comment,$this->request->data);

        if ($this->Comments->save($comment)) {
            $this->Flash->success('comment submitted successfully.');
        } else {
            $this->Flash->error('Sorry,comment could not be updated.');
        }
    }

    return $this->redirect($this->referer());
}

解决方法

让我为你打开 the book:

While Table Objects represent and provide access to a collection of
objects,entities represent individual rows or domain objects in your
application. Entities contain persistent properties and methods to
manipulate and access the data they contain.

why do we have to create new entity / what is the role of entity here.

Cake3中的几乎所有内容(如果不是全部)都与实体一起使用,上面解释了实体是什么.您需要创建一个新实体,以便FormHelper可以使用它,AFAIR它仍然可以使用数组,如果配置也这样做但应该使用该实体.

实体存在的原因是抽象数据.有些人认为实体是数据库行的表示 – 这是错误的.正如书中所说,它们可以是一排但不必代表一行,因为3.0 ORM也可以与其他资源一起使用.理论上,您可以拥有一个CSV数据源,每行返回一个实体.

我建议你阅读CakePHP核心中的实体代码,以便更深入地了解其他实体提供的内容,只是说它们只是“只是”一组属性就是简短的想法.

why do we have to use this line after posting / what is the role of this line.

后期数据合并到先前创建的实体中,就是这样.如果您有类似的基本问题,请使用API??. See the API entry for patchEntity().

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读