当字段有下划线时,Magic Doctrine2 finders? [英] Magic Doctrine2 finders when field has underscore?
问题描述
当字段中间有下划线时,我在使用 Doctrine2 的 find*()
魔术方法时遇到问题.
$repository->findByName("Hello");//工作$repository->findByIsEnabled(true);
<块引用>
实体 'AcmeSecurityBundleEntityPackage' 没有字段 'isEnabled'.因此,您不能在实体上调用findByIsEnabled"存储库.
这是 YAML 中用于复制错误的简单实体定义:
AcmeSecurityBundleEntityPackage:类型:实体存储库类:AcmeSecurityBundleRepositoryPackageRepository表:security_packageID:ID:类型:整数生成器:{策略:自动}领域:名称:类型:字符串长度:255唯一:真is_enabled:类型:布尔值
我记得遇到过同样的问题,并认为我通过写这样的东西解决了它:
<块引用>$repository->findBy(array('is_enabled' => true));
让我们看一下代码:
重点在这里:
$fieldName = lcfirst(DoctrineCommonUtilInflector::classify($by));
现在让我们看看分类:
如果你想让它起作用,你似乎应该写你的字段likeThis".
I'm having problems using find*()
magic methods of Doctrine2 when the field has an underscore in between.
$repository->findByName("Hello"); // Works
$repository->findByIsEnabled(true);
Entity 'AcmeSecurityBundleEntityPackage' has no field 'isEnabled'. You can therefore not call 'findByIsEnabled' on the entities' repository.
This is the simple entity definition in YAML for replicating the error:
AcmeSecurityBundleEntityPackage:
type: entity
repositoryClass: AcmeSecurityBundleRepositoryPackageRepository
table: security_package
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 255
unique: true
is_enabled:
type: boolean
I recall having had the same problem and think I solved it by writing something like this :
$repository->findBy(array('is_enabled' => true));
Let's look at the code :
<?php
/**
* Adds support for magic finders.
*
* @return array|object The found entity/entities.
* @throws BadMethodCallException If the method called is an invalid find* method
* or no find* method at all and therefore an invalid
* method call.
*/
public function __call($method, $arguments)
{
if (substr($method, 0, 6) == 'findBy') {
$by = substr($method, 6, strlen($method));
$method = 'findBy';
} else if (substr($method, 0, 9) == 'findOneBy') {
$by = substr($method, 9, strlen($method));
$method = 'findOneBy';
} else {
throw new BadMethodCallException(
"Undefined method '$method'. The method name must start with ".
"either findBy or findOneBy!"
);
}
if ( !isset($arguments[0])) {
// we dont even want to allow null at this point, because we cannot (yet) transform it into IS NULL.
throw ORMException::findByRequiresParameter($method.$by);
}
$fieldName = lcfirst(DoctrineCommonUtilInflector::classify($by));
if ($this->_class->hasField($fieldName) || $this->_class->hasAssociation($fieldName)) {
return $this->$method(array($fieldName => $arguments[0]));
} else {
throw ORMException::invalidFindByCall($this->_entityName, $fieldName, $method.$by);
}
}
The key line is here:
$fieldName = lcfirst(DoctrineCommonUtilInflector::classify($by));
Now let's have a look to classify :
<?php
/**
* Convert a word in to the format for a Doctrine class name. Converts 'table_name' to 'TableName'
*
* @param string $word Word to classify
* @return string $word Classified word
*/
public static function classify($word)
{
return str_replace(" ", "", ucwords(strtr($word, "_-", " ")));
}
It looks like you're supposed to write your fields "likeThis" if you want this to work.
这篇关于当字段有下划线时,Magic Doctrine2 finders?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!