Codeigniter 2 und Doctrine 2 HowTo 3/5

June 19th, 2011

Ziel:

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

3. Instanz einer Klasse erzeugen

Ziel:  Anlegen einer Aufgabe:

* Controller: showdoctrine_main.php
* Viewer:  start.php

Anpassen des Start-Controllers – Ordner “Application/config/routes.php”

$route['default_controller'] = "showdoctrine_main";

Controller angelegen:

Controller: application/controller/showdoctrine_main.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
use models\Task;
class Showdoctrine_main extends CI_Controller {

public function add_task(){

$task = new \models\Task();
$task->setTitle("first");
$em = $this->doctrine->em;
$em->persist($task);
$em->flush();
$this->load->view('start');
}

public function index()
{
$this->load->view('start');
}
}

?>

Viewer anlegen:

application/view/start.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>Welcome to ShowDoctrine</h1>

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

wird auf  “Task anlegen” geklickt, wird ein neuer Task in der Tabelle Task erstellt (nachfolgende Zusatzinformation beachten).

————————————————————-

Zusatzinformation zum Namespace:

Doctrine muss als Bibliothek automatisch geladen werden

Datei: application/config/autoload.php

$autoload['libraries'] = array('doctrine');

Ein Fehler der darauf hindeutet, dass die Bibliothek noch nicht geladen ist lauted:

Class ‘models\Task’ not found

bezogen auf eine Zeile im  Code an der “$task = new \models\Task();” verwendet wird

Zusatzinformation zum Formhelper

Um in einer View-Datei mit dem Form-Helper zu arbeiten, muss dieser unter “Appclication/config/autoload.php” aktiviert werden.

$autoload['helper'] = array('form');


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.