简体   繁体   English

CakePHP中model-> alias和model-> name之间的区别是什么?

[英]what's the difference between model->alias and model->name in CakePHP?

alias and name are both properties in cake models. aliasname都是蛋糕模型中的属性。 They seem to be similar, but since both of them exist, there must be a difference. 它们似乎相似,但由于它们都存在,因此必然存在差异。 What is it? 它是什么?

I'm guessing, but an alias could be the assigned value when you have a relationship between a model and another and you give the associated model a name not equal to that of the class. 我猜,但是当你在模型和另一个模型之间建立关系时,别名可以是赋值,并且你给关联模型一个不等于类的名称。

For example: 例如:

$hasMany = array('UserNew' => array('className' => 'User'));

In that case, you're using the model that's actually called User, but you're referencing it as 'UserNew'. 在这种情况下,您使用的是实际上称为User的模型,但您将其称为“UserNew”。

It's a collection of table aliases used when cake does a join. 它是蛋糕连接时使用的表别名的集合。 From cake/lib/model/model_php5.php : 来自cake/lib/model/model_php5.php

/**
 * Alias table names for model, for use in SQL JOIN statements.
 *
 * @var array
 * @access public
 */
    var $alias = array();

If you want to temporarily change the table in a model, and you're overriding the CakePHP magic FROM , change $this->alias to what you want it called. 如果要暂时更改模型中的表,并且要覆盖CakePHP魔术FROM,请将$ this-> alias更改为您想要的名称。 $this->name seems to be used in the constructor and must build the alias somewhere in that process. $ this-> name似乎在构造函数中使用,并且必须在该进程中的某处构建别名。 Changing $this->name in a method you want to override, is not enough. 在要覆盖的方法中更改$ this-> name是不够的。 You'll have to change $this->alias and perform the method setSource() as in below: 您将不得不更改$ this-> alias并执行方法setSource(),如下所示:

function my_override_method(){
  $this->setSource('new_db_table_name_here');
  $this->alias = "NewTable";
}
  • name : The name of the model, for example Post . name :模型的名称,例如Post
  • alias : The alias of the model, this is used for registering the instance in the ClassRegistry , for example ParentThread . alias :模型的别名,用于在ClassRegistry注册实例,例如ParentThread

Model::__construct( ) Model :: __ construct()

Take a look at this question also. 看看这个问题也是如此。

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

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