杨庆荣的博客

工作之余接单:公司产品展示网站建设、电商网站建设、小程序、app开发。+86 13714715608

doctrine 简单例子

点击 848    创建时间 2015-07-30 17:45:07

src/Product.php

<?php
/**  
 * @Entity @Table(name="products")  
 **/  
 class Product  
 {  
    /** @Id @Column(type="integer") @GeneratedValue **/  
    protected $id;  
    /** @Column(type="string") **/  
    protected $name;

    public function getId()  
    {  
        return $this->id;  
    }

    public function getName()  
    {  
        return $this->name;  
    }

    public function setName($name)  
    {  
        $this->name = $name;  
    }  
 }
 ?>

test.php


<?php
 ini_set("display_errors", 1);  
 require_once ("bootstrap.php");

 $entityManager = GetEntityManager();

$act = @$_GET["act"];

if ($act == "add") {

    require_once 'src/Product.php'  
    $newProductName = "orange";  
    $product = new Product();  
    $product -> setName($newProductName);

    $entityManager -> persist($product);  
    $entityManager -> flush();

    echo "Created Product with ID " . $product -> getId() . "";

} elseif ($act == "index") {  
    //http://127.0.0.3/doctrine/test.php?act=index

    $productRepository = $entityManager -> getRepository('Product');  
    $products = $productRepository -> findAll();

    foreach ($products as $product) {  
        echo sprintf("-%s", $product -> getName());  
    }

} elseif ($act == "show") {

    //http://127.0.0.3/doctrine/test.php?act=show&id=2  
    $id = intval(@$_GET["id"]);  
    $product = $entityManager -> find('Product', $id);

    if ($product === null) {  
        echo "No product found. ";  
        exit(1);  
    }

    echo sprintf("-%s", $product -> getName());  


 } elseif ($act == "update") {  


    // http://127.0.0.3/doctrine/test.php?act=update&id=2  
    $id = intval(@$_GET["id"]);  
    $product = $entityManager -> find('Product', $id);

    if ($product === null) {  
        echo "Product $id does not exist.";  
        exit(1);  
    }

    $product -> setName( "newName");

    $entityManager -> flush();  
 }

上一篇: css高度自适应代码
下一篇: Failure to find org.restlet:org.restlet:jar:2.2.1