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)

Faces Entity (Varlıklar)

0

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

Entity Varlık sınıfları Entity sınıfından genişletilir. genellikle import işlemlerini controllerda yaparsınız. aşağıdaki satırların controller da bulunması iyidir.

import("dbf.persistence");
import("models",true);

import fonksiyonun ikinci parametresinin true verilmesi durumda bu fonksiyon uygulamaya dahil edilecek dosyalara application/uygulamaadi/ içerisinede bakar.

Tekrar konumza yani varlık sınıflarına dönelim. varlık sınıflarını veri tabanındaki tablolarınız ile ilişkilendirirken haritalarken annotationları kullanırsınız.

Annotationlar

@Table(name = “blog”) tablo adı tanımlanır class sözcüğün üzerine yazılır
Name : parametresine tablo adı yazılır

@Column(name = “name” ,type = “string”) alan tanımlaması yapılır

Name :niteliğine alan adı yazılır. ilişkili private üyenin üzerine yazılmaldır.
üye private olmalıdır bu önemlidir çünki Faces ORM private üyelere tanır.

@Id birincil anahtarı tanımlar

ilişkisel annotationlar

@OneToOne

@OneToMany

üç paremetreleri bulunur

mappedBy = “Sınıf adı”

pk = ” primary key ”

fk = ” foreign key”

Olayı daha iyi kavrayabilmeniz için örneği inceleyin.

CREATE TABLE `blog` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`content` text,
PRIMARY KEY  (`id`)
)

Yukarıdaki gibi bir tablomuz olsun.

/**
*  @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;

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

public function get($name) {

return $this->$name;
}
}

Entity sınıfında hangi alanların güncellendiğini tespit etmek için __set ve __get metotları aşırı yüklenmiştir. bu yüzden set ve get metotlarını kullanın

dilerseniz her üye için get ve set metotlarınıda uygulaya bilirsiniz Faces ORM her iki yaklaşımıda benimser.

Varlık siniflarınızı application/uygulamaadi/models/ dizinine yerleştirin
application/uygulamaadi/models/blog.php gibi

Gelelim Controller içerisinden varlık nesnelerimize nasıl erişeceğimize.

import("phpf.controllers.facescontroller");
import("dbf.persistence");
import("models.*",true);//application/models dizini
class BlogController extends FacesController
{

public function  BlogController () {
parent::FacesController();
$blog = new Blog();
$blog->name="Hello World";
$blog->content="Some text";
EntityManager::getInstance()->save($blog);
//EntityManager::getInstance()->delete($blog);
}
}

Yukarıdaki satırlarda yeni bir Blog nesnesi oluşturulur değerler atanır ve son olarak blog nesnesi veri tabanına insert edilir.eğer blog nesnesi new ile kendi oluşturduğumuz bir nesne olmasaydı bu onun update edileceği anlamına gelmekteydi EntityManager::save(Entity) metodu varlık nesnesinin veritabındanmı yoksa yeni bir nesnemi olduğunu algılar ve bu duruma göre ya insert ya da update sözcükleri oluşturup veritabanı sunucusuna gönderir.

Varlık sınıflarını her seferinde elinizle yazmak zorunda değilsiniz. bunun için geliştirdiğim ufak bir java uygulaması var.
http://code.google.com/p/php-faces/downloads/list adresinden generator.zip dosyasını indirin. uygulamayı kullanabilmeniz için sisteminizde java vm nin kurulu olması kafidir. generator programı sizin için varlık sınıflarını oluşturacaktır.

tabiki bu kadar değil bir sonraki yazı EntityManager olacak.

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 Modeline Giriş

0

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

Önceki yazılarda da belirtiğim gibi PHP Faces framework model katmanında ORM (Object Relation Mapping) PHP Faces ORM yapısından faydalana bilmeniz için PHP PDO eklentisini aktif durumda olmalı.
PDO eklentisini php.ini dosyasından aktif duruma getirebilirsiniz. windows işletim sistemi için uzantılar .dll şeklinde olacak.

extension = pdo.so
extension = pdo_sqlite.so
extension = sqlite.so
extension = pdo_mysql.so

ORM programcıyı CRUD(Create, read, update,delete) işlemlerini kolaylaştıran yeni bir yöntem olarak görünmesine karşın bir hayli eski ve yaygın bir yöntemdir.

ORM ile PHP sınıflarınızı Veri tabanınızdaki tablolar ile ilişklendirir ve pek çok işlemi sorgu gerektirmeden yapabilirsiniz. aşağıda bazı ufak örnekler var

$user = new User("Username","password");
$user->save();
$user = EntityManager::getInstance()->find("User",12);
$user->delete();

Veri tabanı yapılandırması
applications/config.php dosyasını bir metin editorü ile düzenlemek üzere açın burada PHP Faces ORM veritabanı erişim yapılandırması bulunur.

define("DB_CONNECTION_STRING","mysql:host=sunucuadi;dbname=veritabanı");
define("DB_USER","kullanıcıadı");
define("DB_PASS","parola");
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)