结束了一个Object,我该如何使用它?

时间:2012-04-10 17:34:59

标签: php arrays codeigniter object doctrine

here解决了部分问题之后,我意识到我可以将数据放入视图中,但后来我遇到了另一个问题。

(控制器)

$this->load->model('testing/test_v_to_m_model');
$data['mydata'] = $this->test_v_to_m_model->display_character_info();
$this->load->view('testing/test_v_to_m_view', $data);

(模型)

$query = $this->doctrine->em->createQuery("select u from ORM\Dynasties2\Characters u");
return $query->getResult();

(视图)

foreach ($mydata as $key => $row) {
   print_r($row);
}

这样返回输出:

  

ORM \ Dynasties2 \ Characters Object([id:ORM \ Dynasties2 \ Characters:private] => 76 [name:ORM \ Dynasties2 \ Characters:private] => Gwayn [whichFamily:ORM \ Dynasties2 \ Characters:private ] => 12 [bornDate:ORM \ Dynasties2 \ Characters:private] => -467 [deathDate:ORM \ Dynasties2 \ Characters:private] => -6 [marriedTo:ORM \ Dynasties2 \ Characters:private] =&gt ; 77 [已婚日期:ORM \ Dynasties2 \字符:私人] => -304 [marriageCode:ORM \ Dynasties2 \ Characters:private] => [relig:ORM \ Dynasties2 \ Characters:private] => 0 [isFemale: ORM \ Dynasties2 \ Characters:private] => 0 [betrothedTo:ORM \ Dynasties2 \ Characters:private] => [fathersId:ORM \ Dynasties2 \ Characters:private] => 0 [successionOrder:ORM \ Dynasties2 \ Characters: private] => 0 [isPregnant:ORM \ Dynasties2 \ Characters:private] => [pregnantTurnsLeft:ORM \ Dynasties2 \ Characters:private] => [marriedOutOfFamily:ORM \ Dynasties2 \ Characters:private] => [bornMatrilineal :ORM \ Dynasties2 \ Characters:private] =>)

所以......我只是不知道如何对此做任何事情 - 我尝试了一个嵌套的foreach来回显数据并且无法让它工作。我希望它是因为这是 Object ,而不是 Array ,这是正确的吗?

  1. 我究竟如何访问/操作这些字段?

  2. 我可以在Doctrine2 / CodeIgniter2模型中使用不同的代码来为数据提供更简单的字段名称 - 比如sql AS吗?

1 个答案:

答案 0 :(得分:0)

您应该能够使用指针访问对象,如下所示:

foreach ($mydata as $key => $row) {
   echo($row->id);
   echo($row->name);
}

stdClass是php的通用空类。这是一个很好的使用它的教程:http://krisjordan.com/dynamic-properties-in-php-with-stdclass

编辑:

尝试使用访问者:

echo($row->getId());