Expanding the Blog Tutorial with comments on the Blog Home Page

7 months ago

Se7en
Member | 13
+
0
-

Hi all, I'm currently working my way through the Blog tutorial and I am wondering what the best way would be to add a link to the last comment under each blog on the home page?

At the moment each blog item is looped like so:

Blog Date
Blog Title
Blog Content

But I would like to extend it to something like this:

Blog Date
Blog Title
Blog Content
Last Comment By …

Thanks

7 months ago

Se7en
Member | 13
+
0
-

Actually, after experimenting all night I have an easy solution but I have a feeling it will be frowned upon as bad practice. Either way, I'll post my updated code tomorrow in hope that someone can explain if it's right or wrong.

7 months ago

Se7en
Member | 13
+
0
-

So basically I have changed the Blog example by creating a model for the database and the presenter grabs the content like so.....

$content = $this->content->findAllContent()
    ->limit(10)
    ->order('created_at ASC');

$this->template->content = $content;

Now I know I can loop through the content here within the presenter but it's easier for me to do it within the view. But I am not sure if it is bad practice? So far I have the following within the default.latte view file.

{block content}
{foreach $content as $item}
    {var $author = $item->ref('users', 'author_id')}
    {var $comments = $item->related('comments')}
    {var $commentCount = $comments->count()}
    {var $lastCommentId = $comments->order('created_at DESC')->fetch()}

    <h4 class='title'><a href="{link Content:show $item->id}">{$item->title}</a></h4>
    <p class='subtitle is-6'>By: {$author->username} | {$item->created_at|date:'F j, Y h:i:s A'}</p>
    <div>{$item->body}</div>
    <div>Comments: {$commentCount} | Last by: {$lastCommentId}</div>
    <hr>
{/foreach}
{/block}

Is it ok to do it this way? Any feedback would be appreciated.

7 months ago

Se7en
Member | 13
+
0
-

Sorry for the fuss, I kind of guess this is more of a Czech language framework. It's a shame as it's one of the best PHP frameworks from what I can tell.

7 months ago

netteman
Member | 76
+
+1
-

Hi,

have a look at https://doc.nette.org/…ase-explorer#…

Depending on your database you can use

{$item->users->username}

in the template instead of

{var $author = $item->ref('users', 'author_id')}
{$author->username}

Last edited by netteman (2019-03-04 07:11)

6 months ago

Se7en
Member | 13
+
0
-

Thanks for the reply @netteman

For some reason the following that you suggested doesn't work:

{$item->users->username}

It throws up the following error “Cannot read an undeclared column ‘users’”

My database is InnoDB with a ‘users’ ‘content’ and ‘comments’ tables and both the content and comments tables have a foreign key (author_id) relating to the users->id

My database model is as follows:

public function __construct(Nette\Database\Context $database)
{
    $this->database = $database;
}

public function findAllContent()
{
    $findAllContent = $this->database->table('content');
    return $findAllContent;
}

And finally my presenter is like so:

public function __construct(Model\ContentRepository $content)
{
    parent::__construct();
    $this->content = $content;
}


public function renderDefault(): void
{

    $content = $this->content->findAllContent();
    $this->template->content = $content;

}

I'm not sure if you can see anything I've done wrong? Thanks again.

6 months ago

Roman Halaxa
Member | 42
+
+1
-

Hi @Se7en . First of all, welcome to the nette community :)

In my opinion there's nothing wrong with pulling the data in template. Not for now anyways. From what I learned there's always a way to refactor one step further and that it's also not a great idea to have an enormous amount of decomposition for a simple project like this blog example. It's not a bad practice as far as I'm concerned. You're good :)

In the future, you could always create a component for comments. Components are handy because they have their very own template ! :) They can also form a hierarchy which is suitable for example for replies and replies of replies (kinda like on reddit).

I'm sorry you didn't get the attention you should. It's true nette is mainly popular in it's home country but it's constantly trying to appeal to a wider community of foreign devs too ! In fact, thanks to the crowdfunding that's been taking place here , there is new guides and similar content coming that should be able to be professionaly translated into English, so stick around for that ! :)

Last edited by Roman Halaxa (2019-03-06 07:58)

6 months ago

Tyraxor
Member | 29
+
+1
-

Hi, you have to use name of the column that is making the relation, without the “_id”, so something like this:

{$item->author->username}

6 months ago

netteman
Member | 76
+
+1
-

@Se7en As I said, it depends on your database. In this database https://files.nette.org/…chema-1-.png you can get the rows from the table “book” like this:

$this->template->books = $this->database->table('book');

and here's how you get the data in the template

{foreach $books as $book}

    Title: {$book->title}<br>
    Author: {$book->author->name}<br>
    Another way how to get the author: {$book->ref('author', 'id')->name} which is a bit more difficult to write<br>
    <hr>

{/foreach}

Last edited by netteman (2019-03-06 19:50)

6 months ago

Se7en
Member | 13
+
+1
-

@RomanHalaxa @Tyraxor @netteman

Thank you all so much, I have my initial issues sorted now thanks to you all.

I'm slowly understanding things, but the general documentation leaves me with more questions than answers and I really don't want to come across as a pain by constantly firing out questions.

I do however want to create a tutorial on how to create a simple forum. Now I know most people frown over custom forums, but a simple forum isn't that hard when it is broken down into users/profiles/categories/topics/replies and I reckon it would be a great way to learn about the database, users, expanding the user's table to become flexible for custom profiles etc. I have coded a few private forums like this one, but I am new to PHP Frameworks and MVC in general. Basically I'd like to create a minimal alternative to the Sandbox and be able to expand it in a clear manner where every action and file is explained.

Anyway, I'm sure you'll class me as crazy but I want to give it a go if anyone else is interested.

Thanks

Last edited by Se7en (2019-03-08 02:02)

6 months ago

Roman Halaxa
Member | 42
+
+2
-

Se7en wrote:

@RomanHalaxa @Tyraxor @netteman

Thank you all so much, I have my initial issues sorted now thanks to you all.

I'm slowly understanding things, but the general documentation leaves me with more questions than answers and I really don't want to come across as a pain by constantly firing out questions.

I do however want to create a tutorial on how to create a simple forum. Now I know most people frown over custom forums, but a simple forum isn't that hard when it is broken down into users/profiles/categories/topics/replies and I reckon it would be a great way to learn about the database, users, expanding the user's table to become flexible for custom profiles etc. I have coded a few private forums like this one, but I am new to PHP Frameworks and MVC in general. Basically I'd like to create a minimal alternative to the Sandbox and be able to expand it in a clear manner where every action and file is explained.

Anyway, I'm sure you'll class me as crazy but I want to give it a go if anyone else is interested.

Thanks

Thats a great way to start :) Nette might be confusing at first but it all starts falling together once you spend enough time with it. Best luck with your goals

6 months ago

Se7en
Member | 13
+
0
-

Thanks Roman, you have been very helpful.

If you don't mind, I might have to pick your brains again as more questions arise, but I am trying to work things out myself. It is making far more sense now I have started from scratch. The Sandbox was leaving me with too many questions. I really want to come up with some kind of forum tutorial for the site, so any support and feedback will be great.

6 months ago

Roman Halaxa
Member | 42
+
+1
-

Se7en wrote:

Thanks Roman, you have been very helpful.

If you don't mind, I might have to pick your brains again as more questions arise, but I am trying to work things out myself. It is making far more sense now I have started from scratch. The Sandbox was leaving me with too many questions. I really want to come up with some kind of forum tutorial for the site, so any support and feedback will be great.

Hey, no problem. You can hit me up on discord, I've sent you an email with my discord info.

6 months ago

Se7en
Member | 13
+
0
-

Thanks. I didn't receive an e-mail but it's ok as I don't use Discord :D