我可以在 Doctrine2 中迭代实体的属性吗?

发布于 2024-11-29 19:07:55 字数 418 浏览 5 评论 0原文

我使用

$myblogrepo = $this->_doctrine->getRepository('Entities\Blog')->findBy(array('id' => 12);

我访问通过

foreach($myblogrepo as $key =>$value){

echo $key . $value;

}

如何获取字段名称?我认为关键=>可以工作,但它把密钥打印为 0

所以我认为这会工作:

foreach($myblogrepo[0] as $key =>$value){

echo $key . $value;
}

但仍然没有.. }

i use

$myblogrepo = $this->_doctrine->getRepository('Entities\Blog')->findBy(array('id' => 12);

i access via

foreach($myblogrepo as $key =>$value){

echo $key . $value;

}

how can i get the field names? i thought the key => would work but it print s the key as 0

so i thought this would work:

foreach($myblogrepo[0] as $key =>$value){

echo $key . $value;
}

but still nothing..
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难遇 2024-12-06 19:07:55

您的博客实体的属性很可能被声明为受保护。这就是为什么您无法从实体本身外部迭代它们的原因。

如果您以只读方式使用博客实体,并且只需要访问标记为@Columns的属性(阅读:您不需要在实体上调用任何方法),您可以考虑使用数组水合。这样您将处理简单的数组,并且 $k=>$v 类型迭代将正常工作。

否则,您需要在实体类上创建某种 getValues() 方法。这可能是一个简单的实现,只需构建数组并返回它。

最后,您可以创建一个通用的 getValues() 作为实用函数,它使用原则的类元数据来找出列和实体具有哪些数据,并对这些数据进行操作。像这样的简单实现:

function getEntityColumnValues($entity,$em){
  $cols = $em->getClassMetadata(get_class($entity))->getColumnNames();
  $values = array();
  foreach($cols as $col){
    $getter = 'get'.ucfirst($col);
    $values[$col] = $entity->$getter();
  }
  return $values;
}

编辑 - 上述方法的更成熟版本似乎是可在此处获取 - 我还没有使用过它,但它看起来很有希望。

In all likelihood, your Blog entity's properties are declared as protected. This is why you can't iterate over them from outside the the Entity itself.

If you're using your Blog entities in a read-only fashion, and only need access to the properties marked as @Columns (read: you don't need to call any methods on your entity), you might consider using array-hydration. That way you'll be dealing with simple arrays, and $k=>$v type iteration will work fine.

Otherwise, you'll need to create some kind of getValues() method on your entity class. This could be a simple implementation that just builds and array and returns it.

Finally, you could create a general-purpose getValues() as a utility function that uses doctrine's class metadata to figure out what columns and entity has, and operate on those data. A simple implementation like this:

function getEntityColumnValues($entity,$em){
  $cols = $em->getClassMetadata(get_class($entity))->getColumnNames();
  $values = array();
  foreach($cols as $col){
    $getter = 'get'.ucfirst($col);
    $values[$col] = $entity->$getter();
  }
  return $values;
}

EDIT - A more mature version of the above method seems to be available here - I've not played with it yet, but it looks promising.

仙女 2024-12-06 19:07:55

如果您只需要以快速且简单的方式获取实体的属性,这就是我在项目中所做的:

我的所有实体都继承自 EntityBase 类,该类具有以下方法:

public function toValueObject()
{
    $result = new \stdClass();
    foreach ($this as $property => $value) {
        $getter = 'get' . ucfirst($property);
        if (method_exists($this, $getter)) {
            $result->$property = $this->$getter();
        }
    }
    return $result;
}

所以我所要做的就是调用$entity->toValueObject() 并且我获得了一个标准对象,其中实体的所有属性都作为公共属性。

If you just need to get the properties of the entity in a fast and easy way, this is what I do in my projects:

All my entities inherit from an EntityBase class, which has the following method:

public function toValueObject()
{
    $result = new \stdClass();
    foreach ($this as $property => $value) {
        $getter = 'get' . ucfirst($property);
        if (method_exists($this, $getter)) {
            $result->$property = $this->$getter();
        }
    }
    return $result;
}

So all I have to do is call $entity->toValueObject() and I obtain a standard object with all of the entity's properties as public properties.

风情万种。 2024-12-06 19:07:55

使用 findOneBy 而不是 findBy 来选择单行。

$myblogrepo = $this->_doctrine->getRepository('Entities\Blog')->findOneBy(array('id' => 12);

您的键是 0 因为它是可能的多行结果中的第一行。

Use findOneBy instead of findBy to select a single row.

$myblogrepo = $this->_doctrine->getRepository('Entities\Blog')->findOneBy(array('id' => 12);

Your key was 0 because it was the first row in a possible multi-row result.

小霸王臭丫头 2024-12-06 19:07:55

这是我对序列化器类的实现,它还检查它是否是一个学说实体:

/**
 * JsonApiSerializer constructor.
 * @param EntityManagerInterface $em
 */
public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}

/**
 * @param $payLoad
 * @return string
 */
public function serialize($payLoad, $type)
{
    $serializedPayload = new \stdClass();
    $serializedPayload->data = new \stdClass();

    $serializedPayload->data->type = $type;

    if ($this->isDoctrineEntity($payLoad)) {
        $this->addEntityColumnValues($serializedPayload, $payLoad);
    }

    return json_encode($serializedPayload);
}

private function isDoctrineEntity($payLoad)
{
    if (is_object($payLoad)) {
        $payLoad = ($payLoad instanceof Proxy)
            ? get_parent_class($payLoad)
            : get_class($payLoad);
    }

    return !$this->em->getMetadataFactory()->isTransient($payLoad);
}

private function addEntityColumnValues(&$serializedPayload, $entity){
    $serializedPayload->data->attributes = new \stdClass();

    $classMetaData = $this->em->getClassMetadata(get_class($entity));
    $columnNames = $classMetaData->getColumnNames();
    foreach($columnNames as $columnName){
        $fieldName = $classMetaData->getFieldForColumn($columnName);
        $getter = 'get'.ucfirst($fieldName);
        $serializedPayload->data->attributes->$columnName = $entity->$getter();
    }
}

This is a my implementation of a serializer class that also check if it is a doctrine entity:

/**
 * JsonApiSerializer constructor.
 * @param EntityManagerInterface $em
 */
public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}

/**
 * @param $payLoad
 * @return string
 */
public function serialize($payLoad, $type)
{
    $serializedPayload = new \stdClass();
    $serializedPayload->data = new \stdClass();

    $serializedPayload->data->type = $type;

    if ($this->isDoctrineEntity($payLoad)) {
        $this->addEntityColumnValues($serializedPayload, $payLoad);
    }

    return json_encode($serializedPayload);
}

private function isDoctrineEntity($payLoad)
{
    if (is_object($payLoad)) {
        $payLoad = ($payLoad instanceof Proxy)
            ? get_parent_class($payLoad)
            : get_class($payLoad);
    }

    return !$this->em->getMetadataFactory()->isTransient($payLoad);
}

private function addEntityColumnValues(&$serializedPayload, $entity){
    $serializedPayload->data->attributes = new \stdClass();

    $classMetaData = $this->em->getClassMetadata(get_class($entity));
    $columnNames = $classMetaData->getColumnNames();
    foreach($columnNames as $columnName){
        $fieldName = $classMetaData->getFieldForColumn($columnName);
        $getter = 'get'.ucfirst($fieldName);
        $serializedPayload->data->attributes->$columnName = $entity->$getter();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文