- <?php
- namespace App\Model;
- use \DateTime;
- /** @Entity */
- class Blog {
- /**
- * @Id
- * @Column(length=40)
- */
- public $name;
- /**
- * @Column(length=255)
- */
- public $description;
- /**
- * @ManyToMany(targetEntity="Note", inversedBy="blogAppearances")
- * @JoinTable(
- * name="BlogPost",
- * joinColumns={@JoinColumn(name="blog_name", referencedColumnName="name")},
- * inverseJoinColumns={@JoinColumn(name="note_id", referencedColumnName="id")}
- * )
- */
- public $posts;
- // Returns a list of all Blogs accessible by the current User.
- public static function getList() {
- $blog = $GLOBALS['ORM']->createQuery("SELECT Blog from App\Model\Blog Blog");
- return($blog->getResult());
- }
- // Returns a blog. You can get it either by name or by id.
- public static function getBlog($blog) {
- if(is_string($blog)) {
- $query = $GLOBALS['ORM']->createQuery("SELECT Blog from App\Model\Blog Blog WHERE Blog.name=?1");
- }
- elseif(is_int($blog)) {
- $query = $GLOBALS['ORM']->createQuery("SELECT Blog from App\Model\Blog Blog WHERE Blog.id=?1");
- }
- else {
- return(null);
- }
- $query->setParameter(1, $blog);
- try {
- $blog = $query->getSingleResult();
- }
- catch(NoResultException $ex) {
- return(null);
- }
- return($blog);
- }
- // Returns the 5 (or specified) most recent posts.
- public function getLatestPosts($max = 3) {
- // http://www.doctrine-project.org/projects/orm/2.0/docs/reference/dql-doctrine-query-language/en#select-queries:joins
- // Fetch-join user information along with the posts!
- $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");
- $posts->setMaxResults($max);
- $posts->setParameter(1, $this->name);
- return($posts->getResult());
- }
- // Receives data for a new BlogPost and Note records.
- public function createPost($text) {
- // Create a new Note.
- $note = new Note();
- $note->text = $text;
- $note->created = new DateTime();
- $note->poster = $_SESSION['User'];
- //$note->user_id = $_SESSION['User']->id;
- $note->blogAppearances[] = $this;
- $this->posts[] = $note;
- // Add it to the graph.
- $GLOBALS['ORM']->persist($note);
- $GLOBALS['ORM']->flush();
- return(true);
- return(false);
- }
- }
- ?>
- <?php
- namespace App\Model;
- /** @Entity */
- class Note {
- /**
- * @Id
- * @Column(type="integer")
- * @GeneratedValue(strategy="AUTO")
- */
- public $id;
- /**
- * @Column(type="text")
- */
- public $text;
- //public $title;
- ///**
- //* @Column(type="integer")
- //*/
- //public $user_id;
- /**
- * @Column(type="datetime")
- */
- public $created;
- /**
- * @OneToOne(targetEntity="OmegaTech\System\User")
- * @JoinColumn(name="user_id", referencedColumnName="id")
- */
- public $poster;
- /**
- * @ManyToMany(targetEntity="Blog", mappedBy="posts")
- */
- public $blogAppearances;
- }
- ?>