1

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

(控制器)

$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 对象 ( [id:ORM\Dynasties2\Characters:private] => 76 [name:ORM\Dynasties2\Characters:private] => Gwayn [whichFamily:ORM\Dynasties2\Characters:private] => 12 [出生日期:ORM\Dynasties2\Characters:private] => -467 [deathDate:ORM\Dynasties2\Characters:private] => -6 [marriedTo:ORM\Dynasties2\Characters:private] => 77 [marriedDate:ORM\Dynasties2\ Characters:private] => -304 [marriageCode:ORM\Dynasties2\Characters:private] => [religion: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 的代码?

4

1 回答 1

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());
于 2012-04-10T17:43:04.130 回答