«

»

Eyl
10

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

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)

Bir Cevap Yazın

E-Posta adresiniz yayınlanmayacaktır.


*

Bu HTML etiket ve tanımlayıcılarını kullanabilirsiniz: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>