Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 10th, 2012  |  syntax: None  |  size: 2.94 KB  |  hits: 17  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. namespace App\Model;
  4. use \DateTime;
  5.  
  6. /** @Entity */
  7. class Blog {
  8.  
  9.         /**
  10.         * @Id
  11.         * @Column(length=40)
  12.         */
  13.         public $name;
  14.  
  15.         /**
  16.         * @Column(length=255)
  17.         */
  18.         public $description;
  19.  
  20.         /**
  21.         * @ManyToMany(targetEntity="Note", inversedBy="blogAppearances")
  22.         * @JoinTable(
  23.         *       name="BlogPost",
  24.         *       joinColumns={@JoinColumn(name="blog_name", referencedColumnName="name")},
  25.         *       inverseJoinColumns={@JoinColumn(name="note_id", referencedColumnName="id")}
  26.         * )
  27.         */
  28.         public $posts;
  29.  
  30.         // Returns a list of all Blogs accessible by the current User.
  31.         public static function getList() {
  32.  
  33.                 $blog = $GLOBALS['ORM']->createQuery("SELECT Blog from App\Model\Blog Blog");
  34.                 return($blog->getResult());
  35.  
  36.         }
  37.  
  38.         // Returns a blog.  You can get it either by name or by id.
  39.         public static function getBlog($blog) {
  40.  
  41.                 if(is_string($blog)) {
  42.                         $query = $GLOBALS['ORM']->createQuery("SELECT Blog from App\Model\Blog Blog WHERE Blog.name=?1");
  43.                 }
  44.                 elseif(is_int($blog)) {
  45.                         $query = $GLOBALS['ORM']->createQuery("SELECT Blog from App\Model\Blog Blog WHERE Blog.id=?1");
  46.                 }
  47.                 else {
  48.                         return(null);
  49.                 }
  50.  
  51.                 $query->setParameter(1, $blog);
  52.  
  53.                 try {
  54.                         $blog = $query->getSingleResult();
  55.                 }
  56.                 catch(NoResultException $ex) {
  57.                         return(null);
  58.                 }
  59.  
  60.                 return($blog);
  61.  
  62.         }
  63.  
  64.         // Returns the 5 (or specified) most recent posts.
  65.         public function getLatestPosts($max = 3) {
  66.  
  67.                 // http://www.doctrine-project.org/projects/orm/2.0/docs/reference/dql-doctrine-query-language/en#select-queries:joins
  68.                 // Fetch-join user information along with the posts!
  69.                 $posts = $GLOBALS['ORM']->createQuery("SELECT Note, User from App\\Model\\Note Note LEFT JOIN Note.blogAppearances ba JOIN Note.poster User WHERE ba.name=?1 ORDER BY Note.created DESC");
  70.                 $posts->setMaxResults($max);
  71.                 $posts->setParameter(1, $this->name);
  72.  
  73.                 return($posts->getResult());
  74.  
  75.         }
  76.  
  77.         // Receives data for a new BlogPost and Note records.
  78.         public function createPost($text) {
  79.  
  80.                 // Create a new Note.
  81.                 $note = new Note();
  82.  
  83.                 $note->text = $text;
  84.                 $note->created = new DateTime();
  85.                 $note->poster = $_SESSION['User'];
  86.                 //$note->user_id = $_SESSION['User']->id;
  87.                 $note->blogAppearances[] = $this;
  88.                 $this->posts[] = $note;
  89.  
  90.                 // Add it to the graph.
  91.                 $GLOBALS['ORM']->persist($note);
  92.                 $GLOBALS['ORM']->flush();
  93.  
  94.                 return(true);
  95.                 return(false);
  96.  
  97.         }
  98.  
  99. }
  100.  
  101. ?>
  102.  
  103. <?php
  104.  
  105. namespace App\Model;
  106.  
  107. /** @Entity */
  108. class Note {
  109.  
  110.         /**
  111.         * @Id
  112.         * @Column(type="integer")
  113.         * @GeneratedValue(strategy="AUTO")
  114.         */
  115.         public $id;
  116.  
  117.         /**
  118.         * @Column(type="text")
  119.         */
  120.         public $text;
  121.  
  122.         //public $title;
  123.  
  124.         ///**
  125.         //* @Column(type="integer")
  126.         //*/
  127.         //public $user_id;
  128.  
  129.         /**
  130.         * @Column(type="datetime")
  131.         */
  132.         public $created;
  133.  
  134.         /**
  135.         * @OneToOne(targetEntity="OmegaTech\System\User")
  136.         * @JoinColumn(name="user_id", referencedColumnName="id")
  137.         */
  138.         public $poster;
  139.  
  140.         /**
  141.         * @ManyToMany(targetEntity="Blog", mappedBy="posts")
  142.         */
  143.         public $blogAppearances;
  144.  
  145. }
  146.  
  147. ?>