jus*_*ohu 3 cakephp cakephp-3.0
这是我开始学习CakePHP 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());
}
Run Code Online (Sandbox Code Playgroud)
让我为你打开这本书:
虽然表对象表示并提供对对象集合的访问,但实体表示应用程序中的各个行或域对象.实体包含持久属性和方法来操作和访问它们包含的数据.
-
为什么我们必须创建新的实体/这里的实体角色是什么.
Cake3中的几乎所有内容(如果不是全部)都与实体一起使用,上面解释了实体是什么.您需要创建一个新实体,以便FormHelper可以使用它,AFAIR它仍然可以使用数组,如果配置也这样做但应该使用该实体.
实体存在的原因是抽象数据.有些人认为实体是数据库行的表示 - 这是错误的.正如书中所说,它们可以是一排但不必代表一行,因为3.0 ORM也可以与其他资源一起使用.理论上,您可以拥有一个CSV数据源,每行返回一个实体.
我建议你阅读CakePHP核心中的实体代码,以便更深入地了解其他实体提供的内容,只是说它们只是"只是"一组属性就是简短的想法.
为什么我们必须在发布后使用这一行/这一行的作用是什么.
后期数据合并到先前创建的实体中,就是这样.如果您有类似的基本问题,请使用API.请参阅patchEntity()的API条目.