PHP ORM Tekbaşına kullanımı

0

Tarih : 16-10-2009 | Yazan : Hüseyin Bora | Kategori : Faces ORM, PHP, PHP ORM

PHP ORM

PHP Faces ORM tek başına kullanabilirsiniz bunun için öncelikle aşağıdaki adresten ORM çatısını indirin. İlgili dosyaları bir dizine çıkarın ve kullanacağınız sunucuya taşıyın.

PHP ORM indir.

Yapılandırma

PHP ini dosyanızdan aşağıdaki eklentilerin kullanılabilir halde düzenleyin
extension = pdo.so
extension = pdo_sqlite.so
extension = sqlite.so
extension = pdo_mysql.so
Bir yapılandırma dosyası hazırlayın örneğin config.php dosyası olabilir

Örnek yapılandırma dosyası

define("DB_CONNECTION_STRING","mysql:host=localhost;dbname=faces");//sunucunuz ve veritabanı adı
define("DB_USER","root");//veritabanı kullanıcı adı
define("DB_PASS","abc345");
define("DB_CONNECTOR","PDO");//bu satırı değiştirmeyin
>require_once "facespersistence/persistence.php";// PHP ORM Persistence kütüphanesi

ORM katmanını kullanmak istediğimiz php dosyalarında yukarıda oluşturduğumuz config.php dosyasını çağırmamız gerekecek.

require_once “config.php”;

Bir örnekle açıklamaya çalışalım aşağıdaki gibi bir varlık sınıfımız models klasöründe kayıtlu bulunsun olsun.

/**
*  @Table(name = "urun")
*/
class Urun extends Entity
{
/**
* @Id
* @Column(name = "no")
*/

private $no;
/**
* @Column(name = "ad")
*/

private $ad;
/**
* @Column(name = "fiyat")
*/

private $fiyat;
function get($name) {

return $this->$name;
}
function set($name,$value) {
$this->$name = $value;
}
}

Faces ORM ile birlikte importmodels(”models”); fonksiyonu gelmekte aşağıdaki örneği inceleyin.


require_once "config.php";
importmodels("models");
$session = EntityManager::getInstance();
$query =$session->createQuery("select u from Urun u where u.no=10");
$query->execute();
$urunler =$query->getSingle();
echo "<pre>";
print_r($urunler);
echo "</pre>";
$urun = new Urun();
$elma->ad= "Elma";
$elma->fiyat="11.23";
$session->save($elma);
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Doğrulama Sınıfları (Kendi Doğrulayıcılarınız)

0

Tarih : 26-09-2009 | Yazan : Hüseyin Bora | Kategori : FDL, Faces ORM, PHP Faces

Doğrulayıcı(Validator) sınıfları durağan(static) metotları kullanan sınıflardır. işlerinizi kolaylaştırması ve yazılımın bütünleşik parçalar halinde kolektif bir yapıda çalışmasına olanak sağlar.
Bir doğrulama metodunun tanımlaması aşağıdaki gibidir


static function required(Component $c,Component $m,$message,$success)

Birinci parametre “Component $c” doğrulama işleminin yapıldığı bileşen
İkinci parametre “Component $m” mesajların yazdırılacağı bileşen
Üçüncü parametre uyarı mesajı
Dördüncü parametre doğrulama mesajı
Örneğin yeni üye kaydı için bir üye kayıt formumuzun olduğunu ve aynı ismi taşıyabilecek sadece bir üye olduğunu düşünelim.
Bunun için Uyekontrol adında bir sınıf oluşturup varmi ismini taşıyan bir doğrulama metodu ekliyorum

class uyekontrol
{

static function varmi(Component $c,Component $m,$message,$success)
{
$sorgu =  EntityManager::getInstance()->createQuery("select  u from uyeler u where uye.adi =:uyead");
$sorgu->bindParam("uyeadi", $c->getText);
$sorgu->execute();
$sonuc =$sorgu->getSingle();

if($sonuc!=null){
$m->setText($message);

return >true;
}
$m->setText($success);

return >true;
}
}

Şimdi oluşturduğumuz doğrulama sınıfını view (görünüm) içerisinde kullanalım

<faces>
<@import prefix="f" taglib="phpf.ui.*"/>
<f:form method="post" name="uyeform">
Uye adi :<f:textbox
name="uyeadi"
validator="uyekontrol"
rule="varmi"
success="Uye adi doğrulandı "
message=" Daha önce bu isimde bir üye kayıt olmuş farklı bir isim deneyin! "
messagefor="msgbox"
/>
<f:message id="msgbox"/>
<f:button name="kaydet" text="Gönder" onclick="actionevent" forname="uyeform"/>
</f:form>
</faces>

Validador sınıfımızı controller içerisinde import ediyoruz

import("phpf.controllers.facete");
import("dbf.persistence");
import("dogrulayicilar.uyekontrol",true);//uygulama dizini

class Controller extends Facete{
function Controller(){
parent::Facete();
$this->render("uyekayit.phpf");
}
function kaydetClicked($evt){
$uye = new uye();
$uye->adi = $this->uyeadi->text;
$uye->save();
}
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Faces ORM FQL (Faces Query Language)

0

Tarih : 10-09-2009 | Yazan : Hüseyin Bora | Kategori : Faces ORM, PHP Faces

FQL varlık nesneleri üzerinde sorgulama yapmanıza olanak tanıyan SQL e benzer bir sorgulama dilidir. FQL Sorguları PHP Faces Framework tarfından doğal SQL sözcüklerine dönüştürülür.

SELECT İfadesi

SELECT takmaisim FROM sıfıfadı takmaisim WHERE having vb..

Örnekler

SELECT b from Blog b

SELECT c from Categories c

SELECT b from Blog b WHERE bi.id = 1

SELECT b from Blog b WHERE bi.id = 1 and b.name=‘www’

SELECT b from Blog b limit 0,5

SELECT c from Categories c GROUP BY c.id HAVING AVG(c.id) > 1

entity manager ile beraber FQL kullanımı

$query= $this->em->createQuery("SELECT b from Blog b WHERE b.comment.id >1");
$query->execute();
$blogs = $query->getResultList();

FQL ile birlikte SQL fonksiyonları kullanımı

SELECT COUNT(b.id) FROM Blog b

SELECT MAX(b.id) FROM Blog b

entity manager ile beraber kullanımı

$query= $this->em->createQuery("SELECT count(b.id) from bir b");
$query->execute();
echo "count =" .$query->getSingle()."<br>";

İç içe geçmiş alt sorgular

<span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">SELECT b FROM </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 0, 102); letter-spacing: -0.75pt;">Blog</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> b WHERE b</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">coment</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">id </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">=(</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">SELECT MAX</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">(</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">c</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">id</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">)</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> FROM &nbsp;</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 0, 102); letter-spacing: -0.75pt;">Comment</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> c</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">)</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; letter-spacing: -0.75pt;"><o:p></o:p></span></p>
<p class="MsoNormal" style="border: medium none ; padding: 0in;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">SELECT o FROM </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 0, 102); letter-spacing: -0.75pt;">Object</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> o WHERE o</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">id </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">=</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">(</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">SELECT &nbsp;AVG</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">(</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">i</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">id</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">)</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> FROM </span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 0, 102); letter-spacing: -0.75pt;">Object</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> i</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">)</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;"> OERDER BY o</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: rgb(102, 102, 0); letter-spacing: -0.75pt;">.</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; color: black; letter-spacing: -0.75pt;">name</span>

SELECT b FROM Blog b WHERE b.coment.id =(SELECT MAX(c.id) FROM Comment c)

SELECT o FROM Object o WHERE o.id = (SELECT  AVG(i.id) FROM Object i) ORDER BY o.name

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Faces ORM Varlık Sınıfları Arasında İlişkiler

0

Tarih : 10-09-2009 | Yazan : Hüseyin Bora | Kategori : Faces ORM, PHP Faces

ilişkisel annotationlar

@OneToOne

@OneToMany

@ManyToOne

@ManyToMany

üç paremetreleri bulunur

mappedBy = “Sınıf adı”

pk = ” primary key ”

fk = ” foreign key”

CREATE TABLE `blog` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `content` text,
  PRIMARY KEY  (`id`)
)
 CREATE TABLE  comment` (
`comentid` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`content` TEXT NOT NULL ,
`blogid` INT NOT NULL
)

Yukarıdaki yapı şeklinde iki tablomuz olsun
blog ile comment arasında bire e çok
comment ile blog  arasında bire e bir ilişki bulunur.

Buna göre varlık sınıflarımızı aşağıdaki gibi tanımlamız gerekir.

/**
*  @Table(name = "blog")
*/
class Blog extends Entity{
/**
@Id
@Column(name = "id" ,type = "integer")
*/

private $id;
/**
@Column(name = "name" ,type = "string")
*/

private $name;
/**
@Column(name = "content" ,type = "text")
*/

private $content;
/**
@OneToMany(mappedBy = "Comment",pk="id",fk="blogid")
*/

private $comments;//ArrayObject

function Blog(){
parent::Entity();
$this->comments= new ArrayObject();
}

public function set($name, $value) {
$this->$name= $value;
}

public function get($name) {

return $this->$name;
}
}
/**
*  @Table(name = "comment")
*/
class Comment extends Entity{
/**
@Id
@Column(name = "commentid" ,type = "integer")
*/

private $id;
/**
@Column(name = "content" ,type = "text")
*/

private $content;
/**
* @Column(name = "blogid",type ="INT")
* @OneToOne(mappedBy = "blog", pk= "id", fk= "blogid")
*/

private $blogid;

public function set($name, $value) {
$this->$name= $value;
}

public function get($name) {

return $this->$name;
}
}

Controller sınıfımız

import("phpf.controllers.facescontroller");
import("dbf.persistence");
import("models.*",true);
class Blogtest extends FacesController
{

public function  Blogtest () {
parent::FacesController();
$blog = new Blog();
$blog->name="Hello World";
$blog->content="Some text";
$comment = new Comment();
$comment->content= "New Comment";
$blog->comments->append($comment);
EntityManager::getInstance()->save($blog);
}
}

Eğer entitylerinizi Faces Generator ile oluşturmak isterseniz ilişkilerinizi önceden SQL sorgularınızda  bildirmeniz yerinde olacaktır. Faces Generator ilişkileri tanır ve buna göre sınıflar oluşturur.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Faces ORM Entity Manager (Varlık Yöneticisi)

0

Tarih : 10-09-2009 | Yazan : Hüseyin Bora | Kategori : Faces ORM, PHP Faces

Entity Manager (Varlık Yöneticisi) isminden de anlaşılacağı üzeri varlık sınıflarınız üzerinde temel işlemleri gerçekleştirmenizi sağlar. varlık yöneticisi ister controller sınıfınızda isterseniz entity sınıflarınız içersinde kullanın.

Metotlar

bir varlığı veri tabanına kaydetmek duruma göre insert ya da update sözcükleri üretilir

EntityManager::save(Entity $e)

Örnek

$blog = new Blog();
$blog->name="Hello world";
$blog->content="xxxx";
EntityManager::getInstance()->save($blog);

bir varlığı veri tabanınıda aramak için EntityManager’ın find metotdu kullanılır. Bu metodun ilk parametresi aranacak varlık sınıfı ikinci parametersi bu varlığa karşılık gelen birincil anahtar primary key dir.

EntityManager::find(Entity $e,int Id)

Örnek

$em = EntityManager::getInstance();
$blog = $em->find("Blog",2);
echo $blog->name;
echo $blog->content;

bir varlığı veri tabanınıdan silmek için EntityManager’ın delete  metotdu kullanılır.

Entity EntityManager::delete(Entity $e)

Örnek

$em = EntityManager::getInstance();
$blog = $em->find("Blog",2);
$em->delete($blog);

FQL sorguları işletmek  için EntityManager’ın createQuery  metotdu kullanılır. Bu methot string bir parametre alır ve bir Query nesnesi dödürür.

Query nesnesi PDOStatement nesnesinden genişletilmiştir.

FQL le ilerki yazılarda değiniceğim.

Query EntityManager::createQuery(String FQL)

Örnek

$em=  EntityManager::getInstance();
$query =  $em->createQuery("select b from Blog b where b.id =:id");
$id=11;
$query->bindParam("id", $id);
$query->execute();
print_r($query->getResultList())

SQL sorguları işletmek  için EntityManager’ın nativeQuery metotdu kullanılır. Bu methot string bir parametre alır ve bir Query nesnesi dödürür.

Query nesnesi PDOStatement nesnesinden genişletilmiştir.

Query EntityManager::nativeQuery(String SQL)

Örnek

$em=  EntityManager::getInstance();
$query =  $em->createQuery("select* from blog");
$query->execute();

Query nesneleri PDOStatement‘i genişletiği için incelemenizi öneririm.

PDO Transaciton ları destekler

beginTransaction();
commit();
rollBack();

Örnek

Varlık sınıfı içinde EntityManager kullanımı

/**
*  @Table(name ="comment")
*/
class Comment extends Entity{
/**
@Id
@Column(name = "id" ,type = "integer")
*/

private $id;
/**
@Column(name = "content" ,type = "text")
*/

private $content;

public function set($name, $value) {
$this->$name= $value;
}

public function get($name) {

return $this->$name;
}
function save() {
$em = Manager::getInstance();
$em->beginTransaction();

try {
$em->save($this);
}
$em->commit();
} catch (Exception $e) {
$em->rollBack();
}
}
function delete() {
$em = Manager::getInstance();
$em->beginTransaction();

try {
$em->delete($this);
}
$em->commit();
} catch (Exception $e) {
$em->rollBack();
}
}}

Örnek Yukarıdaki Varlık sınıfının kontrolcü içerisinde kullanılması

import("phpf.controllers.facescontroller");
import("dbf.persistence");
import("models.*",true);
class Commenttest extends FacesController
{

public function  Commenttest () {
parent::FacesController();
$blog = new Comment();
$blog->content="bazı yazılar";
$blog->save();
echo "kayıt tamamdır ";
}
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)