我的schema.yml
Organisation:
columns:
id: { type: integer(4), notnull: true, unique: true, primary: true, autoincrement: true }
name: { type: string(100), notnull: true, unique: true }
parent_organisation_id: { type: integer(4), notnull: false }
relations:
ParentOrganisation: { class: Organisation, local: parent_organisation_id, foreignAlias: ChildOrganisations }
有些组织存储了整数值0,并且没有这样的organisation_id。令我惊讶的是,当我运行此代码时
class organisationActions extends autoOrganisationActions{
public function executeEdit(sfWebRequest $request){
$this->organisation = $this->getRoute()->getObject();
$p = $this->organisation->getParentOrganisationId();
var_dump($p);
结果是字符串(1)“0”
为什么这不返回整数,所以我可以比较=== 0
答案 0 :(得分:3)
我做了一些测试,我发现实体的每个值都是通过魔术调用返回的,每个实体模型sfDoctrineRecord
的父类在_call
方法中执行。因此,call_user_func_array
的返回类型似乎与字符串或int等不同。我们在每个实体的每个字段上都执行相同的行为, id 字段也是如此。
因此,作为 解决方法 ,您可以实现自定义getter,以检查记录是否为null或者是比较操作的第一个(id = 0),如下所示:
class Organisation extends BaseOrganisation
{
public function getParentIdAsIntegerOrNullOtherwise()
{
$an_id = $this->getParentOrganisationId();
if (! is_null($an_id))
{
return intval($an_id);
}
return NULL;
}
}
在控制器中:
$p = $this->organisation->getParentIdAsIntegerOrNullOtherwise();
var_dump($p);
它将转储
NULL
如果未链接到任何父节点
并将转储
int(0)
如果它链接到id = 0
的元素希望这个帮助
让我知道你对此的看法