Display lastest dotclear posts in symfony

Here is a little tip to display the latest posts of your dotclear blog in your symfony website.

Create a connection

If your blog database is in an other database than symfony's one, you need to create a connection in the /config/databases.yml. Otherwise, skip this step Mine looks like this :

[yml]
dev:
  beta:
    param:
      classname:  DebugPDO

test:
  beta:
    param:
      classname:  DebugPDO

all:
  symfony:
    class:        sfDoctrineDatabase
    param:
      classname:  DoctrinePDO
      dsn:        mysql:dbname=mysymfonydbanem;host=localhost
      username:   mysymfonyusername
      password:   mysymfonypassword
      encoding:   utf8
      persistent: true
      pooling:    true
  blog:
    class:        sfDoctrineDatabase
    param:
      classname:  DoctrinePDO
      dsn:        mysql:dbname=myblogdbname;host=localhost
      username:   myblogusername
      password:   myblogpassword
      encoding:   utf8
      persistent: true
      pooling:    true

Read more about connections.

Create Dotclear models from the database

You must create models from the database to be able to retrieve the posts

$ ./symfony doctrine:build-schema --env=blog
$ ./symfony doctrine:build-model

As you can see, some Dc*.class.php are now created in the /lib/model/doctrine/ folder.

Fetching latests posts

In your controller (actions.class.php), use and customize this request :

public function executeIndex(sfWebRequest $request)
  {
    $this->posts = Doctrine_Query::create()
        ->from('DcPost p')
        ->where('p.post_status = 1')
        ->orderBy('p.post_creadt DESC')
        ->limit(5)
        ->execute();
  }

Displaying posts

<?php foreach($posts as $post) : ?>
  <div class="blog_box">
    <h5><a href="#"><?php echo $post->getPostTitle() ?></a></h5>
    <?php echo __('Posted by') ?> <a href="http://url.to.your.blog/path/index.php?post/<?php echo $post->getPostUrl() ?>"><span><?php echo $post->getDcUser()->getUserDisplayname () ?></span></a> <?php echo __('in') ?> <a href="#"><span><?php echo $post->getDcCategory()->getCatTitle() ?></span></a><br />
    <?php echo substr(strip_tags($post->getRawValue()->getPostContentXhtml()), 0, 40).'...' ?><br />
    <div class="datetime"><?php echo $post->getPostCreadt() ?></div>
  </div>
<?php endforeach; ?>

and that's it, you made it ! :)

PS: This howto is maybe somehow wrong, I made those steps few times before making this working. Feel free to correct.

Update 1 : Added a where clause to select only posts where post_status = 1. Added the link to the post

Haut de page