Reputation: 1665
I am working on a Symfony2 API just to learn stuff. What I am trying to achieve is this:
Load templates from DB this is working. Now I am trying to output retrieved templates from my database in browser and this is where I am struggling.
So my Doctrine is working and retrives the templates, I also created TemplateResponse where I want to populate each construc variable with Doctrine array key => value.
TemplateResponse file:
<?php
namespace Boltmail\UserBundle\BoltmailResponse;
class TemplateResponse {
/**
* @var integer
*/
public $templates;
/**
* @param $templates
*/
public function __construct(
$templates
){
$this->templates = $templates;
}
}
TemplateListFactory:
<?php
namespace Boltmail\UserBundle\BoltmailFactory;
use Boltmail\UserBundle\BoltmailRepository\TemplateListRepository;
use Boltmail\UserBundle\BoltmailResponse\TemplateResponse;
use Boltmail\UserBundle\BoltmailResponse\Response;
use Symfony\Component\HttpFoundation\Request;
class TemplateListFactory {
public $template;
public function __construct(
TemplateListRepository $templateListRepository
){
$this->template = $templateListRepository;
}
public function build()
{
$template = $this->template->searchTemplate();
if ($template) {
return new TemplateResponse (
$template
);
} else {
return new Response(
false,
'Something went wrong'
);
}
}
}
Error:
Notice: Trying to get property of non-object
if ($template) {
return new TemplateResponse (
$template->temp_id,
$template->title,
$template->content,
$template->author
So I believe Doctrine returns the data as an array I guess and I am trying to access its values as if they were objects that's why I get this error.
I also tried using foreach loop inside If statement before populating TemplateReponse but this also did not work. Any idea how I can make this work.
OK I made some changes and I got the result I was after:
Result:
{
templates: [
{
temp_id: "0",
title: "New Template",
content: "I am new template blehhhhhh",
author: "Michael"
},
{
temp_id: "1",
title: "Liverpool Template",
content: "You Will Never Walk Alone",
author: "Bob"
}
]
}
Upvotes: 0
Views: 106
Reputation:
You are trying to achieve something that already exists inside Symfony: it's called Doctrine ORM.
Upvotes: 3