Codeigniter 2 und Doctrine 2 HowTo 5/5

June 19th, 2011

Ziel:

  1. Codeigniter 2 mit Doctrine 2 installieren
  2. Klasse erstellten und Datenbank anlegen
  3. Instanz eines Objektes erzeugen
  4. Containment Beziehung nutzen
  5. Containment Beziehung auslesen

5. Containment Elemente auslesen

Das Ziel ist es, alle Workpackages mit ihren Tasks auszulesen.

  • Workpackage1
    • Task1
    • Task2
  • Workpackage2
    • Task3
    • Task4

Dazu wird eine neuer Button im View start.php erzeugt

<?php echo form_open("showdoctrine_main/list_packages_with_tasks"); ?>
<input value="Workpackage auflisten"  type="submit">
<?php echo form_close(); ?>

Es muss zusätzlich eine neue Funktion im Controller erzeugt werden

Application/controllers/showdoctrine_main.php

public function list_packages_with_tasks(){
$em = $this->doctrine->em;
$qb = $em->createQueryBuilder();
$qb->select(array('w', 't'))
->from('models\workpackage','w')
->join('w.task_collection','t');
$q = $qb->getQuery();
//$workpackage = $q->getResult();
$this->data['workpackage_list'] = $q->getResult();

$this->load->view('list',$this->data);

}

Wie zu erkennen ist, muss noch ein View erzeugt werden

Application/view/list.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>

<style type="text/css">

body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 14px;
color: #4F5155;
}

a {
color: #003399;
background-color: transparent;
font-weight: normal;
}

h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 16px;
font-weight: bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;
}

code {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}

</style>
</head>
<body>

<h1>List</h1>

<table>
<?php  foreach ($workpackage_list as $workpackage):  ?>

<tr>
<td>-</td>
<td>
<?php echo $workpackage->getTitle(); ?>
</td>
<td>-</td>
</tr>
<?php  foreach ($workpackage->getTask_Collection() as $task):  ?>
<tr>
<td>-</td>
<td>-</td>
<td>
<?php echo $task->getTitle(); ?>
</td>
</tr>

<?php  endforeach; ?>
<?php  endforeach; ?>
</table>

<?php echo form_open("showdoctrine_main"); ?>
<input value="back"  type="submit">
<?php echo form_close(); ?>
</body>
</html>

Damit die Methode $workpackage->getTask_Collection() ausgeführt werden kann, muss diese in die Klasse Workpackage integriert werden

Application/models/workpackage.php

public function getTask_Collection(){
return $this->task_collection;
}

Jetzt kann die Ausgabe der  Liste im Browser getestet werden.

PHP, usefull stuff | Comments | Trackback Jump to the top of this page

Leave a Reply

  •  
  •  
  •  

You can keep track of new comments to this post with the comments feed.