jdo*_*dog 5 php doctrine doctrine-1.2 symfony doctrine-orm
我的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 }
Run Code Online (Sandbox Code Playgroud)
一些组织存储了整数值0,并且没有这样的organisation_id.令我惊讶的是,当我运行此代码时
class organisationActions extends autoOrganisationActions{
public function executeEdit(sfWebRequest $request){
$this->organisation = $this->getRoute()->getObject();
$p = $this->organisation->getParentOrganisationId();
var_dump($p);
Run Code Online (Sandbox Code Playgroud)
结果是字符串(1)"0"
为什么这不返回整数,所以我可以比较=== 0
我做了一些测试,发现实体的每个值都是通过每个实体模型的父类sfDoctrineRecord
在_call
方法中执行的魔术调用返回的。因此,看起来的返回类型call_user_func_array
与 string 或 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;
}
}
Run Code Online (Sandbox Code Playgroud)
在控制器中:
$p = $this->organisation->getParentIdAsIntegerOrNullOtherwise();
var_dump($p);
Run Code Online (Sandbox Code Playgroud)
它会倾倒
NULL
Run Code Online (Sandbox Code Playgroud)
if 没有链接到任何父节点
并将倾倒
int(0)
Run Code Online (Sandbox Code Playgroud)
如果它链接到 id = 0 的元素
希望这有帮助
让我知道你的想法
归档时间: |
|
查看次数: |
895 次 |
最近记录: |